使用 python 从网络摄像头修剪(删除帧)实时视频 [英] Trim (remove frames from) live video from webcam using python

查看:38
本文介绍了使用 python 从网络摄像头修剪(删除帧)实时视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以捕捉视频流的网络摄像头.30 秒后,我想从一开始就删除 1 秒的视频并继续捕获视频流等等.简而言之,我只想保存直播视频的最新 30 秒.

I have a webcam that captures video stream. After 30 seconds I want to remove 1 sec of the video from the start and keep capturing the video stream and so on. In short, I only want to save the latest 30 seconds of the live video.

OpenCV 不提供视频处理

OpenCV does not provide video processing

ffmpeg 修剪视频但创建了一个新的输出文件,我不想保留副本.

ffmpeg trims the video but creates a new output file, I don't want to keep copies.

#Create a video write before entering the loop
#Create a video write before entering the loop
video_writer = cv2.VideoWriter(
    video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
)

#video_file is the file being saved

start = time.time()
i=0
seconds='1'
while cap.isOpened():


    ret, frame = cap.read()
    if ret == True:
        cv2.imshow("frame", frame)
        if time.time() - start > 10:
            print('video > 10 sec')
        
            subprocess.call(['ffmpeg', '-i', video_file, '-ss', seconds, 'output.avi'])
            break

    # Write the frame to the current video writer
    video_writer.write(frame)
    if i%24 == 0:
        cv2.imwrite('image'+str(i)+'.jpg',frame)
    i+=1
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break
else:
    break
cap.release()
cv2.destroyAllWindows()

我正在寻找的是如何让实时视频跳闸并不断保存未来的帧,以便视频不超过 30 秒并保留最新的帧.

What I am looking for is how we can trip a live video and keep saving future frames so that the video don't exceed 30 seconds and keeps the latest frames.

推荐答案

您可以使用 segment多路复用器:

ffmpeg -i input -f segment -segment_time 30 -segment_wrap 2 -reset_timestamps 1 output_%d.avi

这将创建两个文件:output_0.avioutput_1.avi.每 30 秒,输出将在这些文件之间交替并覆盖早期版本.

This will create two files: output_0.avi and output_1.avi. Every 30 seconds the output will alternate between these files and overwrite earlier versions.

您可以使用 -segment_wrap 1 并且只输出 1 个文件,但您可能会丢失视频.例如,在 32 秒停止录制会让您看到 2 秒的视频,而不是 30 秒的视频 + 2 秒的视频.

You could use -segment_wrap 1 and only output 1 file, but you risk losing video. For example stopping recording at 32 seconds will leave you with a 2 second video instead of a 30 second video + a 2 second video.

这篇关于使用 python 从网络摄像头修剪(删除帧)实时视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆