0

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.

Community
  • 1
  • 1
Léon Pelletier
  • 2,701
  • 2
  • 40
  • 67

1 Answers1

1

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.
    }
Léon Pelletier
  • 2,701
  • 2
  • 40
  • 67
  • I noticed that the output result will be corrupted. The output needs to be redirected into something that can handles bytes. Bytes cannot be shown on the screen, or as string, since it contains null characters and endofline. It is better to redirect the standardoutput into the standardinput of another program that will read it with Stream stdin = Console.OpenStandardInput() and output it as Base64 strings or Ascii85 strings so it doesn't contain null chars or endofline chars anymore. – Léon Pelletier Sep 30 '12 at 21:50