如何从MP4文件中提取音频并将其转换为C#中的FLAC文件? [英] How to extract just the audio from an MP4 file and convert it to FLAC file in C#?

查看:228
本文介绍了如何从MP4文件中提取音频并将其转换为C#中的FLAC文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是编写C#,将Microsoft LYNC会议音频转换成文本。
这里是我的项目到目前为止。最初我试图从麦克风录制,保存到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 可执行文件,并将其复制到项目/解决方案中的文件夹中。

将您的音频文件转换为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();
}

这篇关于如何从MP4文件中提取音频并将其转换为C#中的FLAC文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆