English 中文(简体)
C# and FFmpeg preferably without shell commands?
原标题:
  • 时间:2009-11-10 12:13:26
  •  标签:
  • c#
  • ffmpeg

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?

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.

最佳回答

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

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.

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 ();
        }
}
问题回答

There s a wrapper library over FFmpeg for .NET.

I have found this post few weeks ago when I was looking for answer for my problem. I tried to start ffmpeg process and pass arguments to it but it take sooo long to do everything. At this point I use Xabe.FFmpeg as it doing this out of the box and don t have to worry about ffmpeg executables because have feature to download latest version.

bool conversionResult = await new Conversion().SetInput(Resources.MkvWithAudio)
  .AddParameter(String.Format("-f image2pipe -i pipe:.bmp -maxrate {0}k -r {1} -an -y {2}",bitrate, fps, outputfilename))
  .Start();

There is documentation available here that shows how to get current percent of conversion.

I just found fflib at sourceforge. Looks pretty promising, haven t used it though.

How about writing a C++/CLI wrapper around ffmpeg s native interface and then calling your wrapper interface from your application?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签