FFmpeg-Python 音频在最终视频中丢失
FFmpeg-Python audio getting dropped in final video
我试图将视频放在背景图像之上,但生成的输出视频没有音频。有什么办法可以让音频保持原样吗?
def ConvertVideo(source,background,start,end,dest):
stream = ffmpeg.input(source)
strea1 = ffmpeg.input(background)
duration = end - start
stream = stream.trim(start=start,duration=duration).filter('setpts', 'PTS-STARTPTS')
stream = stream.crop(100,0,1080,720)
stream = ffmpeg.overlay(strea1,stream,x=0,y=180)
stream = stream.output(dest)
有谁知道为什么音频会掉线?这个问题有解决办法吗?
我们必须明确添加音频流。
类似于:ffmpeg.output(overlaid_vid_stream, audio_stream, dest)
.
首先使用 FFmpeg CLI 创建合成输入视频和背景(只是为了使发布的解决方案可重现和“自包含”)。
创建带音频的输入视频:
ffmpeg -y -f lavfi -i testsrc=size=128x72:rate=1 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex "[1:a][2:a]amix=inputs=2" -vcodec libx264 -g 10 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 50 in.mp4
创建黄色背景图片:
ffmpeg.exe -y -f lavfi -i color=yellow:size=128x72 -frames:v 1 background.png
例如,尺寸为 128x72(假设您的输入为 1280x720)。
以下代码示例应用 trim
、setpts
、crop
和 overlay
过滤器,并添加源音频:
source = 'in.mp4'
background = 'background.png'
dest = 'out.mp4'
end = 40
start = 10
duration = end - start
vid_stream = ffmpeg.input(source).video # Source video stream
audio_stream = ffmpeg.input(source).audio # Source audio stream
vid_background = ffmpeg.input(background).video # Background video stream
trimed_vid_stream = vid_stream.trim(start=start, duration=duration).filter('setpts', 'PTS-STARTPTS').crop(10, 0, 108, 72) # Source video stream after trimming and cropping
overlaid_vid_stream = ffmpeg.overlay(vid_background, trimed_vid_stream, x=0, y=18) # Video stream overlay of background and trimed_vid_stream
trimed_audio_stream = audio_stream.filter('atrim', start=start, duration=duration).filter('asetpts', 'PTS-STARTPTS') # Trimming the audio
output_video_and_audio = ffmpeg.output(overlaid_vid_stream, trimed_audio_stream, dest) # Output - video applies overlaid_vid_stream, and audio applies trimmed source audio
output_video_and_audio.overwrite_output().run() # Execute FFmpeg
我修改了你的命名以使其更有意义(命名所有内容 stream
让人难以理解)。
我还删除了该方法,并将参数设置为特定值(只是为了使发布的解决方案可重现)。
该解决方案适用于以下主要阶段:
创建对视频和音频流的引用:
vid_stream = ffmpeg.input(source).video # Source video stream
audio_stream = ffmpeg.input(source).audio # Source audio stream
定义视频过滤器:
vid_background = ffmpeg.input(background).video # Background video stream
trimed_vid_stream = vid_stream.trim(start=start, duration=duration).filter('setpts', 'PTS-STARTPTS').crop(10, 0, 108, 72) # Source video stream after trimming and cropping
overlaid_vid_stream = ffmpeg.overlay(vid_background, trimed_vid_stream, x=0, y=18) # Video stream overlay of background and trimed_vid_stream
定义音频过滤器(修剪音频):
trimed_audio_stream = audio_stream.filter('atrim', start=start, duration=duration).filter('asetpts', 'PTS-STARTPTS') # Trimming the audio
定义输出以包含“过滤后”的视频和修剪后的音频:
output_video_and_audio = ffmpeg.output(overlaid_vid_stream, trimed_audio_stream, dest) # Output - video applies overlaid_vid_stream, and audio applies trimmed source audio
执行 FFmpeg:
output_video_and_audio.overwrite_output().run() # Execute FFmpeg
示例输出帧:
该视频包含哔哔声...
我试图将视频放在背景图像之上,但生成的输出视频没有音频。有什么办法可以让音频保持原样吗?
def ConvertVideo(source,background,start,end,dest):
stream = ffmpeg.input(source)
strea1 = ffmpeg.input(background)
duration = end - start
stream = stream.trim(start=start,duration=duration).filter('setpts', 'PTS-STARTPTS')
stream = stream.crop(100,0,1080,720)
stream = ffmpeg.overlay(strea1,stream,x=0,y=180)
stream = stream.output(dest)
有谁知道为什么音频会掉线?这个问题有解决办法吗?
我们必须明确添加音频流。
类似于:ffmpeg.output(overlaid_vid_stream, audio_stream, dest)
.
首先使用 FFmpeg CLI 创建合成输入视频和背景(只是为了使发布的解决方案可重现和“自包含”)。
创建带音频的输入视频:
ffmpeg -y -f lavfi -i testsrc=size=128x72:rate=1 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex "[1:a][2:a]amix=inputs=2" -vcodec libx264 -g 10 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 50 in.mp4
创建黄色背景图片:
ffmpeg.exe -y -f lavfi -i color=yellow:size=128x72 -frames:v 1 background.png
例如,尺寸为 128x72(假设您的输入为 1280x720)。
以下代码示例应用 trim
、setpts
、crop
和 overlay
过滤器,并添加源音频:
source = 'in.mp4'
background = 'background.png'
dest = 'out.mp4'
end = 40
start = 10
duration = end - start
vid_stream = ffmpeg.input(source).video # Source video stream
audio_stream = ffmpeg.input(source).audio # Source audio stream
vid_background = ffmpeg.input(background).video # Background video stream
trimed_vid_stream = vid_stream.trim(start=start, duration=duration).filter('setpts', 'PTS-STARTPTS').crop(10, 0, 108, 72) # Source video stream after trimming and cropping
overlaid_vid_stream = ffmpeg.overlay(vid_background, trimed_vid_stream, x=0, y=18) # Video stream overlay of background and trimed_vid_stream
trimed_audio_stream = audio_stream.filter('atrim', start=start, duration=duration).filter('asetpts', 'PTS-STARTPTS') # Trimming the audio
output_video_and_audio = ffmpeg.output(overlaid_vid_stream, trimed_audio_stream, dest) # Output - video applies overlaid_vid_stream, and audio applies trimmed source audio
output_video_and_audio.overwrite_output().run() # Execute FFmpeg
我修改了你的命名以使其更有意义(命名所有内容 stream
让人难以理解)。
我还删除了该方法,并将参数设置为特定值(只是为了使发布的解决方案可重现)。
该解决方案适用于以下主要阶段:
创建对视频和音频流的引用:
vid_stream = ffmpeg.input(source).video # Source video stream audio_stream = ffmpeg.input(source).audio # Source audio stream
定义视频过滤器:
vid_background = ffmpeg.input(background).video # Background video stream trimed_vid_stream = vid_stream.trim(start=start, duration=duration).filter('setpts', 'PTS-STARTPTS').crop(10, 0, 108, 72) # Source video stream after trimming and cropping overlaid_vid_stream = ffmpeg.overlay(vid_background, trimed_vid_stream, x=0, y=18) # Video stream overlay of background and trimed_vid_stream
定义音频过滤器(修剪音频):
trimed_audio_stream = audio_stream.filter('atrim', start=start, duration=duration).filter('asetpts', 'PTS-STARTPTS') # Trimming the audio
定义输出以包含“过滤后”的视频和修剪后的音频:
output_video_and_audio = ffmpeg.output(overlaid_vid_stream, trimed_audio_stream, dest) # Output - video applies overlaid_vid_stream, and audio applies trimmed source audio
执行 FFmpeg:
output_video_and_audio.overwrite_output().run() # Execute FFmpeg
示例输出帧:
该视频包含哔哔声...