Converting audio/video tracks to AAC format in C# app.

Alexander Kvitko 41 Reputation points
2025-02-13T09:39:31.6733333+00:00

I try convert audio/video tracks to AAC format using qaac lib. In CMD it works and output information about progress of convertation. In my C# app it works too, but I can't get information about progress, i try use these events: OutputDataReceived, ErrorDataReceived. In handler of ErrorDataReceived I don't get needed information, in handler of OutputDataReceived I get received.Data like null value. How can I get conversion progress information during conversion in C# code?

Creating process for converting:

string arguments = $"\"{engineParameters.InputFile.Filename}\" -no-delay -o \"{engineParameters.OutputFile.Filename}\"";

processStartInfo = new ProcessStartInfo
{
    FileName = QaacFilePath,
    Arguments = arguments,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden
};

Also i call these methods for process:

BeginOutputReadLine();
BeginErrorReadLine();
WaitForExit();

Handlers:

this.Process.OutputDataReceived += (sender, received) =>
{
    if (received.Data == null) return;
    //Some code...
}
this.Process.ErrorDataReceived += (sender, received) =>
{
    if (received.Data == null) return;
    //Some code...
}
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2025-02-13T18:09:52.0333333+00:00

    You don't need any external library; you can use MediaTranscoder

    A test :

    
    // Add reference to C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.19041.0\Windows.winmd
    // Add reference to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
    
    
    string sInputFile = "E:\\Sources\\CSharp_ConvertToAAC\\sample_1280x720_surfing_with_audio.mp4";
    string sOutputFile = "E:\\Sources\\CSharp_ConvertToAAC\\sample_1280x720_surfing_with_audio.m4a";
    
    var cts = new CancellationTokenSource();
    try
    {    
        await ConvertToAacAsync(sInputFile, sOutputFile, cts.Token);
    }
    catch (TaskCanceledException)
    {
        System.Diagnostics.Debug.WriteLine("Conversion canceled.");
    }
    
    
           static async Task ConvertToAacAsync(string sInputFile, string sOutputFile, CancellationToken cancellationToken)
           {
               StorageFile inputFile = await StorageFile.GetFileFromPathAsync(sInputFile);
    
               string sOutputFolderPath = System.IO.Path.GetDirectoryName(sOutputFile);
               string sOutputFileName = System.IO.Path.GetFileName(sOutputFile);
               StorageFolder outputFolder = await StorageFolder.GetFolderFromPathAsync(sOutputFolderPath);
               StorageFile outputFile = await outputFolder.CreateFileAsync(sOutputFileName, CreationCollisionOption.ReplaceExisting);
    
               var transcoder = new MediaTranscoder();
               transcoder.VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.Default; // No video processing needed
      
               var profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateM4a(Windows.Media.MediaProperties.AudioEncodingQuality.High); // AAC inside .m4a container
               
               var prepared = await transcoder.PrepareFileTranscodeAsync(inputFile, outputFile, profile);
               if (!prepared.CanTranscode)
               {
                   System.Diagnostics.Debug.WriteLine($"Transcoding not possible: {prepared.FailureReason}");
                   return;
               }
    
               System.Diagnostics.Debug.WriteLine("Conversion started...");
               var progressHandler = new Progress<double>(progress =>
               {
                   System.Diagnostics.Debug.WriteLine($"Progress: {progress}%");
               });
    
               await prepared.TranscodeAsync().AsTask(cancellationToken, progressHandler);
               System.Diagnostics.Debug.WriteLine("Conversion completed!");
           }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.