提取帧数而不使用shell True



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

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]))

但由于安全原因,我想删除CCD_ 1参数。不幸的是,在那之后上面的代码抛出:

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

相关内容

  • 没有找到相关文章

最新更新