基本語法
所用的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][ ]中三個參數(shù)依次為:剪輯視頻源文件;第一個時間為剪輯的起始時間;第二個時間為視頻持續(xù)的時間長度; 剪輯好的文件名
2.合并視頻片段
ffmpeg -f concat -safe 0 -i [cutfiles.txt] -c copy [output_all.mp4]參數(shù)一為合并文件的目錄txt,參數(shù)二為合并后的文件名。
功能需求
本次記錄如何使用ffmpeg對多路視頻按照不同時刻區(qū)間進行一次性快速剪輯,同時合成剪輯片段。
首先準備好要剪輯的視頻文件:
以及需要剪輯的時間片段,采用以下形式:
其中times的value可以是xx:xx:xx-xx:xx:xx
或者?xx:xx-xx:x
代碼實現(xiàn)
test.py:
# coding=utf-8
import os, re
#剪輯參數(shù),時間可以為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):
# []中三個參數(shù)依次為: 剪輯視頻源文件 第一個時間為剪輯的起始時間 第二個時間為視頻持續(xù)的時間長度 剪輯好的文件名
# 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_file
os.system(ffmpeg_com)
# 循環(huán)剪輯一個視頻中的不同片段
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'
#輸出的視頻文件(存入文本中,后續(xù)用于合成該文本那種的所有文件對應的片段)
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)
# 將時間格式轉換為秒數(shù) 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])
# 輸入秒數(shù)轉換成標準的時間參數(shù) 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%3600
m=int(a/60)
if m>=0 and m<10:
M="0"+str(m)
else:
M=str(m)
s=a%60
if 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 和持續(xù)時間00:00:07的格式
def get_time_parm(time_parm):
# 1 拆分 2:48-2:55 =》 2:48 和 2:55
m = re.match(r'^([0-9]*:[0-9]*)-([0-9]*:[0-9]*)', time_parm)
begin_parm = m.group(1) # 2:48
end_parm = m.group(2) # 2:55
# 2 計算持續(xù)時間 2:48 和 2:55 =》 00:00:07
# 2.1 全換成秒
begin_int = time_str_2_seconds(begin_parm) # 168
end_int = time_str_2_seconds(end_parm) # 175
print(begin_int,end_int)
# 2.2 7秒 =》 00:00:07
continuous_int = end_int - begin_int # 7
continuous = seconnds_2_time(continuous_int) # 00:00:07
# 3 2:48=>00:02:48
# 3.1 2:48=>168
seconds_begin = time_str_2_seconds(begin_parm)
# 3.2 168=>00:02:48
begin = seconnds_2_time(seconds_begin)
return [begin, continuous]
#合并剪輯好的視頻
def concat_videos():#參數(shù)一為合并文件的目錄txt,參數(shù)二為合并后的文件名
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)
#循環(huán)剪輯多路視頻
list=[v1, v2, v3, v4, v5,]
for v in list:
print(v)
cut_videos(v)
#合并視頻
concat_videos()
效果展示



原文?ffmpeg--同時剪輯多個視頻并合并_ffmpeg合并多個視頻_小屋*的博客-CSDN博客
★文末名片可以免費領取音視頻開發(fā)學習資料,內容包括(FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)以及音視頻學習路線圖等等。
見下方!↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓文章來源:http://www.zghlxwxcb.cn/news/detail-603581.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-603581.html
到了這里,關于ffmpeg——同時剪輯多個視頻并合并的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!