且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从MP4文件中提取音频并将其转换为C#中的FLAC文件?

更新时间:2023-01-18 15:05:28

最近我有一个ASP.NET MVC 5应用程序,我需要将.mp4转换为.webm和这工作成功,所以这是一个想法,应用与视频文件相同的概念,但在这种情况下,他们将是音频文件。



首先,您将下载 FFMPEG 可执行文件,并将其复制到项目/解决方案中的文件夹中。

将您的音频文件转换为FLAC的命令将如下所示:

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

您可以把这个包装在一个C#方法中,像这样执行FFMPEG:

pre $ public string PathToFfmpeg {get;组; }
$ b $ public void ToFlacFormat(string pathToMp4,string pathToFlac)
{
var ffmpeg = new Process
{
StartInfo = {UseShellExecute = false,RedirectStandardError = true,FileName = PathToFfmpeg}
};
$ b $ var arguments =
String.Format(
@ - i{0}-c:a flac{1},
pathToMp4,pathToFlac);

ffmpeg.StartInfo.Arguments =参数;
$ b $尝试
{
if(!ffmpeg.Start())
{
Debug.WriteLine(Error starting);
return;
}
var reader = ffmpeg.StandardError;
字符串行; ((line = reader.ReadLine())!= null)

Debug.WriteLine(line);

$ b $ catch(异常异常)
{
Debug.WriteLine(exception.ToString());
return;
}

ffmpeg.Close();
}


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?

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