FFmpeg-在最终视频中丢弃了Python音频

FFmpeg-Python audio getting dropped in final video(FFmpeg-在最终视频中丢弃了Python音频)

本文介绍了FFmpeg-在最终视频中丢弃了Python音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视频放在背景图像上,但生成的输出视频没有音频。有没有办法让音频保持原样?
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)。


以下代码示例应用trimsetptscropoverlay过滤器,并添加源音频:

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
    

输出帧示例:

视频包括嘟嘟声...

这篇关于FFmpeg-在最终视频中丢弃了Python音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:FFmpeg-在最终视频中丢弃了Python音频

基础教程推荐