C#和FFmpeg的最好不shell命令? [英] C# and FFmpeg preferably without shell commands?

查看:85
本文介绍了C#和FFmpeg的最好不shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用FFmpeg的一个视频文件从我的C#程序中进行转换。我知道我可以调用一个shell命令的,但有没有更好的办法?

I'd like to be able to use FFmpeg to convert a video file from within my C# program. I know I can just call a shell command, but is there a better way?

通过外壳调用命令的问题,是我不知道,你可以做这样的事情的进度条,等...或者可以这样呢?

The issue with invoking a command via the shell, is I'm not sure you could do things like a progress bar, etc... or could you?

如果没有办法,任何人都可以建议最好方式布局执行shell命令的一些框架。路过一个大的长字符串是很繁琐的ATM。

If there isn't a way, can anyone suggest the best way to layout some framework for executing shell commands. Passing one big long string is very cumbersome atm.

推荐答案

您可以轻松如果运行的ffmpeg实现一个进度条。运行时的ffmpeg的输出是这样的:

You can easily implement a progress bar if running ffmpeg. The output of ffmpeg while running is something like:

frame= 3366 fps=465 q=31.0 Lsize=    6474kB time=140.35 bitrate= 377.9kbits/s

和它刷新〜每秒两次。您可以分析该行并得到你需要显示进度数据。当你在命令行中运行,你只能看到一行正在更新所有的时间,但什么的ffmpeg确实是写的线,然后按 \r 。这就是为什么你没有看到多行。然而,在程序的错误输出使用StreamReader.ReadLine()的时候,你会得到每次更新一行。

And it is refreshed ~twice per second. You can parse that line and get the data you need to display the progress. When you run in the command line, you only see one line being updated all the time, but what ffmpeg does is to write the line followed by \r. That's why you don't see multiple lines. However, when using StreamReader.ReadLine() on the error output of the program, you get one line for every update.

示例代码读取输出如下。你将不得不忽略不与框架开头的行,或者使用 BeginErrorReadLine() + ErrorDataReceived 如果你想读线是异步的,等等,但你的想法(我实际测试吧):

Sample code to read the output follows. You would have to ignore any line that does not begins with 'frame', perhaps use BeginErrorReadLine()+ErrorDataReceived if you want reading lines to be asynchronous, etc., but you get the idea (I've actually tested it):

using System;
using System.Diagnostics;
using System.IO;

class Test {
        static void Main (string [] args)
        {
                Process proc = new Process ();
                proc.StartInfo.FileName = "ffmpeg";
                proc.StartInfo.Arguments = "-i " + args [0] + " " + args [1];
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                if (!proc.Start ()) {
                        Console.WriteLine ("Error starting");
                        return;
                }
                StreamReader reader = proc.StandardError;
                string line;
                while ((line = reader.ReadLine ()) != null) {
                        Console.WriteLine (line);
                }
                proc.Close ();
        }
}

这篇关于C#和FFmpeg的最好不shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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