ffmpeg.py 1.0 KB
Newer Older
HypoX64's avatar
preview  
HypoX64 已提交
1 2 3
import os,json

def video2image(videopath,imagepath):
H
hypox64 已提交
4
    os.system('ffmpeg -i "'+videopath+'" -f image2 '+imagepath)
HypoX64's avatar
preview  
HypoX64 已提交
5 6

def video2voice(videopath,voicepath):
H
hypox64 已提交
7
    os.system('ffmpeg -i "'+videopath+'" -f mp3 '+voicepath)
HypoX64's avatar
preview  
HypoX64 已提交
8 9

def image2video(fps,imagepath,voicepath,videopath):
H
hypox64 已提交
10 11
    os.system('ffmpeg -y -r '+str(fps)+' -i '+imagepath+' -vcodec libx264 '+'./tmp/video_tmp.mp4')
    #os.system('ffmpeg -f image2 -i '+imagepath+' -vcodec libx264 -r '+str(fps)+' ./tmp/video_tmp.mp4')
H
hypox64 已提交
12
    os.system('ffmpeg -i ./tmp/video_tmp.mp4 -i "'+voicepath+'" -vcodec copy -acodec copy '+videopath)
HypoX64's avatar
preview  
HypoX64 已提交
13 14

def get_video_infos(videopath):
H
hypox64 已提交
15
    cmd_str =  'ffprobe -v quiet -print_format json -show_format -show_streams -i "' + videopath + '"'  
HypoX64's avatar
preview  
HypoX64 已提交
16 17 18
    out_string = os.popen(cmd_str).read()
    infos = json.loads(out_string)
    fps = eval(infos['streams'][0]['avg_frame_rate'])
H
hypox64 已提交
19
    endtime = float(infos['format']['duration'])
HypoX64's avatar
preview  
HypoX64 已提交
20 21 22 23
    width = int(infos['streams'][0]['width'])
    height = int(infos['streams'][0]['height'])
    return fps,endtime,width,height