How to extract just the audio from an MP4 file and convert it to FLAC file in C#?

左心房为你撑大大i 提交于 2020-01-02 04:15:14

问题


My goal is to write C# that turns Microsoft LYNC meeting audio into text. Here is my project so far. Initially I was trying to record from microphone, save it to WAV then convert WAV to FLAC and using GoogleSpeechAPI, convert FLAC to text. But I got stuck recording microphone audio to WAV format.

The problem is it needs to be in a very specific WAV format, i.e. int16 or int24 for the WAV to work with the WAV to FLAC convertion method. I kept recording 8 bits per sample instead of (16 or 24 bits) per sample.

So, starting over. Microsoft Lync directly records meetings and saves it as a video in MP4 format. If I can somehow write code to convert MP4 to FLAC, that would also solve my problem. Any code example?


回答1:


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();
}


来源:https://stackoverflow.com/questions/29014419/how-to-extract-just-the-audio-from-an-mp4-file-and-convert-it-to-flac-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!