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

使用C#和ffmpeg将wma流转换为mp3流,c#,process,ffmpeg,mp3,wma,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(

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


谢谢

看起来您希望将WMA文件作为输入,并创建一个MP3文件作为输出。而且,看起来您希望通过stdin/stdout来实现这一点。我刚刚从命令行测试了这一点,它似乎起了作用:

ffmpeg -i - -f mp3 - < in.wma > out.mp3
“-f mp3”指定mp3位流容器,但“-acodec LIBSPEX”指定Speex音频。我很确定你不想要这个。省去该选项,FFmpeg将只为MP3容器使用MP3音频。另外,你确定要重新采样到16000赫兹吗,单声道?因为“-ac1-ar16000”选项就是这么做的


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

也有同样的问题

作为输入,您可以直接包含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:我可以用这个标准代码片段从url转换“live”,但是“ASP”是伪代码,还没有尝试过

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.
}