提问人:Urooba Shahid 提问时间:8/14/2023 最后编辑:RF1991Urooba Shahid 更新时间:8/14/2023 访问量:27
如何使用 python 和 opencv 一个接一个地运行目录中的完整视频,而不会跳过帧
how do I run a full video from a directory one after another video without skipping frames with python and opencv
问:
我正在编写一个脚本,该脚本遍历一个包含多个 2mb 视频的目录,并读取它们并显示它们。问题是它确实读取了它们但跳过了帧,对于某些视频,它只是跳过了它们,从而产生了无法读取帧的错误。当我尝试运行单个视频时,它们工作正常,所以唯一的问题是我是否遍历它们
我正在尝试最终构建一个脚本,该脚本可以拍摄视频并将它们作为发布请求发送到可以将它们保存在 mongodb 数据库中的服务器,但由于我是新手,我只是尝试一次构建一个。如果你们能提出任何很棒的建议
import os
import cv2 as cv
# import time
def get_video_duration(video_path):
cap = cv.VideoCapture(video_path)
fps = cap.get(cv.CAP_PROP_FPS)
frame_count = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
duration = frame_count / fps
cap.release()
return duration
folder_path = '/Users/api/ramses/video3/' # Specify the folder containing video files
video_files = [f for f in os.listdir(folder_path) if f.endswith('.mp4')]
for video_file in video_files:
video_path = os.path.join(folder_path, video_file)
print("Processing video:", video_path)
cap = cv.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
# If frame is read correctly, ret is True
if not ret:
print(f"Can't receive frame from {video_file}. Moving to the next video...")
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# time.sleep(17)
cap.release()
cv.destroyAllWindows()
print("Finished displaying all videos.")```
答: 暂无答案
评论