【问题标题】:ffmpeg's strange behaviour-Conversion started when I close my applicationffmpeg 的奇怪行为 - 当我关闭我的应用程序时开始转换
【发布时间】:2014-01-03 11:35:29
【问题描述】:

我在我的应用程序中使用 ffmpeg 来旋转视频,但问题是当我开始转换时,ffmpeg 只显示它的版本信息而不开始实际的转换,但是当我关闭我的应用程序时,ffmpeg 进程仍然在任务栏中的运行进程中并开始转换文件。

ffmpeg 输出

这是我的代码,请告诉我哪里做错了。

void ConvertVideo(object[] arr) {

    string Argument = (string)arr[0];
    string OutputFolder = (string)arr[1];
    string ConvertedFile = (string)arr[2];


    UpdateStatus("Converting! Please wait...");
    ffmpeg = new Process();
    ffmpeg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    ffmpeg.StartInfo.FileName = "ffmpeg.exe";
    ffmpeg.StartInfo.UseShellExecute = false;
    ffmpeg.StartInfo.RedirectStandardError = true;
    ffmpeg.StartInfo.RedirectStandardOutput = true;
    ffmpeg.StartInfo.CreateNoWindow = true;
    ffmpeg.StartInfo.Arguments = Argument;
    ffmpeg.Start();
    myStreamReader = ffmpeg.StandardError;
    outputLine = myStreamReader.ReadLine();
    UpdateRTB(outputLine);//Write line to ritchtextbox
    do
    {
        if (outputLine.Contains("muxing overhead"))
        {
            UpdateStatus("Muxing video");
        }

        if (outputLine.StartsWith("frame"))
        {
            UpdateStatus("Converting video");
        }
    }
    while (!(ffmpeg.HasExited & (string.Compare(outputLine, "") == 0 | outputLine == null)));
    ffmpeg.Close();
    myStreamReader.Close();
    UpdateStatus("Convertion completed successfully");
}

【问题讨论】:

    标签: c# process ffmpeg


    【解决方案1】:

    看起来您没有初始化 stdout 的重定向,并且您只从 StandardError 流中读取一次。您很可能会因为 stdout 或 stderr 流充满未处理的数据而导致进程停滞。

    如果将处理程序连接到ffmpeg 进程对象上的OutputDataReceivedErrorDataReceived 事件并在其上调用BeginOutputReadLineBeginErrorReadLine,则可以从进程中获取异步输出事件。使用这些来更新您的显示等。将处理程序连接到Exited 事件以发出退出信号,您可以异步执行整个操作。

    对于同步转换,试试这个:

    void ConvertVideo(object[] arr) {
    
        string Argument = (string)arr[0];
        string OutputFolder = (string)arr[1];
        string ConvertedFile = (string)arr[2];
    
    
        UpdateStatus("Converting! Please wait...");
        using (ffmpeg = new Process())
        {
            ffmpeg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            ffmpeg.StartInfo.FileName = "ffmpeg.exe";
            ffmpeg.StartInfo.UseShellExecute = false;
            ffmpeg.StartInfo.RedirectStandardError = true;
            ffmpeg.StartInfo.RedirectStandardOutput = true;
            ffmpeg.StartInfo.CreateNoWindow = true;
            ffmpeg.StartInfo.Arguments = Argument;
    
            ffmpeg.OutputDataReceived += ffmpeg_OutputDataReceived;
            ffmpeg.ErrorDataReceived += ffmpeg_ErrorDataReceived;
    
            ffmpeg.Start();
            ffmpeg.BeginErrorReadLine();
            ffmpeg.BeginOutputReadLine();
    
            ffmpeg.WaitForExit();
        }
    
        UpdateStatus("Convertion completed successfully");
    }
    
    public void ffmpeg_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        // ignore data from stdout?
    }
    
    public void ffmpeg_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        UpdateRTB(e.Data);
    
        if (e.Data.Contains("muxing overhead"))
            UpdateStatus("Muxing video");
    
        if (e.Data.StartsWith("frame"))
            UpdateStatus("Converting video");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 2017-01-02
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多