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

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

我的目标是编写将 Microsoft LYNC 会议音频转换为文本的 C#。 Here 是我目前的项目。最初我试图从麦克风录制,将其保存为 WAV,然后将 WAV 转换为 FLAC 并使用 GoogleSpeechAPI,将 FLAC 转换为文本。但我在将麦克风音频录制为 WAV 格式时遇到了困难。

问题是它需要采用非常特定的 WAV 格式,即 int16 或 int24,WAV 才能使用 WAV 到 FLAC 的转换方法。我一直记录每个样本 8 位,而不是每个样本(16 或 24 位)。

所以,重新开始。 Microsoft Lync 直接录制会议并将其保存为 MP4 格式的视频。如果我能以某种方式编写代码将 MP4 转换为 FLAC,那也将解决我的问题。有代码示例吗?

我最近有一个 ASP.NET MVC 5 应用程序,我需要在其中将 .mp4 转换为 .webm 并成功运行,所以这是一个应用与视频文件相同的概念的想法,但在此例如,它们将是音频文件。

首先,您将下载 FFMPEG 可执行文件并将其复制到 project/solution 中的文件夹中。

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

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

您可以将其包装在 C# 方法中以执行 FFMPEG,如下所示:

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