如何解决使用ffmpeg-python
据我了解,ffmpeg-python
是Python中直接操作ffmpeg
的主要软件包。
现在,我想拍摄视频并将其帧另存为一些fps的文件。
有很多命令行方法可以做到这一点,例如ffmpeg -i video.mp4 -vf fps=1 img/output%06d.png
described here
但是我想用Python做到这一点。还有一些解决方案[1] [2]使用Python的subprocess
来调用ffmpeg
CLI,但对我来说似乎很脏。
有什么方法可以使用ffmpeg-python
来制作它?
解决方法
我建议您尝试imageio module并使用以下代码作为起点:
import imageio
reader = imageio.get_reader('imageio:cockatoo.mp4')
for frame_number,im in enumerate(reader):
# im is numpy array
if frame_number % 10 == 0:
imageio.imwrite(f'frame_{frame_number}.jpg',im)
,
您也可以使用openCV。
参考代码:
import cv2
video_capture = cv2.VideoCapture("your_video_path")
video_capture.set(cv2.CAP_PROP_FPS,<your_desired_fps_here>)
saved_frame_name = 0
while video_capture.isOpened():
frame_is_read,frame = video_capture.read()
if frame_is_read:
cv2.imwrite(f"frame{str(saved_frame_name)}.jpg",frame)
saved_frame_name += 1
else:
print("Could not read the frame.")
,
以下对我有用:
ffmpeg
.input(url)
.filter('fps',fps='1/60')
.output('thumbs/test-%d.jpg',start_number=0)
.overwrite_output()
.run(quiet=True)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。