Merge two uncertain audio files, which may be mono or stereo, into one stereo file using FFmpeg?

audioffmpeg

input:

Two audio file, It may be stereo or mono.

output:

  • A stereo file, and the stereo FL uses one file all channels, and FR uses other file all channels.
  • The total duration is based on the longest file.
  • I hope it can be achieved through FFmpeg.

There are four case:

  • mono + mono -> stereo
  • mono + stereo -> stereo
  • stereo + mono -> stereo
  • stereo + stereo -> stereo

I hope someone can help me, I am a newbie to FFmpeg.

Best Answer

  1. Get duration of each input with ffprobe to determine which audio is shortest.
  2. Convert each input to mono with the aformat filter.
  3. Apply the apad filter to the shorter input.
  4. Join multiple input streams into one multi-channel stream with the join filter.

Example:

ffmpeg -i input0 -i input1 -filter_complex "[0:a]aformat=channel_layouts=mono:sample_rates=44100,apad[a0];[1:a]aformat=channel_layouts=mono:sample_rates=44100[a1];[a0][a1]join=inputs=2:channel_layout=stereo[a]" -map "[a]" output

Also see FFmpeg Wiki: Audio Channels.

Related Question