Question

Using ffmpeg, I would like to know if it's possible to convert mp3 bitrate as data chunks are received?

That means I would send slowly chunks to ffmpeg so that it outputs a mp3 with another bitrate.

So in very-pseudo-code, it looks like it:

  1. MP3 Request from user

  2. Send the default mp3 to ffmpeg with parameters to convert to the desired bitrate.

  3. As it's writing a new file, write what as been writen so far in the Response outputstream (I'm in ASP.Net)

Is that feasable or I need to switch to another technology?

[EDIT]

For now, I'm trying a solution like this: Convert wma stream to mp3 stream with C# and ffmpeg

[EDIT 2]

I answered my question, and it is feasible with an url as input and standard output as output. Using an url allows to process a file chunk by chunk, and using stdout, we can access data while it is processed.

Was it helpful?

Solution

Here is the method in C#, read on http://jesal.us/2008/04/how-to-manipulate-video-in-net-using-ffmpeg-updated/ and changed to work from stream to stream. That means "Live" conversion of streams with ffmpeg.

The '-' at the ends of the command stands for "Standard Output", so that's why it's the destination.

    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.
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top