使用C#和ffmpeg将wma流转换为mp3流

时间:2012-08-01 20:29:37

标签: c# process ffmpeg mp3 wma

是否可以实时将wma流转换为mp3流?

我尝试过这样的事,但没有运气:



    WebRequest request = WebRequest.Create(wmaUrl);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    int block = 32768;
    byte[] buffer = new byte[block];

    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.ErrorDialog = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.FileName = "c:\\ffmpeg.exe";
    p.StartInfo.Arguments = "-i - -y -vn -acodec libspeex -ac 1 -ar 16000 -f mp3 ";

    StringBuilder output = new StringBuilder();

    p.OutputDataReceived += (sender, e) =>
    {
      if (e.Data != null)
      {
        output.AppendLine(e.Data);
        //eventually replace this with response.write code for a web request
      }
    };

    p.Start();
    StreamWriter ffmpegInput = p.StandardInput;

    p.BeginOutputReadLine();

    using (Stream dataStream = response.GetResponseStream())
    {
       int read = dataStream.Read(buffer, 0, block);

       while (read > 0)
       {
          ffmpegInput.Write(buffer);
          ffmpegInput.Flush();
          read = dataStream.Read(buffer, 0, block);
       }
    }

    ffmpegInput.Close();

    var getErrors = p.StandardError.ReadToEnd();

只是想知道我想要做什么甚至是可能的(将wma的字节块转换为mp3的字节块)。如果存在,可以使用任何其他可能的C#解决方案。

由于

2 个答案:

答案 0 :(得分:1)

看起来您想要将WMA文件作为输入并创建MP3文件作为输出。此外,看起来你想通过stdin / stdout这样做。我刚刚从命令行测试了它,它似乎工作:

ffmpeg -i - -f mp3 - < in.wma > out.mp3

如果这是您的目标,那么您的命令行会出现一些问题:

p.StartInfo.FileName = "c:\\ffmpeg.exe";
p.StartInfo.Arguments = "-i - -y -vn -acodec libspeex -ac 1 -ar 16000 -f mp3 ";

“ - f mp3”指定MP3比特流容器,但“-acodec libspeex”指定Speex音频。我很确定你不想要这个。省略该选项,FFmpeg将只为MP3容器使用MP3音频。此外,您确定要重新采样到16000 Hz,单声道吗?因为这就是“-ac 1 -ar 16000”选项的作用。

最后一个问题:你没有提供目标。如果要将stdout指定为目标,则可能需要“-f mp3 - ”。

答案 1 :(得分:0)

遇到同样的问题。

作为输入,您可以直接包含您的网址。作为输出,您使用-,它代表标准输出。这个解决方案适用于mp3之间的转换,所以我希望它与视频一样。

不要忘记包含process.EnableRaisingEvents = true;标志。


private void ConvertVideo(string srcURL)
{
    string ffmpegURL = @"C:\ffmpeg.exe";
    DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\");

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = ffmpegURL;
    startInfo.Arguments = string.Format("-i \"{0}\" -ar 44100 -f mp3 -", srcURL);
    startInfo.WorkingDirectory = directoryInfo.FullName;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardError = true;
    startInfo.CreateNoWindow = false;
    startInfo.WindowStyle = ProcessWindowStyle.Normal;

    using (Process process = new Process())
    {
        process.StartInfo = startInfo;
        process.EnableRaisingEvents = true;
        process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
        process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
        process.Exited += new EventHandler(process_Exited);

        try
        {
            process.Start();
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
            process.WaitForExit();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            process.ErrorDataReceived -= new DataReceivedEventHandler(process_ErrorDataReceived);
            process.OutputDataReceived -= new DataReceivedEventHandler(process_OutputDataReceived);
            process.Exited -= new EventHandler(process_Exited);

        }
    }
}

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        byte[] b = System.Text.Encoding.Unicode.GetBytes(e.Data);
        // If you are in ASP.Net, you do a 
        // Response.OutputStream.Write(b)
        // to send the converted stream as a response
    }
}


void process_Exited(object sender, EventArgs e)
{
    // Conversion is finished.
    // In ASP.Net, do a Response.End() here.
}

PS:我可以使用此stdout片段从网址转换“live”,但“ASP”内容是伪代码,尚未尝试过。