Search code examples
xamarinffmpegxamarin.androidandroid-ffmpeg

Xamarin Android merge audio files with FFMpeg


I am using this binding library for FFMpeg: https://github.com/gperozzo/XamarinAndroidFFmpeg

My goal is to mix two audio files.

String s = "-i " + "test.wav" + " -i " + test2.mp3 + " -filter_complex amix=inputs=2:duration=first " + "result.mp3";

Device.BeginInvokeOnMainThread(async () =>
{
   await FFMpeg.Xamarin.FFmpegLibrary.Run(Forms.Context, s);
});

So I have 2 input files: one is .mp3 and another one is .wav.

I've tried also next commands:

String s= "-i "+ "test.wav" +" -i "+ "test2.mp3" + " -filter_complex [0:0][1:0]concat=n=2:v=0:a=1[out] -map [out] " + "result.mp3";
String s = "-i " + "test.wav" + " -i " + "test2.mp3" + " -filter_complex [0:a][1:a]amerge=inputs=2[aout] -map [aout] -ac 2 " + "result.mp3";

1) Could I mix two different audio formats (in my case .mp3 & .wav) or they should be equivalent? 2) What is the correct command line for the mixing?

Thanks in advance.


Solution

  • 1) Could I mix two different audio formats (in my case .mp3 & .wav)?

    Yes. The input format does not matter because it will be fully decoded to PCM audio before being fed to the filter, but you have to be aware of how the various input channels will be mixed to create the channel layout for the output. Read the documentation on the amerge and amix filters for more info.

    2) What is the correct command line for the mixing?

    Your command using amerge should work:

    ffmpeg -i test.wav -i test2.mp3 -filter_complex "[0:a][1:a]amerge=inputs=2[aout]" -map "[aout]" -ac 2 result.mp3
    

    Or using amix:

    ffmpeg -i test.wav -i test2.mp3 -filter_complex "[0:a][1:a]amix=inputs=2:duration=shortest[aout]" -map "[aout]" result.mp3