I am trying to create an app that allows a user to download the audio of youtube videos in Xamarin.Forms. I can download the video using 'VideoLibrary' and convert it to an mp3 using 'MediaToolkit' in a C# Console Application without any errors.
static void DownloadAudio(string videoUrl, string saveDir)
{
YouTube youtube = YouTube.Default;
Video vid = youtube.GetVideo(videoUrl);
File.WriteAllBytes(saveDir + vid.FullName, vid.GetBytes());
string originalFile = saveDir + vid.FullName;
var inputFile = new MediaToolkit.Model.MediaFile { Filename = saveDir + vid.FullName };
var outputFile = new MediaToolkit.Model.MediaFile { Filename = $"{saveDir + vid.FullName}.mp3" };
using (var engine = new MediaToolkit.Engine())
{
engine.GetMetadata(inputFile);
engine.Convert(inputFile, outputFile);
if (File.Exists(originalFile))
{
File.Delete(originalFile);
}
}
}
The problem is that this won't work in Xamarin. Only the portable version of the nuget package can be installed which doesn't contain definitions for Engine or MediaFile.
Furthermore, not all videos download as .mp4, some download as .webm. I need the output files to be both .mp3 and .mp4.
How can I convert the video files into audio files in Xamarin.Forms?