以Xamarin形式将视频文件转换为音频文件 [英] Covert video file to audio file in Xamarin Forms
问题描述
我正在尝试创建一个允许用户在Xamarin.Forms中下载youtube视频的音频的应用.我可以使用"VideoLibrary"下载视频,并在C#控制台应用程序中使用"MediaToolkit"将其转换为mp3.
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);
}
}
}
问题是,这在Xamarin中不起作用.只能安装nuget软件包的便携式版本,该版本不包含 Engine
或 MediaFile
的定义.
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
.
此外,并不是所有的视频都以 .mp4
的形式下载,有些则以 .webm
的形式下载.我需要输出文件都为 .mp3
和 .mp4
.
Furthermore, not all videos download as .mp4
, some download as .webm
. I need the output files to be both .mp3
and .mp4
.
如何在Xamarin.Forms中将视频文件转换为音频文件?
How can I convert the video files into audio files in Xamarin.Forms?
推荐答案
如果PCL无法使用它,则可以选择平台依赖项.基本上,您会为所需的功能创建一个接口,在特定于平台的项目中实现这些类,然后使用 DependencyAttribute
.现在,您可以从PCL中使用 DependencyService
获取实例(请参见此处和此处).
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).
界面可能如下所示
public interface IMediaHelper
{
void ConvertToMp3(string fullFileName);
}
及其实现(在您的iOS和Android项目中-使用SharedProject避免代码重复)
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);
}
请注意:我已将对路径和文件的手动连接替换为对 Path.Combine
的调用,该调用可处理路径已附带的情况路径分隔符.此外,它可以为您处理不同的平台.
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.
显然,MediaToolkit至少在Android上不起作用,因此,您必须采用相同的方式-使用平台依赖性-并用开箱即用的方式实现所需的功能.请参阅此答案有关如何从视频中提取PCM数据和
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.
请注意
[...]如果要对mp3文件进行编码/解码,则可能需要支付许可证/专利费.
[...] if you want to encode/decode a mp3 file you may have to pay license/patent fees.
这篇关于以Xamarin形式将视频文件转换为音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!