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 秒整)
相关推荐
onceco29 分钟前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄1 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊2 小时前
Python之--基本知识
开发语言·前端·python
笑稀了的野生俊4 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva4 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm
路来了4 小时前
Python小工具之PDF合并
开发语言·windows·python
蓝婷儿5 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
AntBlack5 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python
.30-06Springfield5 小时前
决策树(Decision tree)算法详解(ID3、C4.5、CART)
人工智能·python·算法·决策树·机器学习
我不是哆啦A梦5 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt