简体   繁体   English

可以从OpenCV中的视频文件中删除帧

[英]Possible to delete frames from a video file in opencv

I can open a video and play it with opencv 2 using the cv2.VideoCapture(myvideo) . 我可以打开视频并使用cv2.VideoCapture(myvideo)在opencv 2中播放它。 But is there a way to delete a frame within that video using opencv 2? 但是,有没有一种方法可以使用opencv 2删除该视频中的帧? The deletion must happen in-place, that is, the file being played will end up with a shorter time due to deleted frames. 删除必须就地进行,也就是说,由于删除了帧,正在播放的文件将以较短的时间结束。 Simply zeroing out the matrix wouldn't be sufficient. 仅仅将矩阵归零是不够的。

For example something like: 例如:

video = cv2.VideoCapture(myvideo.flv)

while True:
    img = video.read()

    # Show the image
    cv2.imgshow(img)

    # Then go delete it and proceed to next frame, but is this possible?
    # delete(img)??

So the above code would technically contain 0 bytes at the end since it reads then deletes the frame in the video file. 因此,以上代码从技术上讲将在末尾包含0字节,因为它先读取然后删除了视频文件中的帧。

OpenCV is not the right tool for this job. OpenCV不是适合此工作的工具。 What you need for this is a media processing framework, like ffmpeg (=libavformat/libavcodec/libswscale) or GStreamer. 为此,您需要一个媒体处理框架,例如ffmpeg(= libavformat / libavcodec / libswscale)或GStreamer。

Also depending on the encoding scheme used, simply deleting just a single frame may not be possible. 同样取决于所使用的编码方案,仅删除单个帧可能是不可能的。 Only in a video consisting of just Intra frames (I-frames), frame exact editing is possible. 仅在仅包含帧内帧(I帧)的视频中,才可以进行帧精确编辑。 If the video is encoding in so called group of pictures (GOP) removing a single frame requires to reencode the whole GOP it was part of. 如果视频以所谓的图片组 (GOP)进行编码,则删除单个帧需要对整个GOP进行重新编码。

您不能就地执行此操作 ,但可以使用OpenCV的VideoWriter将所需的帧写入新的视频文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM