使用FFmpeg将视频帧转换为png图像

使用FFmpeg将视频帧转换为png图像


前言

一个使用ffmpeg多进程将大量mp4文件转换为对应多帧png/jpg/bmp等图片文件的代码实例,其中png存储于与视频同名的文件夹当中。


一、核心命令

ffmpeg -i *.mp4 -s 256x256 -r 25 %8d.png

其中 -i 指 输入地址
-s 指定图片分辨率
-r 指定帧数

二、代码实例

核心思想使用 multiprocessing 读取 MP4 文件列表,更改命名方式进行存储

import os
import argparse
from typing import List, Dict
from multiprocessing import Pool
import subprocess
from subprocess import Popen, PIPE
from glob import glob
from tqdm import tqdm, trange


def clip_vid2png(source_dir: os.PathLike, output_dir: os.PathLike, num_workers: int):
    os.makedirs(output_dir, exist_ok=True)
    # os.makedirs(os.path.join(output_dir, '_videos_raw'), exist_ok=True)
    import pdb;pdb.set_trace()
    video_queue = construct_video_queue(source_dir)
    task_kwargs = [dict(
        video_path=vd,  # 视频路径
        png_path= os.path.join(output_dir, os.path.basename(vd))# 图像存储路径
     ) for vd in video_queue]
    pool = Pool(processes=num_workers)
    tqdm_kwargs = dict(total=len(task_kwargs), desc=f'Clips videos into {
      
      output_dir}')

    for _ in tqdm(pool.imap_unordered(task_proxy_clip, task_kwargs), **tqdm_kwargs):
        pass

    print('Clip is finished')


def construct_video_queue(source_dir: os.PathLike) -> List[Dict]:
    video_queue = sorted(glob(os.path.join(source_dir, "*.mp4")))
    return video_queue

def task_proxy_clip(kwargs):
    return clip_video(**kwargs)

def clip_video(video_path, png_path):
    os.makedirs(png_path, exist_ok=True)
    command = [
            "ffmpeg",
            "-loglevel", "quiet", 
            "-i",
            f"{
      
      video_path}", 
            "-s", "256x256",
            "-r", "25",
            f"{
      
      png_path}/%08d.png"
        ]
    return_code = subprocess.call(command)
    return return_code


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="clip video to png")
    parser.add_argument('-s', '--source_dir', type=str, default=None, help='Path to the directory with the dataset')
    parser.add_argument('-o', '--output_dir', type=str, default=None, help='Where to save the videos?')
    parser.add_argument('-w', '--num_workers', type=int, default=4, help='Number of workers for downloading')
    args = parser.parse_args()

    clip_vid2png(
        args.source_dir,
        args.output_dir,
        args.num_workers,
    )

猜你喜欢

转载自blog.csdn.net/weixin_43357695/article/details/129252595