gpt4 book ai didi

python - FFmpeg-Python 音频在最终视频中丢失

转载 作者:行者123 更新时间:2023-12-04 22:56:53 28 4
gpt4 key购买 nike

我试图将视频放在背景图像之上,但输出视频生成时没有音频。有什么办法可以保持音频原样吗?

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 , cropoverlay过滤器,并添加源音频:
    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

  • 示例输出帧:
    enter image description here
    该视频包括一个哔哔声...

    关于python - FFmpeg-Python 音频在最终视频中丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71018371/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com