ffmpeg——同时剪辑多个视频并合并
生活随笔
收集整理的這篇文章主要介紹了
ffmpeg——同时剪辑多个视频并合并
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于ffmpeg的介紹可以參看:FFMPEG視音頻編解碼零基礎學習方法
官網:https://ffmpeg.org/download.html
目錄
- 基本語法
- 功能需求
- 代碼實現
- 效果展示
基本語法
所用的ffmpeg的語法:
1.剪輯單個視頻
ffmpeg -i [2021-11-24-1-2.mp4] -vcodec copy -acodec copy -ss [00:00:00] -to [00:00:05] [output/p3.mp4][ ]中三個參數依次為:剪輯視頻源文件;第一個時間為剪輯的起始時間;第二個時間為視頻持續的時間長度; 剪輯好的文件名
2.合并視頻片段
ffmpeg -f concat -safe 0 -i [cutfiles.txt] -c copy [output_all.mp4]參數一為合并文件的目錄txt,參數二為合并后的文件名。
功能需求
本次記錄如何使用ffmpeg對多路視頻按照不同時刻區間進行一次性快速剪輯,同時合成剪輯片段。
首先準備好要剪輯的視頻文件:
以及需要剪輯的時間片段,采用以下形式:
其中times的value可以是xx:xx:xx-xx:xx:xx或者 xx:xx-xx:xx
代碼實現
test.py:
# coding=utf-8 import os, re#剪輯參數,時間可以為4:25-4:29 或者00:04:25-00:04:29 注意字符格式 v1 = {'name': '2728','times': ['4:25-4:29' , '13:28-13:37',] }v2 = {'name': '2906','times': ['4:50-4:55' , '10:37-10:42',] }v3 = {'name': '2915','times': ['1:38-1:54' , '5:00-5:06' , '6:32-6:39', '8:36-8:43', '12:00-12:06', ] }v4 = {'name': '3100','times': ['1:40-1:46','4:23-4:26', '5:29-5:34', '7:10-7:17', '9:54-10:03','12:00-12:07',] }v5 = {'name': '3101','times': ['15:14-15:20',] }# 剪輯單個視頻 def cut_video(source_file, begin, continuous, output_file):# []中三個參數依次為: 剪輯視頻源文件 第一個時間為剪輯的起始時間 第二個時間為視頻持續的時間長度 剪輯好的文件名# ffmpeg -i [2021-11-24-1-2.mp4] -vcodec copy -acodec copy -ss [00:00:00] -to [00:00:05] [output/p3.mp4]ffmpeg_com = 'ffmpeg -i ' + source_file + ' -vcodec copy -acodec copy -ss ' + begin + ' -t ' + continuous + ' ' + output_fileos.system(ffmpeg_com)# 循環剪輯一個視頻中的不同片段 def cut_videos(videos_info):#剪切時間time_lines = videos_info['times']#所屬視頻文件名file_name = videos_info['name']for i in range(len(time_lines)):time_line = time_lines[i]begin, continuous = get_time_parm(time_line)#剪切的初始視頻 路徑要對應上自己的文件路徑!source_file = './'+file_name + '.mp4'#輸出的視頻文件(存入文本中,后續用于合成該文本那種的所有文件對應的片段)output_file = './output/' + file_name + '-p' + str(i) + '.mp4'f1 = open('cutfiles.txt', 'a+')f1.write('file ' + "'"+output_file+"'" + '\n')f1.close()cut_video(source_file, begin, continuous, output_file)# 將時間格式轉換為秒數 02:48->168 或 2:48->168 或 01:12:11->4331 或 1:12:11->4331 def time_str_2_seconds(time_str):# print('time_str_2_seconds get time_str->',time_str)time_list = time_str.split(':')if len(time_list)==2:return int(time_list[0]) * 60 + int(time_list[1])elif len(time_list)==3:return int(time_list[0]) * 3600 + int(time_list[1]) * 60 + int(time_list[2])# 輸入秒數轉換成標準的時間參數 30 -> 00:00:30 8126->02:15:26 def seconnds_2_time(seconds):h=int(seconds/3600)if h>=0 and h<10:H="0"+str(h)else:H=str(h)a=seconds%3600m=int(a/60)if m>=0 and m<10:M="0"+str(m)else:M=str(m)s=a%60if s>=0 and s<10:S="0"+str(s)else:S=str(s)return H+":"+M+":"+S# 將輸入的 2:48-2:55格式的時間 --》 轉換成ffmpeg所需的 開始時間00:02:28 和持續時間00:00:07的格式 def get_time_parm(time_parm):# 1 拆分 2:48-2:55 =》 2:48 和 2:55m = re.match(r'^([0-9]*:[0-9]*)-([0-9]*:[0-9]*)', time_parm)begin_parm = m.group(1) # 2:48end_parm = m.group(2) # 2:55# 2 計算持續時間 2:48 和 2:55 =》 00:00:07# 2.1 全換成秒begin_int = time_str_2_seconds(begin_parm) # 168end_int = time_str_2_seconds(end_parm) # 175print(begin_int,end_int)# 2.2 7秒 =》 00:00:07continuous_int = end_int - begin_int # 7continuous = seconnds_2_time(continuous_int) # 00:00:07# 3 2:48=>00:02:48# 3.1 2:48=>168seconds_begin = time_str_2_seconds(begin_parm)# 3.2 168=>00:02:48begin = seconnds_2_time(seconds_begin)return [begin, continuous]#合并剪輯好的視頻 def concat_videos():#參數一為合并文件的目錄txt,參數二為合并后的文件名ffmpeg_command = 'ffmpeg -f concat -safe 0 -i cutfiles.txt -c copy output_all.mp4'os.system(ffmpeg_command)if __name__ == "__main__":#建立文件夾存放剪輯結果outpath='./output/'if not os.path.exists(outpath):os.mkdir(outpath)#循環剪輯多路視頻list=[v1, v2, v3, v4, v5,]for v in list:print(v)cut_videos(v)#合并視頻concat_videos()效果展示
總結
以上是生活随笔為你收集整理的ffmpeg——同时剪辑多个视频并合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 10.0 framewo
- 下一篇: ts无损剪辑合并_音视频剪切合并器有哪些