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

查看:18
本文介绍了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命令的框架的最佳方法.传递一大串很长的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 所做的就是写一行后跟 .这就是为什么您看不到多行的原因.但是,在程序的错误输出上使用 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 . 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天全站免登陆