美文网首页
python + ffmpeg视频尺寸及参数转换

python + ffmpeg视频尺寸及参数转换

作者: 码农老K | 来源:发表于2019-03-23 14:49 被阅读0次

一、下载安装ffmpeg

地址:http://www.ffmpeg.org/download.html

二、需求:按照原视频尺寸裁剪出多尺寸、多规格和不同渠道视频资源,具体不多说,直接上源码,注意中文转码的细节

# -*- coding: utf-8 -*-

import os

import subprocess

import datetime

import platform

import sys

reload(sys)

sys.setdefaultencoding('gb18030')

#遍历原视频文件

def cut_O_V(root_path):

    vedio_dir=  '%s/vedio_file' % root_path

    dirs= os.listdir(vedio_dir)

for iin dirs:

        if os.path.splitext(i)[1]== ".mp4":

            bname= str(os.path.splitext(i)[0].encode('utf-8'))#.replace('\\','%').replace(' ','_')

# returnget = subprocess.call(getmp3,shell = True)

            print(bname)

create_dir(root_path, bname)

#获取视频配置参数

def get_txt_info(txt_path):

    uipath= unicode(txt_path, "utf8")

file= open(uipath, "r")

info_list= []

for linein file:

        info= line.strip().decode('gbk').encode('utf-8')

if info!= "":

            info_list.append(info)

return  info_list

#创建文件夹

def system_mkdir(dir, rec=False):

    def mkdir(dir):

        if platform.system()== "Windows":

            os.system("md \"%s\"" % (dir.replace("/", "\\"),))

else:

            os.system("mkdir -p \"%s\"" % (dir,))

if not rec:

        mkdir(dir)

else:

        dir= dir.replace("\\", "/")

pdir= ""

        for dirin dir.split("/"):

            if pdir== "":

                pdir= dir

            else:

                pdir+= "/" + dir

            if not os.path.exists(pdir):

                mkdir(pdir)

#移除文件夹

def system_rmdir(dir):

    if dir.\

find("*")== -1 and \

not os.path.exists(dir):

        return

    if platform.system()== "Windows":

        os.system("rd /s /q \"%s\"" % (dir.replace("/", "\\"),))

else:

        os.system("rm -rf \"%s\"" % (dir,))

#获取当前脚本路径

def get_root_path():

    current_dir= os.path.abspath(os.path.dirname(__file__))

root_path= current_dir.replace("\\", "/")

print(root_path)

cut_O_V(root_path.encode('utf8'))

#创建视频生成文件夹

def create_dir(root_path, old_void_name):

    normal_file_path= root_path + '/new_vedio_file/'+ unicode(old_void_name, "utf8")+ '/normal-channel'

    if os.path.exists(normal_file_path):

        system_rmdir(normal_file_path)

system_mkdir(normal_file_path)

wechat_file_path= root_path + '/new_vedio_file/'+ unicode(old_void_name, "utf8")+ '/wechat-channel'

    if os.path.exists(wechat_file_path):

        system_rmdir(wechat_file_path)

system_mkdir(wechat_file_path)

transform_vedio_action(root_path, old_void_name)

#视频尺寸转换逻辑

def transform_vedio_action(root_pass,old_void_name):

    ffmpeg_path= root_pass + '/ffmpeg-win64-static/bin/ffmpeg.exe'

    video_file= root_pass + '/vedio_file/%s.mp4' %old_void_name

    video_file= video_file.decode("utf-8").encode("gbk")

txt_path= root_pass + '/vedio_file/%s.txt' % old_void_name

    nowTime= datetime.datetime.now().strftime('%Y-%m-%d')

txt_info= []

if os.path.exists(unicode(txt_path, "utf8")):

        txt_info= get_txt_info(txt_path)

title= txt_info[0]#txt_info[0]

    print(title)

time_length= txt_info[1]

print(time_length)

start_time= txt_info[2]

print(start_time)

end_time= txt_info[3]

print(end_time)

name= txt_info[4]

print(name)

if ffmpeg_path:

        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

print("####-------- normal-channel---------###")

print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

# 1280x720

        print("start_normal-channel_1280x720")

video_name_1= nowTime+ "-" + title+ "-" + time_length+ '-1280x720-' + name

get_video_file1= root_pass +'/new_vedio_file/'+ old_void_name + '/normal-channel/%s' % video_name_1

get_video_file1= get_video_file1.decode("utf-8").encode("gbk")

print(get_video_file1)

if end_time[-4:-2]!= '00' or end_time[-7:-5]!= '00':

            cmd_run1= '%s -i %s -ss %s -t %s -r 25 -b 194k -b:v 500k -ac 2 -profile:v main -ar 48k -s 1280x720  %s.mp4' % (ffmpeg_path, video_file, start_time, end_time, get_video_file1)

else:

            cmd_run1= '%s -i %s -r 25 -b 194k -b:v 500k -ac 2 -profile:v main -ar 48k -s 1280x720 %s.mp4' % (ffmpeg_path, video_file, get_video_file1)

print(cmd_run1)

os.popen3(cmd_run1)

print("end_normal-channel_1280x720")

# 640x360

        print("start_normal-channel_640x360")

video_name_2= nowTime+ "-" + title+ "-" + time_length+ '-640x360-' + name

get_video_file2= root_pass +'/new_vedio_file/'+ old_void_name + '/normal-channel/%s' % video_name_2

get_video_file2= get_video_file2.decode("utf-8").encode("gbk")

print(get_video_file2)

if end_time[-4:-2]!= '00' or end_time[-7:-5]!= '00':

            cmd_run2= '%s -i %s -ss %s -t %s -r 25 -b 194k -b:v 500k -ac 2 -profile:v main -ar 48k -s 640x360 %s.mp4' % (ffmpeg_path, video_file, start_time, end_time, get_video_file2)

else:

            cmd_run2= '%s -i %s -r 25 -b 194k -b:v 500k -ac 2 -profile:v main -ar 48k -s 640x360 %s.mp4' % (ffmpeg_path, video_file, get_video_file2)

print(cmd_run2)

os.popen3(cmd_run2)

print("end_normal-channel_640x360")

print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

print("####---------wechat-channel---------###")

print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

# 640x360

        print("start_wechat-channel_640x360")

video_name_3= nowTime+ "-" + title+ "-" + time_length+ '-640x360-' + name

get_video_file3= root_pass +'/new_vedio_file/'+ old_void_name + '/wechat-channel/%s' % video_name_3

get_video_file3= get_video_file3.decode("utf-8").encode("gbk")

print(get_video_file3)

if end_time[-4:-2]!= '00' or end_time[-7:-5]!= '00':

            cmd_run3= '%s -i %s -ss %s -t %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 640x360 %s.mp4' % (ffmpeg_path, video_file, start_time, end_time, get_video_file3)

else:

            cmd_run3= '%s -i %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 640x360 %s.mp4' % (ffmpeg_path, video_file, get_video_file3)

print(cmd_run3)

os.popen3(cmd_run3)

print("end_wechat-channel_640x360")

# 960x540

        print("start_wechat-channel_960x540")

video_name_4= nowTime+ "-" + title+ "-" + time_length+ '-960x540-' + name

get_video_file4= root_pass +'/new_vedio_file/'+ old_void_name + '/wechat-channel/%s' % video_name_4

get_video_file4= get_video_file4.decode("utf-8").encode("gbk")

print(get_video_file4)

if end_time[-4:-2]!= '00' or end_time[-7:-5]!= '00':

            cmd_run4= '%s -i %s -ss %s -t %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 960x540 %s.mp4' % (ffmpeg_path, video_file, start_time, end_time, get_video_file4)

else:

            cmd_run4= '%s -i %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 960x540 %s.mp4' % (ffmpeg_path, video_file, get_video_file4)

print(cmd_run4)

os.popen3(cmd_run4)

print("end_wechat-channel_960x540")

# 640x480

        print("start_wechat-channel_640x480")

video_name_5= nowTime+ "-" + title+ "-" + time_length+ '-640x480-' + name

get_video_file5= root_pass +'/new_vedio_file/'+ old_void_name + '/wechat-channel/%s' % video_name_5

get_video_file5= get_video_file5.decode("utf-8").encode("gbk")

print(get_video_file5)

if end_time[-4:-2]!= '00' or end_time[-7:-5]!= '00':

            cmd_run5= '%s -i %s -ss %s -t %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 640x480 %s.mp4' % (ffmpeg_path, video_file, start_time, end_time, get_video_file5)

else:

            cmd_run5= '%s -i %s -r 24 -b:a 90k -b:v 500k -ac 2 -profile:v main -ar 44.1k -s 640x480 %s.mp4' % (ffmpeg_path, video_file, get_video_file5)

print(cmd_run5)

os.popen3(cmd_run5)

print("end_wechat-channel_640x480")

if __name__== "__main__":

    get_root_path()

使用说明

1.将资源视频放到vedio_file文件夹中(提示:可以放多个视频原文件,支持批量视频处理)

2.配置参数可根据实际需求填写和视频文件同名放在同一级目录(提示:第一个参数为生成视频的命名标题,第二个:生成视频

的命名时长,第三个、第四个分别为视频截取的开始时间和结束时间 不截取视频则全部填00:00:00.0”,第五个:生成视频命

名的姓名)

3.点击run.bat,会自动生成需求尺寸视频到new_vedio_file中对应文件夹下(提示:1.run.bat执行完毕自动关闭则全部文件生

成完成 2.每一个原视频会在new_vedio_file中生成一个对应名字的文件夹,文件夹中有normal-channel和wechat-channel两个

文件夹,分别表示普通渠道和微信渠道)

相关文章

网友评论

      本文标题:python + ffmpeg视频尺寸及参数转换

      本文链接:https://www.haomeiwen.com/subject/kfdsvqtx.html