"how to extract just the audio from an mp4 file and convert it to flac file in c#?" Code Answer

5

i recently had a asp.net mvc 5 application where i needed to convert .mp4 to .webm and this worked successfully, so this is an idea to apply the same concept that worked with video files but in this instance they would be audio files.

first, you would download the ffmpeg executable and copy it to a folder inside your project/solution.

the command to convert your audio file to a flac would be something like this:

ffmpeg -i audio.xxx -c:a flac audio.flac

you can wrap this inside a c# method to execute ffmpeg like this:

public string pathtoffmpeg { get; set; }    

public void toflacformat(string pathtomp4, string pathtoflac)
{
    var ffmpeg = new process
    {
        startinfo = {useshellexecute = false, redirectstandarderror = true, filename = pathtoffmpeg}
    };

    var arguments =
        string.format(
            @"-i ""{0}"" -c:a flac ""{1}""", 
            pathtomp4, pathtoflac);

    ffmpeg.startinfo.arguments = arguments;

    try
    {
        if (!ffmpeg.start())
        {
            debug.writeline("error starting");
            return;
        }
        var reader = ffmpeg.standarderror;
        string line;
        while ((line = reader.readline()) != null)
        {
            debug.writeline(line);
        }
    }
    catch (exception exception)
    {
        debug.writeline(exception.tostring());
        return;
    }

    ffmpeg.close();
}
By f.cipriani on April 2 2022

Answers related to “how to extract just the audio from an mp4 file and convert it to flac file in c#?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.