合并 gtts/ffmpeg 创建的 mp3 文件时出现问题

合并 gtts/ffmpeg 创建的 mp3 文件时出现问题

我想从 Linux 命令行创建一个 mp3 文件,该文件是两个 mp3 文件的串联,中间有一段静音。我使用的是 Google 文本转语音 gtts 包 (https://pypi.org/project/gTTS/)创建两个(非静音)mp3 文件:

gtts-cli 'hello' --output hello.mp3
gtts-cli 'goodbye' --output goodbye.mp3

然后我用ffmpeg软件创建一个mp3文件:

ffmpeg -filter_complex aevalsrc=0 -t 10 10SecSilence.mp3

我分别听了每个 mp3 音频,它们都很好。

然后我用 ffmpeg 连接这三个音频:

ffmpeg -i "concat:hello.mp3|10SecSilence.mp3|goodbye.mp3" -acodec copy test.mp3

这是输出。

  ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.28)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_7 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0x7fbe7b00d200] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'concat:hello.mp3|10SecSilence.mp3|goodbye.mp3':
  Duration: 00:00:10.85, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, mono, fltp, 64 kb/s
Output #0, mp3, to 'test.mp3':
  Metadata:
    TSSE            : Lavf58.45.100
    Stream #0:0: Audio: mp3, 44100 Hz, mono, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mp3 @ 0x7fbe7b010000] Audio packet of size 227 (starting with 49443304...) is invalid, writing it anyway.
size=      85kB time=00:00:11.61 bitrate=  60.0kbits/s speed=3.01e+03x    
video:0kB audio:85kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.291438%

当我听 test.mp3 时,我听到“你好”,紧接着是“再见”,然后是停顿。

我怎样才能解决这个问题?

已更新以包含 ffmpeg concat 命令的输出(上面)并显示每个输入流的详细信息(下面)。

ffmpeg -i hello.mp3 -i 10SecSilence.mp3 -i goodbye.mp3 test.mp3
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.28)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_7 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0x7fc59e009600] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'hello.mp3':
  Duration: 00:00:00.77, start: 0.000000, bitrate: 32 kb/s
    Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Input #1, mp3, from '10SecSilence.mp3':
  Metadata:
    encoder         : Lavf58.45.100
  Duration: 00:00:10.03, start: 0.025057, bitrate: 64 kb/s
    Stream #1:0: Audio: mp3, 44100 Hz, mono, fltp, 64 kb/s
[mp3 @ 0x7fc59e00cc00] Estimating duration from bitrate, this may be inaccurate
Input #2, mp3, from 'goodbye.mp3':
  Duration: 00:00:00.82, start: 0.000000, bitrate: 32 kb/s
    Stream #2:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'test.mp3':
  Metadata:
    TSSE            : Lavf58.45.100
    Stream #0:0: Audio: mp3 (libmp3lame), 24000 Hz, mono, fltp
    Metadata:
      encoder         : Lavc58.91.100 libmp3lame
size=       3kB time=00:00:00.76 bitrate=  36.4kbits/s speed=91.7x    
video:0kB audio:3kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 7.261029%

已更新以提供更多细节。

ffmpeg -f lavfi -i anullsrc=channel_layout=mono:sample_rate=44100 -t 10 10SecSilence.mp3
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.28)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_7 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, lavfi, from 'anullsrc=channel_layout=mono:sample_rate=44100':
  Duration: N/A, start: 0.000000, bitrate: 352 kb/s
    Stream #0:0: Audio: pcm_u8, 44100 Hz, mono, u8, 352 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_u8 (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to '10SecSilence.mp3':
  Metadata:
    TSSE            : Lavf58.45.100
    Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, mono, s16p
    Metadata:
      encoder         : Lavc58.91.100 libmp3lame
size=      79kB time=00:00:10.00 bitrate=  64.3kbits/s speed= 310x    
video:0kB audio:78kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.282873%

$ cat input.txt 
file 'hello.mp3'
file '10SecSilence.mp3'
file 'goodbye.mp3'
$ ffmpeg -f concat -i input.txt -c copy output.mp3
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple clang version 12.0.0 (clang-1200.0.32.28)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_7 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0x7fa695009200] Estimating duration from bitrate, this may be inaccurate
Input #0, concat, from 'input.txt':
  Duration: N/A, start: 0.000000, bitrate: 32 kb/s
    Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Output #0, mp3, to 'output.mp3':
  Metadata:
    TSSE            : Lavf58.45.100
    Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mp3 @ 0x7fa695013800] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 17856 >= 17831
[mp3 @ 0x7fa696019800] Estimating duration from bitrate, this may be inaccurate
size=      85kB time=00:00:11.59 bitrate=  59.9kbits/s speed=3.42e+03x    
video:0kB audio:85kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.273723%

hello.mp3在这里:https://drive.google.com/file/d/1VkT0Uuune8D9RnD5gZ4BgqHLY-ddorSt/view?usp=sharing

goodbye.mp3在这里:https://drive.google.com/file/d/1STdYIHhOeuWAQ5RONEiP5k0anrpUbNLt/view?usp=sharing

答案1

所有输入必须具有相同的通道布局和采样率。您的静音音频具有不同的采样率。使用以下命令重新设置:

ffmpeg -f lavfi -i anullsrc=channel_layout=mono:sample_rate=24000 -t 10 10SecSilence.mp3

然后使用连接解复用器而不是 concat 协议。concat 协议过于简单,不适用于大多数格式。我几乎从不使用它。

包含input.txt以下内容:

file 'hello.mp3'
file '10SecSilence.mp3'
file 'goodbye.mp3'

然后连接:

ffmpeg -f concat -i input.txt -c copy output.mp3

如果输入文件名有空格或特殊字符,则将-safe 0输入选项添加到连接命令。

相关内容