假定分别有视频和音频两个列表,把它们合成在一个视频里。
# coding=utf-8
import ffmpeg
VIDEO_FRAMERATE = 30
VIDEO_P = '1280:720'
VIDEO_FORMAT = 'mp4'
video_list = ['./video/v1.mp4','./video/v2.mp4','./video/v3.mp4']
audio_list = ['./audio/a1.mp3','./audio/a2.mp3']
def av_list_2_mp4():
v_in_arr = []
for fn in video_list:
inv = ffmpeg.input(fn)
v_s = ffmpeg.filter(inv,filter_name='scale', size=VIDEO_P)
v_in_arr.append(v_s)
video_in = ffmpeg.concat(*v_in_arr, v=1,a=0)
a_in_arr = []
for fn in audio_list:
ina = ffmpeg.input(fn)
a_in_arr.append(ina)
audio_in = ffmpeg.concat(*a_in_arr, v=0,a=1)
try:
out, err = (ffmpeg
.output(
video_in,
audio_in,
'mp4.mp4',
vcodec='libx264',
acodec='aac',
r=VIDEO_FRAMERATE,
f=VIDEO_FORMAT)
.run(cmd=["ffmpeg"])
)
except ffmpeg.Error as error:
print("stderr:",error.stderr)
av_list_2_mp4()
video_in = ffmpeg.concat(*v_in_arr, v=1,a=0)是只取视频,如果仅仅合并视频,可以改为
video_in = ffmpeg.concat(*v_in_arr, v=1,a=1),然后删除audio_in相关部分即可