与.NET程序中的ffmpeg进行交互 - 写入输入 [英] Interact with ffmpeg from a .NET program - Write Input

查看:1713
本文介绍了与.NET程序中的ffmpeg进行交互 - 写入输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 问题,您可以看到我设法运行和接收数据来自程序。



但是,我没有设法提交数据,例如转换文件时,按 q 立即停止转换并停止程序。

我需要我的应用程序来支持停止进程,我认为这应该通过将此参数传递到ffmpeg应用程序,因为我要让它照顾所有未收集的资源,或者如果我只是去使用 process.Kill()



这是我试过的:

  static int lineCount = 0; 
static bool flag;
static void process_ErrorDataReceived(object sender,DataReceivedEventArgs e)
{
Console.WriteLine(Error({1:m:s:fff}:{0}),lineCount ++,
DateTime.Now);

如果(e.Data!= null&& string.Equals(e.Data,按[q]停止,[?]寻求帮助))
flag =真正;

if(flag)
{
flag = false;
Console.WriteLine(Stopping({0:m:s:fff})...,DateTime.Now);
process.CancelErrorRead();
process.CancelOutputRead();
process.StandardInput.WriteLine(q);
}

Console.WriteLine(e.Data);
Console.WriteLine();
}

但它没有做任何事情,似乎一旦转换被请求,我再也没有任何控制,我只能收到它的输出。作为独立运行它允许我当然的交互。



我在这里缺少什么,提交输出或上一个答案中的代码是一个不同的技巧是错误的或者我应该选择不同的方法?



请注意, RedirectStandardInput 已打开。



注意:你可以在 answer 我以前的问题,ffmpeg的交互方式不一样,我认为知道答案的人(或许我错了)有ffmpeg经验的人。

解决方案

使用 WriteLine('q'); 而不是 Write ');



:)



我试图从cygwin运行ffmpeg bash shell,看到我不得不在'q'之后键入一个输入。所以....

  static void process_ErrorDataReceived(object sender,DataReceivedEventArgs e)
{
控制台。 WriteLine(Error line:{0}({1:m:s:fff}),lineCount ++,
DateTime.Now);
Console.WriteLine(e.Data);
Console.WriteLine();
if(lineCount == 5)
process.StandardInput.WriteLine(q);
}

使用股票 c:\Documents and Settings \All Users\Documents\My Music\Sample Music\Beethoven's Symphony No. 9(Scherzo).wma




  • 没有 process.StandardInput.WriteLine 它在stderr上打印61行,在stdout上打印1行,并创建1212457字节的mp3文件。

  • 添加退出,它在stderr上打印较少的行数,stdout上没有任何内容,更小的mp3文件。



请注意,它会离开mp3文件。



所以那里。



编辑



看到您的评论后,您已经尝试过这个..



我刚刚重新检查。 X- 20045 X-



首先我重试了我所看到的,发现在第五行发送q\\\
创建一个更小的文件,尽管在不同的运行中略有不同的大小 - 在160K到220K之间。



接下来我评论了 Console.WriteLine 希望它会使ffmpeg退出更快。相反,ffmpeg现在没有停止,并创建完全相同的字节数,1,212,457字节的完整文件。这种行为与您的观察一致



最后,使用 WriteLines ,我抽取在第五行之后,每行的q\\\
stderr 大惊喜记录40个错误行后,

 未处理的异常:System.InvalidOperationException:StandardIn未被重定向。 
在System.Diagnostics.Process.get_StandardInput()
在StandAlone.Program.process_ErrorDataReceived(Object sender,DataReceivedEventArgs e)在C:\ [..] \StandAlone\Program.cs:line 171
在System.Diagnostics.Process.ErrorReadNotifyUser(String data)

不重定向吗? 闻起来像一个错误。



由asker更新:



这里(静态)解决了我的问题,我从一个非官方的网站显然使用了一些构建。


In reference to this question, as you can see I managed to run and receive data from the program.

However I didn't manage to submit data to it, for instance, while converting a file, pressing q immediately stop conversion and stops the program.
I need my application to support stopping the process as well, and I think this should be done by passing this parameter to the ffmpeg app, since I want it to take care of all uncollected resource or whatever dust it would leave behind if I would just go and use process.Kill()

Here is what I've tried:

static int lineCount = 0;
static bool flag;
static void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine("Error ({1:m:s:fff}: {0})", lineCount++,
      DateTime.Now);

  if (e.Data != null && string.Equals(e.Data,"Press [q] to stop, [?] for help")) 
    flag = true;

  if (flag)
  {
    flag = false;
    Console.WriteLine("Stopping ({0:m:s:fff})...", DateTime.Now);
    process.CancelErrorRead();
    process.CancelOutputRead();
    process.StandardInput.WriteLine("q");
  }   

  Console.WriteLine(e.Data);
  Console.WriteLine();
}

But it doesn't do anything, seems that once the conversion has been requested, I have no control on it any more, I can only receive output from it. Running it as stand alone does allow me interaction of course.

What am I missing here, is it a different trick in submitting the output or the code in previous answer is wrong, or I should have chosen a different approach?

For your attention, RedirectStandardInput is on.

NOTE: as you can see in the answer of my previous question, ffmpeg interacts differently, I think the one who knows the answer will be (maybe I'm wrong) someone with experience in ffmpeg.

解决方案

Use WriteLine('q'); instead of Write('q');.

:)

I tried to run ffmpeg from cygwin bash shell and saw that I had to type an enter after 'q'. So....

    static void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine("Error line: {0} ({1:m:s:fff})", lineCount++,
            DateTime.Now);
        Console.WriteLine(e.Data);
        Console.WriteLine();
        if (lineCount == 5)
            process.StandardInput.WriteLine("q");
    }

With the stock c:\Documents and Settings\All Users\Documents\My Music\Sample Music\Beethoven's Symphony No. 9 (Scherzo).wma

  • without the process.StandardInput.WriteLine it prints 61 lines on stderr, 1 line on stdout and creates mp3 file of 1212457 bytes.
  • add the quit and it prints less number of lines on stderr, nothing on stdout and a much smaller mp3 file.

Note that it does leave the mp3 file around.

So there.

EDIT

After seeing your comment that you already tried this..

I just rechecked. The behavior is strange.

First I retried what I had and saw that sending "q\n" on the 5th line to sterr creates a much smaller file, though slightly different sizes in different runs -- between 160K and 220K.

Next I commented out Console.WriteLine hoping it will make ffmpeg quit faster. On the contrary, ffmpeg now did not stop at all and created the full file with exact same byte count, 1,212,457 bytes. This behavior is consistent with your observation

Finally, with WriteLines in place, I pumped "q\n" on every line to stderr after the fifth. Big surprise! After logging 40 error lines,

Unhandled Exception: System.InvalidOperationException: StandardIn has not been redirected.
   at System.Diagnostics.Process.get_StandardInput()
   at StandAlone.Program.process_ErrorDataReceived(Object sender, DataReceivedEventArgs e) in C:\[..]\StandAlone\Program.cs:line 171
   at System.Diagnostics.Process.ErrorReadNotifyUser(String data)

Not redirected? And you are telling me after I sent 35 lines to its input?

Something is not quite right...smells like a bug.

Update by asker:

Updating the windows builds from here (static) solved my issue, I used some builds from an unofficial website apparently.

这篇关于与.NET程序中的ffmpeg进行交互 - 写入输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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