以指定的时间间隔录制视频,然后将它们保存到文件 OpenCv Python
Recording video in specified time intervals and then saving them into file OpenCv Python
这是我的目标。
- 连续拍摄视频直到'q;被按下
- 每十秒将视频保存在创建的目录文件中
- 继续第二步,直到按下 'q'
我正在执行以下代码。但是在创建文件时,它正在创建 6kb 文件并说无法播放。我对 opencv 和 python 还很陌生。不知道我错过了什么。 运行 pycharm 此代码与 Python 3.6。还有
cv2.imshow('frame',帧)
十秒后停止,但正在后台录制并创建文件。
import numpy as np
import cv2
import time
import os
import random
import sys
fps=24
width=864
height=640
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
name = random.randint(0,1000)
print (name)
if (os.path.isdir(str(name)) is False):
name = random.randint(0,1000)
name=str(name)
name = os.path.join(os.getcwd(), str(name))
print('ALl logs saved in dir:', name)
os.mkdir(name)
cap = cv2.VideoCapture(0)
ret=cap.set(3, 864)
ret=cap.set(4, 480)
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
start=time.time()
video_file_count = 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
print('Capture video saved location : {}'.format(video_file))
while(cap.isOpened()):
start_time = time.time()
ret, frame = cap.read()
if ret==True:
cv2.imshow('frame',frame)
if (time.time() - start > 10):
start = time.time()
video_file_count += 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
video_writer = cv2.VideoWriter(video_file,video_codec, fps,(int(cap.get(3)),int(cap.get(4))))
time.sleep(10)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
我想要包含录制视频的文件。已生成文件,但大小为 6kb,并且未记录任何内容。
你快完成了!鉴于我了解您的目标是什么,并且只需对您的代码进行最少的更改,这就是对我有用的方法。
每十秒写入一个新的视频文件,同时将每一帧记录到当前视频中。
import numpy as np
import cv2
import time
import os
import random
import sys
fps = 24
width = 864
height = 640
video_codec = cv2.VideoWriter_fourcc("D", "I", "V", "X")
name = random.randint(0, 1000)
print(name)
if os.path.isdir(str(name)) is False:
name = random.randint(0, 1000)
name = str(name)
name = os.path.join(os.getcwd(), str(name))
print("ALl logs saved in dir:", name)
os.mkdir(name)
cap = cv2.VideoCapture(0)
ret = cap.set(3, 864)
ret = cap.set(4, 480)
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
start = time.time()
video_file_count = 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
print("Capture video saved location : {}".format(video_file))
# 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)))
)
while cap.isOpened():
start_time = time.time()
ret, frame = cap.read()
if ret == True:
cv2.imshow("frame", frame)
if time.time() - start > 10:
start = time.time()
video_file_count += 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
)
# No sleeping! We don't want to sleep, we want to write
# time.sleep(10)
# Write the frame to the current video writer
video_writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
一个迹象表明正在以 6 kb 的速度接收视频,编解码器出错。您需要下载 opencv_ffmpeg.dll 并将其放在 Python3.2.1 文件夹中并重命名为 opencv_ffmpeg321.dll
这解决了我的问题,在此之前,无论我做什么,都创建了 5.6 kb 的视频。但问题比看起来更严重,它仍然可能与流和录制的分辨率不匹配有关。
对于 OpenCV 版本 X.Y.Z
opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll
对于 64 位版本的 OpenCV X.Y.Z
opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll
这是我的目标。
- 连续拍摄视频直到'q;被按下
- 每十秒将视频保存在创建的目录文件中
- 继续第二步,直到按下 'q'
我正在执行以下代码。但是在创建文件时,它正在创建 6kb 文件并说无法播放。我对 opencv 和 python 还很陌生。不知道我错过了什么。 运行 pycharm 此代码与 Python 3.6。还有
cv2.imshow('frame',帧)
十秒后停止,但正在后台录制并创建文件。
import numpy as np
import cv2
import time
import os
import random
import sys
fps=24
width=864
height=640
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
name = random.randint(0,1000)
print (name)
if (os.path.isdir(str(name)) is False):
name = random.randint(0,1000)
name=str(name)
name = os.path.join(os.getcwd(), str(name))
print('ALl logs saved in dir:', name)
os.mkdir(name)
cap = cv2.VideoCapture(0)
ret=cap.set(3, 864)
ret=cap.set(4, 480)
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
start=time.time()
video_file_count = 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
print('Capture video saved location : {}'.format(video_file))
while(cap.isOpened()):
start_time = time.time()
ret, frame = cap.read()
if ret==True:
cv2.imshow('frame',frame)
if (time.time() - start > 10):
start = time.time()
video_file_count += 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
video_writer = cv2.VideoWriter(video_file,video_codec, fps,(int(cap.get(3)),int(cap.get(4))))
time.sleep(10)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
我想要包含录制视频的文件。已生成文件,但大小为 6kb,并且未记录任何内容。
你快完成了!鉴于我了解您的目标是什么,并且只需对您的代码进行最少的更改,这就是对我有用的方法。
每十秒写入一个新的视频文件,同时将每一帧记录到当前视频中。
import numpy as np
import cv2
import time
import os
import random
import sys
fps = 24
width = 864
height = 640
video_codec = cv2.VideoWriter_fourcc("D", "I", "V", "X")
name = random.randint(0, 1000)
print(name)
if os.path.isdir(str(name)) is False:
name = random.randint(0, 1000)
name = str(name)
name = os.path.join(os.getcwd(), str(name))
print("ALl logs saved in dir:", name)
os.mkdir(name)
cap = cv2.VideoCapture(0)
ret = cap.set(3, 864)
ret = cap.set(4, 480)
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
start = time.time()
video_file_count = 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
print("Capture video saved location : {}".format(video_file))
# 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)))
)
while cap.isOpened():
start_time = time.time()
ret, frame = cap.read()
if ret == True:
cv2.imshow("frame", frame)
if time.time() - start > 10:
start = time.time()
video_file_count += 1
video_file = os.path.join(name, str(video_file_count) + ".avi")
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4)))
)
# No sleeping! We don't want to sleep, we want to write
# time.sleep(10)
# Write the frame to the current video writer
video_writer.write(frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
一个迹象表明正在以 6 kb 的速度接收视频,编解码器出错。您需要下载 opencv_ffmpeg.dll 并将其放在 Python3.2.1 文件夹中并重命名为 opencv_ffmpeg321.dll
这解决了我的问题,在此之前,无论我做什么,都创建了 5.6 kb 的视频。但问题比看起来更严重,它仍然可能与流和录制的分辨率不匹配有关。
对于 OpenCV 版本 X.Y.Z opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll
对于 64 位版本的 OpenCV X.Y.Z opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll