当前位置: 首页 > news >正文

从视频截取每一帧作为图像

查看视频有多少帧

import cv2def count_frames_per_second(video_path):cap = cv2.VideoCapture(video_path)if not cap.isOpened():print("Error: Could not open video")return None# Get frames per secondfps = cap.get(cv2.CAP_PROP_FPS)# Get total number of framestotal_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))# Calculate total time in secondstotal_time_seconds = total_frames / fps if fps > 0 else 0# Convert total time to minutes and seconds for better readabilitytotal_minutes = total_time_seconds // 60total_seconds = total_time_seconds % 60print(f"Frames per second: {fps}")print(f"Total frames in the video: {total_frames}")print(f"Total time of the video: {int(total_minutes)} minutes and {total_seconds:.2f} seconds")cap.release()return fps, total_frames, total_time_seconds# Example usage
video_path = "D:\\WorkSpace\\pitaya_video\\video\\VID_20241015_082527.mp4"  # Change this to your video file path
count_frames_per_second(video_path)

单个视频

import cv2
import osdef capture_frames(video_path,save_frame_path):# Get the video name without extensionvideo_name,suffix_mp4 = os.path.splitext(os.path.basename(video_path))    # Create a directory to save framesframes_dir = os.path.join(save_frame_path, video_name)os.makedirs(frames_dir, exist_ok=True)# Open the video filecap = cv2.VideoCapture(video_path)if not cap.isOpened():print(f"Error: Could not open video file {video_path}")returnframe_count = 0while True:# Read a frame from the videoret, frame = cap.read()# Break the loop if there are no more framesif not ret:break# Save the frame as an image fileframe_filename = os.path.join(frames_dir, f"{video_name}_frame_{frame_count:04d}.jpg")cv2.imwrite(frame_filename, frame)print(f"Saved {frame_filename}")frame_count += 1# Release the video capture objectcap.release()print(f"Total frames saved: {frame_count}")# Example usage
video_file_path = "D:\\WorkSpace\\pitaya_video\\video"  # Replace with your video file path
video_file_path = os.path.join(video_file_path,"VID_20241015_082527.mp4")
save_frame_path = "D:\\WorkSpace\\pitaya_video\\all_image"
capture_frames(video_file_path,save_frame_path)

多个视频

import cv2
import osdef capture_frames_from_videos(video_directory,save_frame_path):# List all video files in the specified directoryvideo_files = [f for f in os.listdir(video_directory) if f.endswith('.mp4')]for video_file in video_files:video_path = os.path.join(video_directory, video_file)print(f"Processing video: {video_path}")# Get the video name without extensionvideo_name, suffix_mp4= os.path.splitext(video_file)# Create a directory to save framesframes_dir = os.path.join(save_frame_path, video_name)os.makedirs(frames_dir, exist_ok=True)# Open the video filecap = cv2.VideoCapture(video_path)if not cap.isOpened():print(f"Error: Could not open video file {video_path}")continueframe_count = 0while True:# Read a frame from the videoret, frame = cap.read()# Break the loop if there are no more framesif not ret:break# Save the frame as an image fileframe_filename = os.path.join(frames_dir, f"{video_name}_frame_{frame_count:04d}.jpg")cv2.imwrite(frame_filename, frame)print(f"Saved {frame_filename}")frame_count += 1# Release the video capture objectcap.release()print(f"Total frames saved for {video_file}: {frame_count}")# Example usage
video_directory = "D:\\WorkSpace\\pitaya_video\\video"  # Replace with your video directory path
save_frame_path = "D:\\WorkSpace\\pitaya_video\\all_image"
capture_frames_from_videos(video_directory,save_frame_path)

一秒截取3帧

import cv2
import osdef capture_frames_from_videos(video_directory, save_frame_path):# List all video files in the specified directoryvideo_files = [f for f in os.listdir(video_directory) if f.endswith('.mp4')]for video_file in video_files:video_path = os.path.join(video_directory, video_file)print(f"Processing video: {video_path}")# Get the video name without extensionvideo_name, _ = os.path.splitext(video_file)# Create a directory to save framesframes_dir = os.path.join(save_frame_path, video_name)os.makedirs(frames_dir, exist_ok=True)# Open the video filecap = cv2.VideoCapture(video_path)if not cap.isOpened():print(f"Error: Could not open video file {video_path}")continue# Get the frames per second (fps) of the videofps = cap.get(cv2.CAP_PROP_FPS)print(f"Frames per second: {fps}")# Calculate the interval to capture 3 frames per secondframe_interval = max(int(fps / 3), 1)  # Ensure at least one frame intervalframe_count = 0saved_frame_count = 0while True:# Read a frame from the videoret, frame = cap.read()# Break the loop if there are no more framesif not ret:break# Save the frame at specific intervalsif frame_count % frame_interval == 0:frame_filename = os.path.join(frames_dir, f"{video_name}_frame_{saved_frame_count:04d}.jpg")cv2.imwrite(frame_filename, frame)print(f"Saved {frame_filename}")saved_frame_count += 1frame_count += 1# Release the video capture objectcap.release()print(f"Total frames saved for {video_file}: {saved_frame_count}")# Example usage
video_directory = "D:\\WorkSpace\\pitaya_video\\video"  # Replace with your video directory path
save_frame_path = "D:\\WorkSpace\\pitaya_video\\all_image"  # Replace with your desired output path
capture_frames_from_videos(video_directory, save_frame_path)

http://www.lryc.cn/news/462266.html

相关文章:

  • 终端 数据表格
  • 2.4.ReactOS系统运行级别降低IRQL级别KfLowerIrql 函数
  • 数字后端实现静态时序分析STA Timing Signoff之min period violation
  • phpstorm+phpstudy 配置xdebug(无需开启浏览器扩展)
  • AI赋能安全运营 | 赛宁网安深度参与四川省网络安全沙龙
  • R语言中,.RData 和 .rds 的区别
  • python实现录屏功能
  • 酷克数据出席2024金融业数据库技术大会
  • find_library、pkg_check_modules、pkg_search_module的区别
  • 多jdk版本环境下,jenkins系统设置需指定JAVA_HOME环境变量
  • Java mybatis day1015
  • 音乐播放器项目专栏介绍​
  • 如何修改SpringBoot内置容器默认上下文
  • R语言详解predict函数
  • QT 实现随机码验证
  • 集合框架12:Set集合概述、Set接口使用
  • 如何打开荣耀手机的调试模式?
  • Meta新模型Dualformer:融合快慢思维,推理能力媲美人脑
  • CDGA|数据治理:如何让传统行业实现数据智能
  • Spring源码5.2.9 编译踩坑
  • 【前端】如何制作一个自己的网页(5)
  • Unity实战案例全解析 类宝可梦回合制的初级案例 源码分析(加了注释和流程图)
  • AI绘图大模型 Stable Diffusion 使用详解
  • es索引库操作和使用RestHignLevelClient客户端操作es
  • 安卓数据共享
  • Gin框架操作指南02:JSON渲染
  • 【随手记】MySQL单表访问方法
  • 机器学习:情感分析的原理、应用场景及优缺点介绍
  • 基于SSM的医院药品管理系统
  • 特征融合篇 | YOLOv10 引入动态上采样模块 | 超过了其他上采样器