据我所知,ffmpeg-python是Python中直接操作ffmpeg的主要包。
现在,我想拍摄一段视频,并将其帧保存为一些fps的单独文件。
有很多命令行方法可以做到这一点,例如ffmpeg -i video.mp4 -vf fps=1 img/output%06d.png described here
但我想用Python来实现。还有一些解决方案[1] [2]使用Python的subprocess来调用ffmpeg命令行界面,但在我看来它看起来很脏。
有没有什么方法可以用ffmpeg-python来实现?
发布于 2020-10-01 02:28:44
我建议您尝试使用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)发布于 2020-11-11 23:07:41
下面的方法对我很有效:
ffmpeg
.input(url)
.filter('fps', fps='1/60')
.output('thumbs/test-%d.jpg',
start_number=0)
.overwrite_output()
.run(quiet=True)发布于 2020-10-01 03:03:24
您也可以使用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.")https://stackoverflow.com/questions/64143387
复制相似问题