OpenCV将帧附加到现有视频(.avi)文件?

Posted

技术标签:

【中文标题】OpenCV将帧附加到现有视频(.avi)文件?【英文标题】:OpenCV append frame to existing video (.avi) file? 【发布时间】:2012-08-25 01:33:28 【问题描述】:

我有一个小问题。是否可以使用 OpenCV 将新帧添加(附加)到现有的 .avi 视频文件,而不覆盖整个文件?我在带有 QT 的 Windows7 上使用 OpenCV2.4.2。

【问题讨论】:

不。您必须使用 VideoCapture 加载所有视频,重写所有内容,然后添加帧。有关更有效的方法,请查看 ffmpeg。它是一个命令行实用程序,可以拆分、合并从视频中提取的帧、转码等 "OpenCV4adnroid 不支持视频读写。保存一个图像序列,然后从 java 中从这个序列中编码一个视频"***.com/questions/21546906/… 这里是如何使用 ffmpeg ***.com/questions/18452058/… 【参考方案1】:

看看VideoWriter 类。

【讨论】:

【参考方案2】:

如果你想使用OpenCV,你必须读写所有的数据内容。

import cv2
import os

# this two lines are for loading the videos.
# in this case the video are named as: cut1.mp4, cut2.mp4, ..., cut15.mp4
# videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.mp4']
# videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))


videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.avi']
videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))

video_index = 0
cap = cv2.VideoCapture(videofiles[0])

# video resolution: 1624x1234 px
# out = cv2.VideoWriter("video.avi", 
#                       cv2.cv.CV_FOURCC('F','M','P', '4'), 
#                       15, (1624, 1234), 1)

# fourcc = cv2.VideoWriter_fourcc(*'MP4V')
# out = cv2.VideoWriter('cutout.mp4', fourcc, 20, (640, 480))

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('cutout.avi', fourcc, 20.0, (640, 480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if frame is None:
        print ("end of video " + str(video_index) + " .. next one now")
        video_index += 1
        if video_index >= len(videofiles):
            break
        cap = cv2.VideoCapture(videofiles[ video_index ])
        ret, frame = cap.read()
    cv2.imshow('frame',frame)
    out.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

print ("end.")

【讨论】:

以上是关于OpenCV将帧附加到现有视频(.avi)文件?的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV中如何播放avi文件的音频

Python 和 OpenCV - 无法编写可读的 avi 视频文件

如何在 Java 中使用 openCV 的 VideoCapture 方法打开和处理 .mpeg 或 .avi 等视频文件

如何用opencv读取avi视频并在界面截图进行人脸检测

当对象靠近opencv时将帧保存到文件中

OpenCV+visual studio 2019 实现对avi视频或摄像头 laplace边缘检测。从AVI文件( bsd.avi)中得到视频流,对视频流进行Laplace边缘检测,并输出结果。(代