Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streams mixed when using -filter_complex amerge in FFmpeg

I am currently having issues with ffmpeg and one of its filters.

I am trying to merge 2 audio streams of a video into one. for this purpose I tried this command:

ffmpeg -i /home/maniaplanet/Videos/ManiaPlanet\ 2014-08-21\ 20-09-13-082.avi.output.mkv -filter_complex "[0:1][0:2] amerge=inputs=2"-c:v copy -c:a libvo_aacenc -b:a 256k /var/www/files/output.mp4

But I get this output:

ffmpeg version 1.0.10 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jul 25 2014 07:50:40 with gcc 4.7 (Debian 4.7.2-5)
  configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-nonfree --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-libvpx --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter --enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/x86_64-linux-gnu --disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --enable-libass --enable-libopus --enable-fontconfig --enable-libfdk-aac --enable-libdc1394 --disable-altivec --dis  libavutil      51. 73.101 / 51. 73.101
  libavcodec     54. 59.100 / 54. 59.100
  libavformat    54. 29.104 / 54. 29.104
  libavdevice    54.  2.101 / 54.  2.101
  libavfilter     3. 17.100 /  3. 17.100
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
Input #0, matroska,webm, from '/home/maniaplanet/Videos/ManiaPlanet 2014-08-21 20-09-13-082.avi.output.mkv':
  Metadata:
    ISRC            : Video:RGB24 Audio0:Headset (2- Plantronics .Audio 655 DSP) Audio1:Headset (2- Plantronics .Audio 655 DSP)
    ENCODER         : Lavf55.37.100
  Duration: 01:49:48.47, start: 0.000000, bitrate: 3867 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 1280x1024, SAR 1:1 DAR 5:4, 30 fps, 30 tbr, 1k tbn, 60 tbc (default)
    Stream #0:1: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/s (default)
    Stream #0:2: Audio: mp3, 48000 Hz, stereo, s16, 320 kb/s (default)
File '/var/www/files/output.mp4' already exists. Overwrite ? [y/N] y
Input channel layouts overlap: output layout will be determined by the number of distinct input channels
[libvo_aacenc @ 0x7ae800] Unable to set encoding parameters
Output #0, mp4, to '/var/www/files/output.mp4':
  Metadata:
    ISRC            : Video:RGB24 Audio0:Headset (2- Plantronics .Audio 655 DSP) Audio1:Headset (2- Plantronics .Audio 655 DSP)
    ENCODER         : Lavf55.37.100
    Stream #0:0: Audio: aac, 48000 Hz, 4.0, s16, 256 kb/s
    Stream #0:1: Video: h264, yuv420p, 1280x1024 [SAR 1:1 DAR 5:4], q=2-31, 30 fps, 90k tbn, 1k tbc (default)
Stream mapping:
  Stream #0:1 (mp3) -> amerge:in0
  Stream #0:2 (mp3) -> amerge:in1
  amerge -> Stream #0:0 (libvo_aacenc)
  Stream #0:0 -> #0:1 (copy)
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

I think the important part is at the bottom:

Stream mapping:
  Stream #0:1 (mp3) -> amerge:in0
  Stream #0:2 (mp3) -> amerge:in1
  amerge -> Stream #0:0 (libvo_aacenc)
  Stream #0:0 -> #0:1 (copy)

It maps the video stream as the second stream and the audio gets first. How do i resolve this? -map did not help. (Maybe I just used it wrong)

like image 657
BrainStone Avatar asked Aug 25 '14 15:08

BrainStone


1 Answers

Example

ffmpeg -i input -filter_complex "[0:a:0][0:a:1] amerge=inputs=2 [a]" \
-map [0:v] -map "[a]" -c:v copy -c:a libfdk_aac -ac 2 -b:a 128k output.mp4

Notes

  • Your output in your question contained 4 channels of audio, but I'll assume you actually wanted to downmix it to stereo. You can do that with the pan audio filter or with -ac 2 as shown above. See FFmpeg Audio Channel Manipulation: 2 × stereo → stereo for an example using pan.

  • As you guessed, you can control mapping with -map. The order of the mapping can determine the output of the output mapping.

  • libfdk_aac is the best AAC encoder supported by ffmpeg, and libvo_aacenc is the worst. I switched to libfdk_aac since your build supports it, and it will allow a lower bitrate and still sound fairly good. See the FFmpeg AAC Encoding Guide.

  • I changed the filtering input from [0:1] to [0:a:0] which means "first input:audio stream type:first (audio) stream". In this case it maps to the same stream but this allows you to be slightly lazier.

  • You can add -movflags +faststart if your viewers are going to watch this via progressive download in a browser. It will relocate the moov atom from the end of the file to the beginning to allow playback to begin with less of a wait by the viewer.

like image 118
llogan Avatar answered Oct 14 '22 16:10

llogan