Windows 11 批量每个视频截取20张图全视频格式支持

使用此操作方式前需要先安装 Python 程序,安装教程请阅读 Windows 11 应用商店安装 Python

同时需要安装 FFmpeg 程序,安装教程请阅读 Windows 11 安装 FFmpeg 最新版

新建一个txt文本,将下方的代码复制进去后保存。并将文件名和后缀修改为 script.py

import subprocess
import os

def is_video_file(file_path):
    """检查文件是否是视频文件"""
    try:
        ffprobe_cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=format_name', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
        output = subprocess.check_output(ffprobe_cmd).decode().strip()
        return bool(output)
    except subprocess.CalledProcessError:
        return False

def extract_frames(video_path, output_dir, num_frames):
    # 创建输出目录
    os.makedirs(output_dir, exist_ok=True)
    
    # 使用 FFmpeg 获取视频时长
    ffprobe_cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_path]
    duration = float(subprocess.check_output(ffprobe_cmd).decode().strip())

    # 计算时间间隔
    interval = duration / (num_frames + 1)

    # 提取帧
    for i in range(1, num_frames + 1):
        time = i * interval
        minutes = int(time // 60)
        seconds = int(time % 60)
        output_file = os.path.join(output_dir, f'{os.path.splitext(os.path.basename(video_path))[0]}_time_{minutes:02d}m{seconds:02d}s.jpg')
        ffmpeg_cmd = ['ffmpeg', '-ss', str(time), '-i', video_path, '-vframes', '1', '-q:v', '2', output_file]
        subprocess.run(ffmpeg_cmd)

if __name__ == "__main__":
    # 获取当前文件夹下的所有文件
    current_dir = os.getcwd()
    files = [f for f in os.listdir(current_dir) if os.path.isfile(f)]

    num_frames = 20  # 需要截取的帧数

    # 对每个视频文件进行截图
    for file in files:
        file_path = os.path.join(current_dir, file)
        if is_video_file(file_path):
            output_dir = os.path.splitext(file)[0] + '_frames'
            extract_frames(file_path, output_dir, num_frames)

将视频,程序放在同一目录下

在需要添截图的视频文件夹路径输入 CMD 后回车用于打开 CMD

在执行窗口中输入 python 并将 script.py 拖入执行窗口, 和如下命令行相同即可

python "C:\Users\losto\OneDrive\桌面\New folder\script.py"

回车后即可开始运行程序

文件夹内会多出一个文件夹,图片就在里面

资源下载
下载地址1立即下载
常见问题
123盘资源下载
本站提供 123云盘 资源链接
可无登入直接下载
安全声明
如文章内提供下载内容
此内容可能为执行脚本,软件,图像或Ai模型
所有内容均经过病毒查杀,可放心下载
免责声明
因模型可能包含 NSFW 内容,请不要将模型用于非法用途
本站点只提供模型下载,不参与制作者图片生成
因制作者生成图片造成的违法问题与本站无关
0

评论0

没有账号?注册  忘记密码?