2025-06-14【视觉】视频转化为图集

很简单的,很容易的代码,

1.以时间命名图片

2.多线程处理

3.tqdm进度条可视化

复制代码
import cv2
import os
from pathlib import Path
import concurrent.futures
from tqdm import tqdm


def video_to_images(video_path, output_dir='frames', frame_interval=1, workers=4, use_time_name=True):
    """
    使用多线程和tqdm进度条将视频转换为图片序列

    参数:
    video_path (str): 输入视频的路径
    output_dir (str): 输出图片的目录
    frame_interval (int): 每隔多少帧提取一帧
    workers (int): 线程池大小
    use_time_name (bool): 是否使用时间作为文件名
    """
    # 确保输出目录存在
    Path(output_dir).mkdir(parents=True, exist_ok=True)

    # 打开视频获取基本信息
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print(f"错误:无法打开视频文件 {video_path}")
        return

    fps = cap.get(cv2.CAP_PROP_FPS)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = total_frames / fps  # 视频总时长(秒)
    cap.release()

    # 计算需要处理的帧索引
    frame_indices = list(range(0, total_frames, frame_interval))
    print(f"视频信息: {fps:.2f} FPS, 共 {total_frames} 帧, 时长 {duration:.2f} 秒, 将提取 {len(frame_indices)} 帧")

    def frame_to_timecode(frame_idx, fps):
        """将帧索引转换为时分秒格式"""
        total_seconds = frame_idx / fps
        hours = int(total_seconds // 3600)
        minutes = int((total_seconds % 3600) // 60)
        seconds = total_seconds % 60
        return hours, minutes, seconds

    def process_frame(index):
        cap = cv2.VideoCapture(video_path)
        cap.set(cv2.CAP_PROP_POS_FRAMES, index)
        ret, frame = cap.read()
        cap.release()

        if ret:
            if use_time_name:
                # 生成时间格式文件名: time_时_分_秒_毫秒.jpg
                h, m, s = frame_to_timecode(index, fps)
                ms = int((s - int(s)) * 1000)  # 毫秒部分
                output_path = os.path.join(output_dir, f"time_{h:02d}_{m:02d}_{int(s):02d}_{ms:03d}.jpg")
            else:
                # 原始的帧索引命名
                output_path = os.path.join(output_dir, f"frame_{index:06d}.jpg")

            cv2.imwrite(output_path, frame)
            return True
        return False

    # 使用线程池并行处理帧,并添加tqdm进度条
    with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor:
        results = list(tqdm(executor.map(process_frame, frame_indices),
                            total=len(frame_indices),
                            desc="处理进度",
                            unit="帧"))

    success_count = sum(results)
    print(f"视频转图片完成!成功生成 {success_count} 张图片,保存在 {output_dir} 目录下")


if __name__ == "__main__":
    # 使用示例 - 按时间命名
    video_file = "2.mp4"  # 替换为你的视频文件路径
    video_to_images(
        video_file,
        output_dir="output_frames",
        frame_interval=25,  # 每秒提取一帧(假设视频为25fps)
        workers=4,  # 根据CPU核心数调整
        use_time_name=True  # 使用时间命名
    )
    #时间格式:time_时_分_秒_毫秒.jpg(例如:time_00_01_53_000.jpg表示 1 分 53 秒整)
相关推荐
百锦再1 天前
第11章 泛型、trait与生命周期
android·网络·人工智能·python·golang·rust·go
zbhbbedp282793cl1 天前
如何在VSCode中安装Python扩展?
ide·vscode·python
Python私教1 天前
Python 开发环境安装与配置全指南(2025版)
开发语言·python
百锦再1 天前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang
熠熠仔1 天前
QGIS 3.34+ 网络分析基础数据自动化生成:从脚本到应用
python·数据分析
测试19981 天前
Appium使用指南与自动化测试案例详解
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
神仙别闹1 天前
基于 C++和 Python 实现计算机视觉
c++·python·计算机视觉
hongjianMa1 天前
【论文阅读】Hypercomplex Prompt-aware Multimodal Recommendation
论文阅读·python·深度学习·机器学习·prompt·推荐系统
饼干,1 天前
第23天python内容
开发语言·python
酷柚易汛智推官1 天前
基于librespot的定制化Spotify客户端开发:开源替代方案的技术实践与优化
python·开源·酷柚易汛