【问题标题】:asp.net ffmpeg video encoding hangsasp.net ffmpeg 视频编码挂起
【发布时间】:2010-10-07 16:43:37
【问题描述】:

我有以下方法可以使用 ffmpeg 对上传到网站的视频进行编码。它适用于高达 8-9 MB 的视频,但如果视频大小大于 8-9 MB,它会挂起网站。恢复它的唯一方法是重新启动 iis。

当我观看该过程时,我可以看到 ffmpeg 对视频进行编码并退出。结果视频很好。一旦 ffmpeg 存在,问题就开始了。

应用程序在win2003 x86 webserver iis6上运行

有人有使用 ffmpeg 从 asp.net Web 应用程序编码大文件的经验吗?

public EncodedVideo EncodeVideo(VideoFile input, string encodingCommand, string outputFile)
    {
        EncodedVideo encoded = new EncodedVideo();
        Params = string.Format("-i \"{0}\" {1} \"{2}\"", input.Path, encodingCommand, outputFile);
        //string output = RunProcess(Params);
        string output = RunProcessLargeFile(Params);
        encoded.EncodingLog = output;
        encoded.EncodedVideoPath = outputFile;

        if (File.Exists(outputFile))
        {
            encoded.Success = true;
        }
        else
        {
            encoded.Success = false;
        }
        //System.Web.HttpContext.Current.Response.Write(Params);
        return encoded;

    }

private string RunProcessLargeFile(string Parameters)
    {
        /* The below will be the right solution ....
         * The while loop which reads the stream is very improtant 
         * for FFMPEG as .NET does not provide more memory to FFMPEG. 
         * When converting large files, FFMPEG's out put stream gets filled...
         * And waits for .NET to allocate memory resources but is never done. 
         * In order to utilize less memory, we are clearing the buffer periodically.
         **/

        ProcessStartInfo oInfo = new ProcessStartInfo(this.FFmpegPath, Parameters);
        oInfo.WorkingDirectory = Path.GetDirectoryName(this.FFmpegPath);
        oInfo.UseShellExecute = false;
        oInfo.CreateNoWindow = true;
        oInfo.RedirectStandardOutput = true;
        oInfo.RedirectStandardError = true; 
        Process proc = System.Diagnostics.Process.Start(oInfo);
        StreamReader srOutput = proc.StandardError;
        System.Text.StringBuilder output = new System.Text.StringBuilder();

        StreamReader objStreamReader = proc.StandardError;
        System.Text.StringBuilder sbOutPut = new StringBuilder();

        while (!proc.WaitForExit(1000))
        {
            sbOutPut.Append(objStreamReader.ReadToEnd().ToString());
        }

        if (proc.ExitCode == 0)
        {
            proc.Close();
            if (objStreamReader != null)
            {
                objStreamReader.Close();
            }
        }
        else
        {
            proc.Close();
            if (objStreamReader != null) objStreamReader.Close();
        }

        return sbOutPut.ToString();
    }

【问题讨论】:

    标签: c# asp.net ffmpeg


    【解决方案1】:

    闻起来像典型的死锁。

    您在proc.StandardError.ReadToEnd 方法之前调用proc.WaitForExit 似乎会导致死锁。

    来自MSDN的参考:

    如果 父进程调用 WaitForExit 在 StandardOutput.ReadToEnd 和之前 子进程写入足够的文本 填充重定向的流。这 父进程将无限期等待 让子进程退出。这 子进程将无限期等待 让家长阅读全文 标准输出流。

    因此,将您的 while 循环替换为:

    string output = objStreamReader.ReadToEnd();
    proc.WaitForExit();
    

    应该可以解决你的问题。

    【讨论】:

      【解决方案2】:

      我不是 asp.net 的人,我使用 php,在 php.ini 这是一个配置文件,我可以设置我能够上传的最大兆字节数。也许如果您检查配置文件以查看有关上传的任何信息并将其设置为最大值。

      【讨论】:

      • 这与上传无关
      猜你喜欢
      • 2011-07-04
      • 1970-01-01
      • 2017-11-15
      • 2015-02-23
      • 2011-04-15
      • 2014-03-15
      • 2011-10-30
      • 2018-02-12
      • 2013-12-14
      相关资源
      最近更新 更多