当前位置: 首页 > 编程语言 > C#

如何在WindowsAzure上调用ffmpeg.exe转换音频文件?分享

时间:2023-04-11 01:43:18 C#

WindowsAzure上如何调用ffmpeg.exe转换音频文件?我在WindowsAzure上运行一个网络角色来接收AAC音频文件(通过base64字符串上传)并将它们存储到blob中。它现在工作正常。接下来,我还必须将它们转换为MP3并将MP3存储到blob中。我决定使用类似ffmpeg.exe-ipath.aacpath.mp3的东西。问题来了:如何以Web角色调用Web服务中的外部ffmpeg.exe?路径是什么?如果你知道请帮助我。先感谢您。我建议您为webrole使用localstorage资源,您可以在其中从blob存储下载AAC文件并将它们转换为MP3。然后上传回blob存储。另请注意,您还可以使用Path.GetTempFileName()来获取AAC/MP3文件的临时文件名,但我不鼓励这样做(即使我以前做过)。至于运行ffmpeg,您可能需要浏览我之前构建的AzureVideoConv的代码。你会在那里找到很多有用的代码。这是实际ffmpeg调用的示例(请注意,我从blob存储下载了exe,以避免使用外部exe膨胀我的azure包,并在需要时轻松更新ffmpeg.exe):internalvoidConvertFile(stringinputFileName,GuidtaskID){stringtmpName=string.Format("{0}\{1}.flv",Path.GetTempPath(),inputFileName.Substring(inputFileName.LastIndexOf("\")+1));ProcessStartInfopsi=newProcessStartInfo();psi.FileName=this._processorExecutable;psi.Arguments=string.Format(@"-i""{0}""-y""{1}""",inputFileName,tmpName);psi。创建无窗口=真;psi.ErrorDialog=false;psi.UseShellExecute=false;psi.WindowStyle=ProcessWindowStyle.Hidden;psi.RedirectStandardOutput=true;psi.RedirectStandardInput=false;psi.RedirectStandardError=true;try{//使用我们指定的信息启动进程。//调用WaitForExit然后关闭using语句。使用(ProcessexeProcess=Process.Start(psi)){exeProcess.PriorityClass=ProcessPriorityClass.High;stringoutString=string.Empty;//使用异步读取for至少一个流//以避免死锁exeProcess.OutputDataReceived+=(s,e)=>{outString+=e.Data;};exeProcess.BeginOutputReadLine();//现在读取StandardError流直到结束//这将导致我们的主线程等待//流关闭(当ffmpeg退出时)stringerrString=exeProcess.StandardError.ReadToEnd();Trace.WriteLine(outString);Trace.TraceError(errString);byte[]fileBytes=File.ReadAllBytes(tmpName);如果(fileBytes.Length>0){this._sSystem.SaveOutputFile(fileBytes,tmpName.Substring(tmpName.LastIndexOf("\")+1),taskID);}}}catch(Exceptione){Trace.TraceError(e.Message);请注意,该项目的最后一次签入是使用WindowsAzureSDK1.3非常感谢@astaykov所做的出色工作。虽然它不是特定于我的情况(我需要一段特定的代码而不是整个大项目),但它确实激励了我。为了说明我的情况,我将自己回答这个问题——请注意,我是直接从@astaykov的某处代码复制和粘贴的。首先,使用本地存储资源配置角色。然后通过这些代码获取它的路径:LocalResourceconverter_path=RoleEnvironment.GetLocalResource("AudioConvertSpace");字符串rootPathName=converter_path.RootPath;获取ffmpeg.exe、xxx.aac、xxx.mp3在本地存储的路径:stringaac_path=rootPathName+"\"+"fmwa-"+guidguid+".aac";stringmp3_path=rootPathName+"\"+"fmwa-"+guidguid+".mp3";stringexe_path=rootPathName+"\"+"ffmpeg.exe";将.aac文件写入本地存储:System.IO.File.WriteAllBytes(aac_path,decoded_audio_byte_array);请记住,本地存储不能保证稳定或持久,因此请检查ffmpeg.exe是否存在——如果不存在,请从blob下载。if(System.IO.File.Exists(exe_path)==false){varexeblob=_BlobContainer.GetBlobReference("ffmpeg.exe");exeblob.DownloadToFile(exe_path,null);}初始化并运行ffmpeg.exe进程:ProcessStartInfopsi=newProcessStartInfo();psi.FileName=exe_path;psi.Arguments=string.Format(@"-i""{0}""-y""{1}""",aac_path,mp3_path);psi.CreateNoWindow=true;psi.ErrorDialog=false;psi.UseShellExecute=false;psi.WindowStyle=ProcessWindowStyle.Hidden;psi.RedirectStandardOutput=true;psi.RedirectStandardInput=false;psi.RedirectStandardError=true;进程exeProcess=Process.Start(psi);exeProcess.PriorityClass=ProcessPriorityClass.High;字符串outString=string.Empty;exeProcess.OutputDataReceived+=(s,e)=>{outString+=e.Data;};exeProcess.BeginOutputReadLine();字符串errString=exeProcess.StandardError.ReadToEnd();Trace.WriteLine(outString);Trace.TraceError(errString);exeProcess.WaitForExit();将ffmpeg.exe的输出上传到blob存储中:byte[]mp3_audio_byte_array=System.IO.File.ReadAllBytes(mp3_path);varmp3blob=_BlobContainer.GetBlobReference("fmwa-"+guidguid+".mp3");mp3blob.Properties.ContentType="音频/mp3";mp3blob.UploadByteArray(mp3_audio_byte_array);清理临时文件:以上是C#学习教程:如何在WindowsAzure上调用ffmpeg.exe转换音频文件?如果分享的内容对你有用,需要了解更多C#学习教程,希望大家多多关注——System.IO.File.Delete(aac_path);System.IO.File.Delete(mp3_path);本文来自网络收藏,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: