python批处理MP4转换为yuv

2020-10-31  本文已影响0人  zht1999

批处理MP4转换为yuv,并保存width 和 height信息为mat文件

import os
import cv2
from os import path
import numpy as np
import scipy.io as scio
from ffmpy3 import FFmpeg
from subprocess import PIPE

Path_orig = 'G:/CRSR/test_seq/'
Path_train = 'G:/CRSR/train_yuv/'
Path_validation = 'G:/CRSR/validation_yuv/'

wdr = path.normpath(Path_orig)
subwdr = os.listdir(wdr)

widthList_train=[]
heightList_train=[]
widthList_validation=[]
heightList_validation=[]

count = 0 
for videoClass in subwdr:
    videoList = os.listdir(Path_orig+videoClass)
    for name in videoList:
        count =count+1
        name_path=Path_orig+videoClass+'/'+name
        cap=cv2.VideoCapture(name_path)
        width = int(cap.get(3))
        
        height = int(cap.get(4))
        
        fps = int(cap.get(5))
        framenum = int(cap.get(7))
        yuvname = str(count)+videoClass + '_'+str(width) +'x'+str(height)+'_'+str(framenum)+'_'+str(fps)+'_'+'.yuv'
            
        if name==videoList[-1]:
            widthList_validation.append(width)
            heightList_validation.append(height)
            ff = FFmpeg(inputs={name_path: None},outputs={Path_validation+yuvname: '-pix_fmt yuv420p -vsync passthrough'})
        else:
            widthList_train.append(width)
            heightList_train.append(height)
            ff = FFmpeg(inputs={name_path: None},outputs={Path_train+yuvname:'-pix_fmt yuv420p -vsync passthrough'})
        
        # print(ff.cmd)
        ff.run(stdout=PIPE,stderr=PIPE)
        # print(yuvname)

scio.savemat('G:/CRSR/para.mat', {'widthList_train': np.array(widthList_train),'heightList_train':np.array(heightList_train),\
               'widthList_validation':np.array(widthList_validation),'heightList_validation':np.array(heightList_validation)})
上一篇下一篇

猜你喜欢

热点阅读