c# - 为什么从我的网络应用程序转换视频时 ffmpeg 永远不会完成?

标签 c# asp.net multithreading ffmpeg

我正在尝试在用户提交表单时转换视频。它似乎转换正常,但当我尝试对它做任何事情时,文件“正在被另一个进程使用”。看起来 ffmpeg.exe 永远不会退出。我的代码如下,我应该做些什么来让进程释放文件?如果我手动运行它,它会正常退出。

internal class ConversionUtility : Utility
{
    public void Convert(string videoFileName)
    {
        var video = new VideoFile(videoFileName);

        if (!video.infoGathered)
            GetVideoInfo(video);

        var Params = string.Format("-y -i \"{0}\" -coder ac -me_method full -me_range 16 -subq 5 -sc_threshold 40 -vcodec libx264 -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -g 250 -r 20 \"{1}\"", video.Path, Path.ChangeExtension(videoFileName,".mp4"));
        //var Params = string.Format("-y -i \"{0}\" -acodec libfaac -ar 44100 -ab 96k -coder ac -me_method full -me_range 16 -subq 5 -sc_threshold 40 -vcodec libx264 -s 1280x544 -b 1600k -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -i_qfactor 0.71 -keyint_min 25 -b_strategy 1 -g 250 -r 20 c:\\output3.mp4", video.Path, videoFileName);
        //var Params = String.Format(" {0} \"{1}\"",this.FFmpegLocation, video.Path);

        var threadStart = new ParameterizedThreadStart(del => RunProcess(Params));
        var thread = new Thread(threadStart);
        thread.Start();            
        //RunProcess(Params);
    }
}

internal class Utility
{
    public string FFmpegLocation { get; set; }        
    private string WorkingPath { get { return Path.GetDirectoryName(FFmpegLocation); } }

    protected string RunProcess(string Parameters)
    {
        //create a process info
        var oInfo = new ProcessStartInfo(this.FFmpegLocation, Parameters)
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            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.WaitForExit();
            //if (!proc.WaitForExit(10000))
            //    proc.Kill();


            //get the output
            srOutput = proc.StandardError;

            //now put it in a string
            output = srOutput.ReadToEnd();

            proc.Close();
        }
        catch (Exception)
        {
            output = string.Empty;
        }
        finally
        {
            //now, if we succeded, close out the streamreader
            if (srOutput != null)
            {
                srOutput.Close();
                srOutput.Dispose();
            }
        }
        return output;
    }

最佳答案

经过大量研究后,我发现了这个 post .这让我走上了正确的轨道。我阅读了更多关于 RedirectStandardError 的内容发现我需要改变我打电话的顺序。最后调用 WaitForExit 后,一切都按预期进行。这是更新后的代码:

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

    var output = string.Empty;

    try
    {                
        Process process = System.Diagnostics.Process.Start(oInfo);

        output = process.StandardError.ReadToEnd();
        process.WaitForExit();
        process.Close();
    }
    catch (Exception)
    {
        output = string.Empty;
    }                       
    return output;
}

关于c# - 为什么从我的网络应用程序转换视频时 ffmpeg 永远不会完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7577855/

相关文章:

c# - ASP.NET 和 jQuery 日历 - 不起作用

Java长时间运行线程会导致内存泄漏吗?

c# - 使用 WPF 以编程方式更改语言/资源

c# - 无法将简单的 XML 解析为对象?

c# - 在 C# 中对这种行为的解释是什么

javascript - Asp.net避免重新加载整个页面

c# - 来自 C# 的 Runas/NETONLY

c# - 使用 Kentor.AuthServices.StubIdp 作为生产 IDP

java - 线程安全: Does incrementing values of a primitive wrapper okay?

java - 强制方法调用 null 变量以抛出 NullPointerException