Covert video file to audio file in Xamarin Forms

纵然是瞬间 提交于 2021-02-19 06:00:50

问题


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?


回答1:


If it's not available from a PCL, you could opt to platform dependencies. Basically you create an interface for the functionality you need, implement the classes in your platform specific projects and export them with the DependencyAttribute. From your PCL you can now obtain an instance with the DependencyService (see here and here).

The interface could look like the following

public interface IMediaHelper
{
    void ConvertToMp3(string fullFileName);
}

and the implementation (in your iOS and Android projects - use a SharedProject to avoid code duplication)

[assembly: Dependency(typeof(MediaHelper))]

public class MediaHelper
{
    public void ConvertToMp3(string fullFileName)
    {
        var infile = new MediaToolkit.Model.MediaFile { Filename = fullFileName;
        var outfile = new MediaToolkit.Model.MediaFile { Filename = $"{fullFileName}.mp3" };

        using (var engine = new MediaToolkit.Engine())
        {
            engine.GetMetadata(infile);

            engine.Convert(infile, outfile);
            if (File.Exists(fullFileName))
            {
                File.Delete(fullFileName);
            }
        }
    }
}

Consequently your client code would be

void DownloadAudio(string videoUrl, string saveDir)
{
    YouTube youtube = YouTube.Default;
    Video vid = youtube.GetVideo(videoUrl);

    var fullFileName = Path.Combine(saveDir, vid.FullName);

    File.WriteAllBytes(fullFileName, vid.GetBytes());

    var mediaHelper = DependencyService.Get<IMediaHelper>();
    mediaHelper.ConvertToMp3(fullFileName);
}

Please Note: I have replaced the manual concatenation of the path and the file with the call to Path.Combine, which handles cases in which the path already comes with the path separator. Furthermore it handles the different platforms for you.

Without MediaToolkit

Obviously MediaToolkit won't work for Android (at least), hence you'll have to go the same way - with the platform dependencies - and implement what you need with what you've got out-of-the-box. See this answer on how to extract PCM data from a video and this one on how to convert PCM to MP3 - just to give you an idea.

Please note

[...] if you want to encode/decode a mp3 file you may have to pay license/patent fees.

source



来源:https://stackoverflow.com/questions/47739793/covert-video-file-to-audio-file-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!