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

查看视频有多少帧

python 复制代码
import cv2

def 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 second
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Get total number of frames
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    # Calculate total time in seconds
    total_time_seconds = total_frames / fps if fps > 0 else 0

    # Convert total time to minutes and seconds for better readability
    total_minutes = total_time_seconds // 60
    total_seconds = total_time_seconds % 60

    print(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)

单个视频

python 复制代码
import cv2
import os

def capture_frames(video_path,save_frame_path):
    # Get the video name without extension
    video_name,suffix_mp4 = os.path.splitext(os.path.basename(video_path))    
    # Create a directory to save frames
    frames_dir = os.path.join(save_frame_path, video_name)
    os.makedirs(frames_dir, exist_ok=True)

    # Open the video file
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        print(f"Error: Could not open video file {video_path}")
        return

    frame_count = 0

    while True:
        # Read a frame from the video
        ret, frame = cap.read()
        
        # Break the loop if there are no more frames
        if not ret:
            break

        # Save the frame as an image file
        frame_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 object
    cap.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)

多个视频

python 复制代码
import cv2
import os

def capture_frames_from_videos(video_directory,save_frame_path):
    # List all video files in the specified directory
    video_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 extension
        video_name, suffix_mp4= os.path.splitext(video_file)
        
        # Create a directory to save frames
        frames_dir = os.path.join(save_frame_path, video_name)
        os.makedirs(frames_dir, exist_ok=True)

        # Open the video file
        cap = cv2.VideoCapture(video_path)

        if not cap.isOpened():
            print(f"Error: Could not open video file {video_path}")
            continue

        frame_count = 0

        while True:
            # Read a frame from the video
            ret, frame = cap.read()
            
            # Break the loop if there are no more frames
            if not ret:
                break

            # Save the frame as an image file
            frame_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 object
        cap.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帧

python 复制代码
import cv2
import os

def capture_frames_from_videos(video_directory, save_frame_path):
    # List all video files in the specified directory
    video_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 extension
        video_name, _ = os.path.splitext(video_file)
        
        # Create a directory to save frames
        frames_dir = os.path.join(save_frame_path, video_name)
        os.makedirs(frames_dir, exist_ok=True)

        # Open the video file
        cap = 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 video
        fps = cap.get(cv2.CAP_PROP_FPS)
        print(f"Frames per second: {fps}")

        # Calculate the interval to capture 3 frames per second
        frame_interval = max(int(fps / 3), 1)  # Ensure at least one frame interval

        frame_count = 0
        saved_frame_count = 0

        while True:
            # Read a frame from the video
            ret, frame = cap.read()
            
            # Break the loop if there are no more frames
            if not ret:
                break

            # Save the frame at specific intervals
            if 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 += 1
            
            frame_count += 1

        # Release the video capture object
        cap.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)
相关推荐
xcLeigh1 小时前
HTML5超酷响应式视频背景动画特效(六种风格,附源码)
前端·音视频·html5
韩曙亮2 小时前
【FFmpeg】FFmpeg 内存结构 ③ ( AVPacket 函数简介 | av_packet_ref 函数 | av_packet_clone 函数 )
ffmpeg·音视频·avpacket·av_packet_clone·av_packet_ref·ffmpeg内存结构
9527华安6 小时前
FPGA实现PCIE3.0视频采集转10G万兆UDP网络输出,基于XDMA+GTH架构,提供工程源码和技术支持
网络·fpga开发·udp·音视频·xdma·pcie3.0·万兆网
电子科技圈7 小时前
XMOS携手合作伙伴晓龙国际联合推出集成了ASRC等功能的多通道音频板
科技·嵌入式硬件·mcu·物联网·音视频·iot
码码哈哈0.07 小时前
免费的视频混剪综合处理工具介绍与下载
音视频
莫固执,朋友7 小时前
网络抓包工具tcpdump 在海思平台上的编译使用
网络·ffmpeg·音视频·tcpdump
深海呐7 小时前
Android 从本地选择视频,用APP播放或进行其他处理
android·音视频·从本地选择视频,用app播放·从本地选择视频,并拿到信息·跳转到本地视频列表
cuijiecheng20188 小时前
音视频入门基础:MPEG2-TS专题(6)——FFmpeg源码中,获取MPEG2-TS传输流每个transport packet长度的实现
ffmpeg·音视频
安静读书11 小时前
Java解析视频FPS(帧率)、分辨率信息
java·python·音视频
VisionX Lab13 小时前
数据脱敏工具:基于 FFmpeg 的视频批量裁剪
python·ffmpeg·音视频