Convert mp4 to mp3 in Xamarin.Android

Viewed 37

If I understand correctly you can't use ffmpeg on android.

How can I convert either mp4 or webm to mp3 in Xamarin.Android on Android 10 and higher without using ffmpeg or any external servers?

1 Answers

Okay, so there indeed is FFmpeg for Android.

For anyone in the same situation:
GitHub: https://github.com/Laerdal/Laerdal.FFmpeg.Android
NuGet: https://www.nuget.org/packages/Laerdal.FFmpeg.Android.Full/
On GitHub there are also links to Xamarin.Forms or Xamarin.IOS and non-full builds.

Usage:
After adding package add to top of the file: using Laerdal.FFmpeg.Android;
Then: FFmpeg.Execute(string comand)

It essentially works the same as FFmpeg in CLI.
FFmpeg.Execute($"-i {source_file} {output_path}/filename.mp3");
Would be same as ffmpeg -i input_file output_file in CLI.

Code example of converting any file type to .mp3 and handling status codes (works for .webm and .mp4, tested on Android 10):

int status = FFmpeg.Execute($"-i {source_file} {new_path}/filename.mp3");
if(status == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine($"FFmpeg failed with status code {status}");
}

If you have other questions about FFmpeg please refer to FFmpeg Documentation https://www.ffmpeg.org/documentation.html

Related