python直接操作ffmpeg处理视频

python操作ffmpeg处理视频

ffmpeg的安装及基本使用看上一篇FFmpeg工具进行快速的视频拼接
简单操作,批量处理。

注意:这里视频路径是绝对路径。

视频切割

import os
import time

# 将视频切割
def video_cut():
    list = [1108,1108,1108,1108,1098,1108,
            1108,1098,1108,1108,1108,
            1108,1108,1108,1108,1108,
            1098,1108,1108,1108,1108,
            1108,1108,1108,1108,1108,1108]
    for i in range(1, 27):
        # -ss 指定从什么时间开始
		# -t 指定需要截取多长时间(秒)
        # ffmpeg -ss 00:00:01 -t 00:1:30 -i 01.ts -vcodec copy -acodec copy 1.ts // 一般就可以
        # ffmpeg -ss 111 -t 1108 -accurate_seek -i 04.ts -codec copy -avoid_negative_ts 1 4.mp4  // 二般情况
        cmd = "ffmpeg -ss 111 -t " + f'{list[i]}' + " -accurate_seek -i G:\\xing\\0" + f'{i}' \
              + ".ts -codec copy -avoid_negative_ts 1 G:\\xing\\" + f'{i}' + ".mp4"
        re = os.system(cmd)
        time.sleep(1)
        if re == 0:
            print(f'{i}' + '成功')
        else:
            print(f'{i}' + '失败!!!!!!!!!!!!!!!!!!')

if __name__ == '__main__':
    video_cut()
    print('over')

将mp4转换为ts文件

import os
import time

# 将mp4转换为ts文件
def mp4_ts():
    for i in range(1, 27):
        # ffmpeg -i 3.ts -c copy -map 0:v -map 0:a -bsf:a aac_adtstoasc 3.mp4  // ts-mp4
        # ffmpeg -i 4.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 4.ts // mp4-ts
        cmd = "ffmpeg -i G:\\xing\\" + f'{i}' \
              + ".mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb G:\\xing\\" + f'{i}' + ".ts"
        re = os.system(cmd)
        time.sleep(1)
        if re == 0:
            print(f'{i}' + '转换成功')
        else:
            print(f'{i}' + '转换失败!!!!!!!!!!!!!!!!!!')

if __name__ == '__main__':
    mp4_ts()
    print('over')

ts文件合并为一个mp4文件

import os
import time

# ts文件合并为一个mp4文件
def mp4():
    # ffmpeg -f concat -i list.txt -c copy output.mp4文件格式:
    """
	list.txt文件格式:(要合并的视频,注意file后面有一个空格)
	file '1.ts'
	file '2.ts'
	file '3.ts'
	....
	"""
    cmd = "ffmpeg -f concat -i G:\\xing\\list.txt -c copy G:\\xing\\output.mp4"
    re = os.system(cmd)
    time.sleep(1)
    if re == 0:
        print('合并成功')
    else:
        print('合并失败!!!!!!!!!!!!!!!!!!')

if __name__ == '__main__':
    mp4()
    print('over')

猜你喜欢

转载自blog.csdn.net/weixin_46085748/article/details/124221771
今日推荐