当前位置:   article > 正文

Python调用ffmpeg对视频抽帧_python ffmpeg视频抽帧

python ffmpeg视频抽帧

1.安装ffmpfg

sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next 
sudo apt-get update 
sudo apt-get install ffmpeg
  • 1
  • 2
  • 3

2.抽取视频帧

#!/usr/bin/python
# -*- coding: utf-8 -*-  
import PIL.Image as Image
import pylab
import skimage
import numpy as np
import os
from subprocess import call
from tqdm import tqdm
import pandas as pd
import re
#视频的绝对路径和图片存储的目标路径
def extract_frames(src_path,target_path):

    new_path = target_path

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

#生成对应视频帧的标签列表
def generate_list(frame_path):
    result = open("train.list",'w')
    for root_dir, dirs, files in os.walk(frame_path):
        print('root_dir:', root_dir)  # 当前目录路径
        print('sub_dirs:', dirs)  # 当前路径下所有子目录
        print('files:', files)  # 当前路径下所有非目录子文件
        r = r"clip(.*)_"
        re_frame = re.compile(r)
        for frame_name in files:
            frame_path = root_dir+'/'+frame_name
            label = re.findall(re_frame,root_dir)[0] #对应帧的label
            result.write(frame_path+' '+str(label)+'\n')

if __name__ == '__main__':
    extract_frames(src_path='./data/',target_path='./data_pics/')
    generate_list('./data_pics/')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/742644?site
推荐阅读
相关标签
  

闽ICP备14008679号