python 调用ffmpeg 命令行对批量视频进行图片分解并保存到对应文件夹

Posted 一夢浮生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 调用ffmpeg 命令行对批量视频进行图片分解并保存到对应文件夹相关的知识,希望对你有一定的参考价值。

import subprocess
import os


# ffmpeg -i /data/video_1.mp4 -f image2  -vf fps=fps=1/60 -qscale:v 2 /data/mp4-%05d.jpg

def videoFileName(videoDir, framesDir, ffmpegCmd):
    videoFiles = os.listdir(videoDir)
    for video in videoFiles:
        sinVideoDir = os.path.join(videoDir, video)
        if (sinVideoDir.endswith(".avi")):
            videoName = os.path.basename(sinVideoDir)
            videoBaseName = videoName.rsplit('.')
            curVideoFrameOut = framesDir + '\\\\'+ "%04d.jpg"

            video2framesCmd = ffmpegCmd + " -i " + sinVideoDir + " -f image2 -vf fps=fps=30 -qscale:v 2 " + curVideoFrameOut
            subprocess.call(video2framesCmd, shell=True)


if __name__ == "__main__":
    ffmpegCmd = "D:\\\\Anaconda3\\\\envs\\\\pytorch1.10\\\\Library\\\\bin\\\\ffmpeg.exe"  # ffmpeg 路径
    # frameDir = "E://ImageTest//videoTest//"
    # videoDir = "E://downloads//drumsVideos//"
    # videoFileName(videoDir, frameDir, ffmpegCmd)
    frame_baseDir = "E:\\\\pythonfile1\\\\rppg_tdm_talos1\\\\data\\\\UBFC_FACE"  # 存放图像序列路径
    video_baseDir = "D:\\\\data\\\\UBFC"  # 存放视频路径
    subjects = os.listdir(video_baseDir)
    subjects.sort()
    for i, subject in enumerate(subjects):
        print(subject)
        frameDir=os.path.join(frame_baseDir,subject)
        if not os.path.exists(frameDir):
            os.makedirs(frameDir)
        videoDir=os.path.join(video_baseDir,subject)
        videoFileName(videoDir, frameDir, ffmpegCmd)

 

注意:保存路径 framesDir 后要加'\\\\'与文件名分隔开

python调用ffmpeg批量转换音频flac、mp3、m4a

参考技术A 查看使用说明: ffmpeg -h 。

转换命令如下:

这次我们用到的主要是 -ab 命令,用来指定音频转换的比特率,常见的有: 192K, 320K 等。

以上是关于python 调用ffmpeg 命令行对批量视频进行图片分解并保存到对应文件夹的主要内容,如果未能解决你的问题,请参考以下文章

linux 上java调用ffmpeg转码只有几秒长

命令行批量合并视频脚本

ffmpeg如何批量切割视频尾部4秒?

如何使用 bash 命令或 Ruby 使用 ffmpeg 将 mp4 文件批量转换为 ogg

pyinstaller打包PySide2写的GUI程序,调用ffmpeg隐藏CMD控制台解决方案

使用ffmpeg批量合并flv文件