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

是否可以实时将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 

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

 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 – ”。

刚遇到同样的问题。

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

不要忘记包含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’的东西是伪代码,还没试过。