【问题标题】:Convert wma stream to mp3 stream with C# and ffmpeg使用 C# 和 ffmpeg 将 wma 流转换为 mp3 流
【发布时间】:2012-07-30 19:29:22
【问题描述】:

是否可以将 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# 解决方案(如果存在)开放。

谢谢

【问题讨论】:

    标签: c# process ffmpeg mp3 wma


    【解决方案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 -”。

    【讨论】:

      【解决方案2】:

      刚刚遇到同样的问题。

      作为输入,您可以直接包含您的 URL。作为输出,您使用代表标准输出的-。此解决方案适用于 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:我可以使用这个标准输出 sn-p 从 url 转换 'live',但是 'ASP' 的东西是伪代码,还没有尝试过。

      【讨论】:

        猜你喜欢
        • 2013-10-25
        • 2012-11-02
        • 1970-01-01
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        • 2011-10-02
        相关资源
        最近更新 更多