How to call ffmpeg.exe to convert audio files to Windows Azure?

I run a web role in Windows Azure to receive AAC audio files (loaded with base64 string) and save them to blob. It works great.

Next, I also have to convert them to MP3 and store MP3s in blob too. I decided to use something like ffmpeg.exe -i path.aac path.mp3 .

The problems are as follows:

  • How to call external ffmpeg.exe inside web role web service?
  • What will be the way ?

Please help me if you know. Thank you in advance.

+6
source share
2 answers

I suggest you use Local Storage Resource for your website, where you can download AAC files from blob storage and convert them to MP3. Then upload back to blob repository.

Please note that you can also use Path.GetTempFileName () to get the temporary file name for your AAC / MP3 files, but I do not encourage it (even if I did it before).

As with ffmpeg, you can view the AzureVideoConv code I created a while ago. You will find a lot of useful code here.

Here is an example of the actual ffmpeg call (note that I download exe from the blob repository to avoid inflating my azure package with external exe files and easily update the ffmpeg.exe file if necessary):

internal void ConvertFile(string inputFileName, Guid taskID) { string tmpName = string.Format( "{0}\\{1}.flv", Path.GetTempPath(), inputFileName.Substring(inputFileName.LastIndexOf("\\")+1)); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = this._processorExecutable; psi.Arguments = string.Format(@"-i ""{0}"" -y ""{1}""", inputFileName, tmpName); psi.CreateNoWindow = true; psi.ErrorDialog = false; psi.UseShellExecute = false; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = false; psi.RedirectStandardError = true; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(psi)) { exeProcess.PriorityClass = ProcessPriorityClass.High; string outString = string.Empty; // use ansynchronous reading for at least one of the streams // to avoid deadlock exeProcess.OutputDataReceived += (s, e) => { outString += e.Data; }; exeProcess.BeginOutputReadLine(); // now read the StandardError stream to the end // this will cause our main thread to wait for the // stream to close (which is when ffmpeg quits) string errString = exeProcess.StandardError.ReadToEnd(); Trace.WriteLine(outString); Trace.TraceError(errString); byte[] fileBytes = File.ReadAllBytes(tmpName); if (fileBytes.Length > 0) { this._sSystem.SaveOutputFile( fileBytes, tmpName.Substring(tmpName.LastIndexOf("\\")+1), taskID ); } } } catch (Exception e) { Trace.TraceError(e.Message); } } 

NOTE The last project check is performed using the Windows Azure SDK 1.3

+9
source

Thanks a lot @astaykov. You did a good job. Although this is not characteristic of my case (I need a specific piece of code instead of a whole large project), but it really inspired me. To indicate in my case, I'm going to answer this question myself - please note that I did this based on @astaykov code from somewhere directly copy and paste .

  • First, configure the role with Local Storage Resource . Then enter the path for this code:

      LocalResource converter_path = RoleEnvironment.GetLocalResource("AudioConvertSpace"); string rootPathName = converter_path.RootPath; 
  • enter the path to the ffmpeg.exe, xxx.aac and xxx.mp3 file in local storage:

      string aac_path = rootPathName + "\\" + "fmwa-" + guidguid + ".aac"; string mp3_path = rootPathName + "\\" + "fmwa-" + guidguid + ".mp3"; string exe_path = rootPathName + "\\" + "ffmpeg.exe"; 
  • write the .aac file to local storage:

      System.IO.File.WriteAllBytes(aac_path, decoded_audio_byte_array); 
  • Keep in mind that local storage is not guaranteed to be stable or durable, so check for ffmpeg.exe if it does not exist, download it from blob.

      if (System.IO.File.Exists(exe_path) == false) { var exeblob = _BlobContainer.GetBlobReference("ffmpeg.exe"); exeblob.DownloadToFile(exe_path, null); } 
  • and run the ffmpeg.exe process:

      ProcessStartInfo psi = new ProcessStartInfo(); 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; Process exeProcess = Process.Start(psi); exeProcess.PriorityClass = ProcessPriorityClass.High; string outString = string.Empty; exeProcess.OutputDataReceived += (s, e) => { outString += e.Data; }; exeProcess.BeginOutputReadLine(); string errString = exeProcess.StandardError.ReadToEnd(); Trace.WriteLine(outString); Trace.TraceError(errString); exeProcess.WaitForExit(); 
  • upload the output of the ffmpeg.exe file to the blob repository:

      byte[] mp3_audio_byte_array = System.IO.File.ReadAllBytes(mp3_path); var mp3blob = _BlobContainer.GetBlobReference("fmwa-"+guidguid+".mp3"); mp3blob.Properties.ContentType = "audio/mp3"; mp3blob.UploadByteArray(mp3_audio_byte_array); 
  • clear temporary files:

      System.IO.File.Delete(aac_path); System.IO.File.Delete(mp3_path); 
+8
source

Source: https://habr.com/ru/post/905081/


All Articles