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

查看:130
本文介绍了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?

通过shell调用命令的问题是我不确定你可以做一些进度条等等...或者你可以吗?

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命令。传递一个长长的字符串是非常繁琐的大写。

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.

读取输出的示例代码如下。你必须忽略不以'frame'开头的任何行,也许使用 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天全站免登陆