[英]Save video clip from longer video between two timestamps in Python cv2
I have an hour-long video that I would like to save a clip between two timestamps-- say, 11:20-11:35.我有一个长达一小时的视频,我想在两个时间戳之间保存一个剪辑——比如 11:20-11:35。 Is the best way to do this frame-by-frame, or is there a better way?是逐帧执行此操作的最佳方法,还是有更好的方法?
Here's the gist of what I did frame-by-frame.这是我逐帧所做的要点。 If there's a less lossy way to do it, I'd love to know!如果有一种损失较小的方法可以做到这一点,我很想知道! I know I could do it from the terminal using ffmpeg, but I am curious for how to best do it using cv2.我知道我可以使用 ffmpeg 从终端执行此操作,但我很好奇如何使用 cv2 最好地执行此操作。
def get_clip(input_filename, output_filename, start_sec, end_sec):
# input and output videos are probably mp4
vidcap = cv2.VideoCapture(input_filename)
# math to find starting and ending frame number
fps = find_frames_per_second(vidcap)
start_frame = int(start_sec*fps)
end_frame = int(end_sec*fps)
vidcap.set(cv2.CAP_PROP_POS_FRAMES,start_frame)
# open video writer
vidwrite = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*'MP4V'), fps, get_frame_size(vidcap))
success, image = vidcap.read()
frame_count = start_frame
while success and (frame_count < end_frame):
vidwrite.write(image) # write frame into video
success, image = vidcap.read() # read frame from video
frame_count+=1
vidwrite.release()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.