【问题标题】:Convert MP3 bitrate from a stream into another stream with ffmpeg使用 ffmpeg 将 MP3 比特率从一个流转换为另一个流
【发布时间】:2012-09-15 06:59:13
【问题描述】:

使用ffmpeg,我想知道是否可以在收到数据块时转换mp3比特率?

这意味着我会缓慢地将块发送到 ffmpeg,以便它输出具有另一个比特率的 mp3。

所以在非常伪代码中,它看起来像这样:

  1. 来自用户的 MP3 请求

  2. 将默认的 mp3 发送到 ffmpeg,并带有参数以转换为所需的比特率。

  3. 在写入新文件时,将目前已写入的内容写入响应输出流(我在 ASP.Net 中)

这可行还是我需要改用另一种技术?

[编辑]

目前,我正在尝试这样的解决方案:Convert wma stream to mp3 stream with C# and ffmpeg

[编辑 2]

我回答了我的问题,用 url 作为输入,标准输出作为输出是可行的。使用 url 可以逐块处理文件,使用 stdout 可以在处理数据的同时访问数据。

【问题讨论】:

    标签: stream ffmpeg file-conversion


    【解决方案1】:

    这是 C# 中的方法,在 http://jesal.us/2008/04/how-to-manipulate-video-in-net-using-ffmpeg-updated/ 上阅读并更改为从流到流。这意味着使用 ffmpeg 对流进行“实时”转换。

    命令末尾的 '-' 代表“标准输出”,因此它是目的地。

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

    【讨论】:

    • 我注意到输出结果会损坏。输出需要重定向到可以处理字节的东西。字节不能在屏幕上显示,也不能作为字符串显示,因为它包含空字符和 endofline。最好将标准输出重定向到另一个程序的标准输入,该程序将使用 Stream stdin = Console.OpenStandardInput() 读取它并将其输出为 Base64 字符串或 Ascii85 字符串,因此它不再包含空字符或 endofline 字符。跨度>
    猜你喜欢
    • 2017-08-14
    • 2012-07-30
    • 2016-10-12
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多