如何捕获FFMPEG输出 [英] How to catch FFMPEG Output

查看:139
本文介绍了如何捕获FFMPEG输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我试过下一行:



过程p =  new  Process(); 
p.StartInfo.UseShellExecute = false ;
p.StartInfo.RedirectStandardOutput = true ;
p.StartInfo.CreateNoWindow = true ;
p.StartInfo.FileName = ffmpeg;
p.StartInfo.Arguments = -h;
p = Process.Start(p.StartInfo);
this .textBox1.Text = p.StandardOutput.ReadToEnd();
this .textBox1.Update();
p.WaitForExit();





在这种情况下,一切看起来都很好,TextBox的输出为ffmpeg.exe。

但是在实际工作的情况下:

 p.StartInfo.Arguments =   -i input.avi -y output.ts; 



我只能看到空文本框convertion已经完成并且output.ts已经创建。



提前谢谢。

解决方案

文章中发现了最好和最短的解决方案

'由Jonathan Lathigee发布的ffmpeg'视频转码YouTube风格。

使用ffmpeg 视频转码YouTube风格

他写道:

另一个问题是从FFMPEG捕获输出以获取所有文件的元数据。我创建了一个新进程和streamReader,从进程中捕获StandardOutput。没有。原来在FFMPEG中没有特定命令从视频文件中读取元数据,而当调用FFMPEG时没有指定输出文件,此信息作为StandardError流出现。一旦切换从捕获StandardOutput到StandardError,我没有遇到任何问题。



这对我来说是关键因素。当我试图捕获ffmpeg的StandardOutput而不是捕获StandardError时。所以我在TextBox中获得了所有必要的行。最后我使用了更适合我的目的的异步阅读。这是代码。



 尝试 
{
处理p = .process1;
字符串 input = -i + this .listBox2.Text; // / c ffmpeg.exe //用于通过cmd.exe启动ffmpeg
String size = -s + .comboBox1.Text;
字符串 codec = -vcodec + .comboBox2.Text;
字符串编码器= -vtag + .comboBox3.Text;
字符串 quality = -qscale + .comboBox4.Text;
字符串 videobr = -b:v + .comboBox4.Text + K;
字符串 audiobr = -ab + this .comboBox5.Text + k ;
字符串 output = -y + this .textBox2.Text;
字符串 report = 2> \ + .textBox2.Text + .log\; // 用于通过cmd.exe启动ffmpeg并将输出放入文件
字符串 s = input + size + codec + encoder + videobr + audiobr + output; // + report;
字符串 sa = -h; // 来自ffmpeg的帮助
p.StartInfo.Arguments = s;
p.Start();

p.BeginOutputReadLine();
p.BeginErrorReadLine();
MessageBox.Show( 等待进程退出....);
// p.WaitForExit();
if (p.HasExited){p.CancelErrorRead(); p.CancelOutputRead(); p.Close(); MessageBox.Show( Closed); }
}
catch (Exception ex)
{
MessageBox.Show( 1. \ n + ex.Message);
}



关注ErrorDataReceived处理程序。

  private   void  process1_ErrorDataReceived( object  sender,DataReceivedEventArgs e)
{
this .textBox1.Text + = e.Data;
this .textBox1.Update();
// MessageBox.Show(4.Error\\\
+ e.Data);

}





你可以找到一些额外的行:MessageBoxes等它们只是用于调试。

非常感谢Jonathan Lathigee和他的5星。

如果我的经验对你有用,我很高兴。

祝福。


Hello everybody.
I've tried next lines:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "ffmpeg";
p.StartInfo.Arguments = " -h ";
p = Process.Start(p.StartInfo);
this.textBox1.Text = p.StandardOutput.ReadToEnd();
this.textBox1.Update();
p.WaitForExit();



In this case everything looks fine and TextBox have got a output of ffmpeg.exe.
But in case of real job:

p.StartInfo.Arguments = " -i input.avi -y output.ts ";


I can see just empty TextBox although convertion has been completed and output.ts has been created.

Thanks in advance.

解决方案

The best and the shortest solution was found in the article
' Video Transcoding "YouTube-Style" with ffmpeg ' posted by Jonathan Lathigee.
Video Transcoding "YouTube-Style" with ffmpeg
He writes:
"Another problem came in the capturing of output from FFMPEG to get all of the file's metadata. I created a new process and streamReader, and captured the StandardOutput from the process. Nothing. Turns out that there is no specific command in FFMPEG to read metadata from a video file, instead this info comes out as a StandardError stream when FFMPEG is called with no output file specified. Once I switched from capturing the StandardOutput to StandardError, I had no problems."

And that was key factor for me. As I tried to catch StandardOutput of ffmpeg instead of catching StandardError. So I got all necessary lines into my TextBox. Finally I used asynchronous reading that was more suitable for my purpose. Here is the code.

try
            {
                Process p = this.process1;
                String input = " -i " + this.listBox2.Text; //  /c ffmpeg.exe // for launching ffmpeg via cmd.exe
                String size = " -s " + this.comboBox1.Text;
                String codec = " -vcodec " + this.comboBox2.Text;
                String encoder = " -vtag " + this.comboBox3.Text;
                String quality = " -qscale " + this.comboBox4.Text;
                String videobr = " -b:v " + this.comboBox4.Text + "k";
                String audiobr = " -ab " + this.comboBox5.Text + "k";
                String output = " -y " + this.textBox2.Text;
                String report = " 2> \"" + this.textBox2.Text + ".log\""; // for launching ffmpeg via cmd.exe and putting output into file
                String s = input + size + codec + encoder + videobr + audiobr + output;// +report;
                String sa = " -h ";// help from ffmpeg
                p.StartInfo.Arguments = s;
                p.Start();

                p.BeginOutputReadLine();
                p.BeginErrorReadLine();
                MessageBox.Show("Waiting for the process to exit....");
                //p.WaitForExit();
                if (p.HasExited) { p.CancelErrorRead(); p.CancelOutputRead(); p.Close(); MessageBox.Show("Closed"); }
            }
            catch (Exception ex)
            {
                MessageBox.Show("1.\n"+ex.Message);
            }


Following ErrorDataReceived handler.

private void process1_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.textBox1.Text += e.Data;
            this.textBox1.Update();
            //MessageBox.Show("4.Error\n" + e.Data);
        }



You can find some extra lines: MessageBoxes etc. they are just for debugging.
So Great Thanks to Jonathan Lathigee and 5 stars for him.
I'm glad if my experience is usefull for you.
Best wishes.


这篇关于如何捕获FFMPEG输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆