python获取视频文件夹下每个视频的总帧数输出,并输出所有视频最后的帧求和
时间: 2023-08-12 15:11:39 浏览: 86
你可以使用OpenCV库读取视频文件并计算总帧数。以下是代码示例:
```python
import cv2
import os
videos_folder = "path/to/videos/folder"
total_frames = 0
last_frame_sum = 0
# Loop through each video in the folder
for filename in os.listdir(videos_folder):
if filename.endswith(".mp4") or filename.endswith(".avi"): # Check if it's a video file
video_path = os.path.join(videos_folder, filename)
cap = cv2.VideoCapture(video_path)
total_frames += int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # Get total frames and add to sum
while cap.isOpened():
ret, frame = cap.read()
if not ret:
last_frame_sum += cap.get(cv2.CAP_PROP_POS_FRAMES) - 1 # Get last frame and add to sum
break
cap.release()
print("Total frames:", total_frames)
print("Last frame sum:", last_frame_sum)
```
请注意,此代码仅适用于MP4和AVI格式的视频文件。如果你的视频文件格式不同,你需要相应地更改代码。
阅读全文