gpt4 book ai didi

python - 不使用 shell-True 提取帧数

转载 作者:行者123 更新时间:2023-12-04 23:10:03 24 4
gpt4 key购买 nike

我有以下代码从视频中提取帧数:

filename = "test.mp4"
video_path = os.path.join(os.getcwd(), filename)
with open(video_path, "rb") as file:
cmd = f'ffmpeg -i {video_path} -r 10 -q:v 3 -s 1x1 -f image2 -update 1 - >/dev/null'
proc = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
(stdout, stderr) = proc.communicate()

# i.e: 'frame= 2062 fps=966 q=3.0 Lsize=N/A time=00:10:34.46 bitrate=N/A dup=1049 drop=3563 speed= 297x'
decoded = stderr.decode('utf-8')
frame_count_line = decoded.splitlines()[-2]
m = FRAME_COUNT_REGEX.search(frame_count_line)
if m:
print(int(m.groups()[0]))
但出于安全原因,我想删除 shell=True范围。
不幸的是,之后上面的代码抛出:
FileNotFoundError: [Errno 2] No such file or directory
请帮忙修复

最佳答案

将您的命令放入列表中,例如

import subprocess
media = 'some media.mp4'
try:
comm = subprocess.Popen(['ffmpeg', '-hide_banner', '-i', media, '-f', 'null', '-'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
except Exception as e:
print("Error: ", str(e))

try:
with comm.stdout:
for i in iter(comm.stdout.readline, b''):
if i != '':
print(i.strip())
else:
break
except Exception as e:
print("Error: ", str(e))
您也可以使用 ffprobe而不是 ffmpeg因为 ffprobe允许您选择 -count_frames 之类的内容和 -show_frames .
计数帧只会输出最终的数字,所以如果你需要显示发生了什么,你可能会更好 -show_frames .
例如
ffprobe -show_frames -count_frames -v error -select_streams v:0 -show_entries stream=nb_read_frames -show_entries frame=coded_picture_number -of default=nokey=1:noprint_wrappers=1 -i media
或者
ffprobe -count_frames -v error -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -i media
或者
ffprobe -show_frames -v error -select_streams v:0 -show_entries frame=coded_picture_number -of default=nokey=1:noprint_wrappers=1 -i media

关于python - 不使用 shell-True 提取帧数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71577956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com