我正在尝试录制Logitech网络摄像头视频。凸轮能够记录它,但是以nX速度仅记录6秒钟的40秒视频。我为解决方案引用了以下link ,但它无法解决RPi中的问题。重要的是代码可以在Ubuntu桌面上找到,但可能是RPi处理较慢。
这是我的代码段:
fourcc = cv2.cv.CV_FOURCC(*'XVID')
videoOut = cv2.VideoWriter("video_clip.avi", fourcc, 20.0, (640, 480))
start_time = time.time()
frame_count = 0
while True:
ret, frame = cap.read()
videoOut.write(frame) # write each frame to make video clip
frame_count += 1
print int(time.time()-start_time) # print the seconds
if int(time.time()-start_time) == 10:
videoOut.release()
break
# get out of loop after 10 sec video
print 'frame count =', frame_count
# gives me 84 but expected is 20.0 * 10 = 200
答案 0 :(得分:1)
我前段时间也有同样的问题。我做了很多搜索,但是没有找到解决方案。问题在于传递的fps是视频播放的速率。这并不意味着该视频将以该FPS 记录。 AFAIK,没有直接的方法来设置记录的FPS。如果您记录的FPS太高,则可以降低采样率(即,每个时间段仅保留1帧)。但是从您的描述来看,它似乎远低于要求。这是硬件限制,对此无能为力。
关于设置录制的FPS,我找到了一种解决方法。在捕获列表中的所有所有帧之后,我创建了videoWriter。这样,我可以计算记录的FPS,并在创建它时将其传递给VideoWriter。
答案 1 :(得分:1)
如果内存不足,则无法制作帧列表。另一种方法是动态计算fps,然后remux the video with the new fps using ffmpeg
。
import numpy as np
from skvideo import io
import cv2, sys
import time
import os
if __name__ == '__main__':
file_name = 'video_clip.avi'
fourcc = cv2.VideoWriter_fourcc(*'XVID')
videoOut = cv2.VideoWriter(file_name, fourcc, 30.0, (640, 480))
cap = cv2.VideoCapture(0)
if not cap.isOpened() or not videoOut.isOpened():
exit(-1)
start_time = time.time()
frame_count = 0
duration = 0
while True:
ret, frame = cap.read()
if not ret:
print('empty frame')
break
videoOut.write(frame) # write each frame to make video clip
frame_count += 1
duration = time.time()-start_time
if int(duration) == 10:
videoOut.release()
cap.release()
break
actualFps = np.ceil(frame_count/duration)
os.system('ffmpeg -y -i {} -c copy -f h264 tmp.h264'.format(file_name))
os.system('ffmpeg -y -r {} -i tmp.h264 -c copy {}'.format(actualFps,file_name))