我正在编写一个脚本,该脚本循环遍历一个包含多个 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.")```
我已经测试了你的代码,它似乎工作正常。
Processing video: C:/Users/david/Downloads/videos\video1.mp4
Can't receive frame from video1.mp4. Moving to the next video...
Processing video: C:/Users/david/Downloads/videos\video2.mp4
Can't receive frame from video2.mp4. Moving to the next video...
Processing video: C:/Users/david/Downloads/videos\video3.mp4
Can't receive frame from video3.mp4. Moving to the next video...
Finished displaying all videos.
您到底得到了什么错误?如果只是
Can't receive frame from video3.mp4. Moving to the next video...
,你确定这不仅仅是视频结束了吗?