FFmpeg+python实现批量视频分帧

使用FFmpeg工具对短视频批量分帧,以下是一段很简单的代码:

import PIL.Image as Image
import pylab
import imageio
#注释的代码执行一次就好,以后都会默认下载完成
#imageio.plugins.ffmpeg.download()  #第一次运行是删除注释,下载ffmpeg工具
import skimage
import numpy as np
import os
from subprocess import call
from tqdm import tqdm
import pandas as pd
#视频的绝对路径和图片存储的目标路径
def extract_frames(src_path,target_path):

    new_path = target_path

    for video_name in tqdm(os.listdir(src_path)):
        #video_name = "ZJL35.mp4"
        filename = src_path + video_name
        cur_new_path = new_path+video_name.split('.')[0]+'/'
        if not os.path.exists(cur_new_path):
            os.mkdir(cur_new_path)
        dest = cur_new_path + video_name.split('.')[0]+'-%04d.jpg'
        call(["ffmpeg", "-i", filename,"-r","5", dest]) #这里的5为5fps,帧率可修改

运行

extract_frames(src_path='./Charades_test_video/',target_path='./Charades_test_video_pics/')

猜你喜欢

转载自blog.csdn.net/qq_33373858/article/details/83690331
今日推荐