添加背景音频后的ffmpeg问题

Posted

技术标签:

【中文标题】添加背景音频后的ffmpeg问题【英文标题】:ffmpeg issue after adding background audio 【发布时间】:2021-04-08 21:57:36 【问题描述】:

所以我有一个找不到答案的问题。我有一个视频,连接后 7 分 38 秒。当我想为这个视频添加背景音频时,它会切断原始视频的最后 7 秒。我找不到原因。

如果有人能以任何方式帮助我,那就太棒了,因为过去 3 天我一直在排除故障。

我的 FFmpeg 版本是ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers

对于 concat,我使用这个:FFmpeg -f concat -safe 0 -i txtfile.txt -c copy -ac 2 output.mp4

为了添加背景音频,我使用这个:ffmpeg -i output.mp4 -i background.mp3 -c:v copy -filter_complex "[0:a]aformat=fltp:44100:stereo,apad[0a];[1]aformat=fltp:44100:stereo,volume=0.2[1a];[0a][1a]amerge[a]" -map 0:v -map "[a]" -ac 2 -shortest output_with_background.mp4

在做它的事情时控制台输出是:

ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.1 (GCC) 20200523
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baee72440] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\videos.txt':
  Duration: N/A, start: -0.023220, bitrate: 18916 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 18861 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 54 kb/s
    Metadata:
      handler_name    : SoundHandler
File 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\output.mp4' already exists. Overwrite ? [y/N] y
Output #0, mp4, to 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\output.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 18861 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 54 kb/s
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baee72440] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 5 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baeeec180] Auto-inserting h264_mp4toannexb bitstream filter
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baee72440] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 14 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baee72440] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 20 times
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000024baee72440] Auto-inserting h264_mp4toannexb bitstream filter
    Last message repeated 7 times
frame=   52 fps= 42 q=-1.0 Lsize=    7838kB time=00:07:38.82 bitrate= 139.9kbits/s speed= 374x
video:4802kB audio:2956kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.044608%
ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3.1 (GCC) 20200523
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:07:38.85, start: 0.000000, bitrate: 139 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 87 kb/s, 0.12 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 52 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #1, mp3, from 'C:\Users\shooey\Desktop\junky\Content\assets\background.mp3':
  Metadata:
    major_brand     : dash
    Software        : Lavf57.56.101
    minor_version   : 0
    compatible_brands: iso6mp41
  Duration: 00:20:03.02, start: 0.025057, bitrate: 167 kb/s
    Stream #1:0: Audio: mp3, 44100 Hz, stereo, fltp, 167 kb/s
    Metadata:
      encoder         : LAME3.100
File 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\output_bckg.mp4' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:1 (aac) -> aformat
  Stream #1:0 (mp3float) -> aformat
  Stream #0:0 -> #0:0 (copy)
  amerge -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
[Parsed_amerge_4 @ 0000017093965c80] No channel layout for input 1
[Parsed_amerge_4 @ 0000017093965c80] Input channel layouts overlap: output layout will be determined by the number of distinct input channels
Output #0, mp4, to 'C:\Users\shooey\Desktop\junky\Content\07-04-2021\output_bckg.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 87 kb/s, 0.12 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      encoder         : Lavc58.54.100 aac
frame=   52 fps=5.5 q=-1.0 Lsize=   11993kB time=00:07:31.67 bitrate= 217.5kbits/s speed=  48x
video:4802kB audio:7111kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.670061%
[aac @ 00000170935b4980] Qavg: 319.394

【问题讨论】:

【参考方案1】:

我想我自己找到了答案。我将要添加的 background.mp3 制作成 .aac 音频文件。之后,我使用相同的命令添加音频,但我使用 -t 00:07:38 而不是 -shortest,因为它是视频的长度,我发现 FFmpeg 很难识别视频中的边界。

【讨论】:

以上是关于添加背景音频后的ffmpeg问题的主要内容,如果未能解决你的问题,请参考以下文章

怎么通过ffmpeg将音频文件转换成Mp3 转

iOS利用FFmpeg解码音频数据并播放

ffmpeg:如何将 AAC 音频数据包转换为 ADTS 格式

FFmpeg进行音频的解码和播放

ffmpeg在开始和结束之前将广告音频文件添加到原始音频文件[重复]

FFmpeg学习3:播放音频