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):
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
def mp4_ts():
for i in range(1, 27):
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
def 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')
|