Using ffmpeg in asp.net

2019-08-09 11:15发布

I needed a audio conversion library. After already pulling my hair..I have given up on the fact that there is no such audio library out there..every library out there has some or the other problem.

The only option left is ffmpeg which is the best but unfortunately you cannot use it in asp.net (not directly I mean). Every user on the website that will convert a file; will launch an exe?; I think I will hit the server memory max soon.

Bottom Line: I will try using ffmpeg.exe and see how many users it can support simultaneously.

I went to the ffmpeg website and in the windows download section I found 3 different version; static, shared and dev.

Does any one know which would be the best? All packed in one exe (static) or dll's separely and exe small, wrt using it in asp.net?

PS: any one has a good library out there..would be great if you can share.

Static builds provide one self-contained .exe file for each program (ffmpeg, ffprobe, ffplay).

Shared builds provide each library as a separate .dll file (avcodec, avdevice, avfilter, etc.), and .exe files that depend on those libraries for each program

Dev packages provide the headers and .lib/.dll.a files required to use the .dll files in other programs.

1条回答
Summer. ? 凉城
2楼-- · 2019-08-09 11:39

ffMpeg is the best library out there from what I have used but I wouldn't recommend trying to call it directly from asp.net.

What I have done, is accepted the upload, stored it on the server, or S3 in my case, then have a worker role (if using something like Azure) and a process that continuously looks and monitors for new files to convert.

If you needed a realtime like solution, you could update flags in your database and have an AJAX solution to poll the database to keep providing progress updates, then a link to download once the conversion is complete.

Personally my approach would be

  1. Azure Web Roles
  2. Azure Worker Role
  3. ServiceBus

The WorkerRole starts up and is monitoring the ServiceBus Queue for messages.

The ASP.NET site uploads and stores the file in S3 or Azure The ASP.NET site then records information in your DB if needed and sends a message to the ServiceBus queue.

The WorkerRole picks this up and converts.

AJAX will be needed on the ASP.NET site if you want a realtime monitoring solution. Otherwise you could send an email when complete if needed.

Using a queuing process also helps you with load as when you are under heavy load people just wait a little longer and it doesn't grind everything to a halt. Also you can scale out your worker roles as needed to balance loads, should it ever become too much for one server.

Here is how I run ffMpeg from C# (you will need to change the parameters for your requirements)

String params = string.Format("-i {0} -s 640x360 {1}", input.Path, "C:\\FilePath\\file.mp4");

RunProcess(params);

private string RunProcess(string Parameters)
    {
        //create a process info
        ProcessStartInfo oInfo = new ProcessStartInfo(this._ffExe, Parameters);
        oInfo.UseShellExecute = false;
        oInfo.CreateNoWindow = true;
        oInfo.RedirectStandardOutput = true;
        oInfo.RedirectStandardError = true;

        //Create the output and streamreader to get the output
        string output = null; StreamReader srOutput = null;

        //try the process
        try
        {
            //run the process
            Process proc = System.Diagnostics.Process.Start(oInfo);
            proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived);
            proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);

            proc.BeginOutputReadLine();
            proc.BeginErrorReadLine();

            proc.WaitForExit();

            proc.Close();
            proc.Dispose();
        }
        catch (Exception)
        {
            // Capture Error
        }
        finally
        {
            //now, if we succeeded, close out the streamreader
            if (srOutput != null)
            {
                srOutput.Close();
                srOutput.Dispose();
            }
        }



        return output;
    }
查看更多
登录 后发表回答