从 .NET 程序与 ffmpeg 交互? [英] Interact with ffmpeg from a .NET program?

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

问题描述

我正在尝试使用 ffmepg 创建用于媒体文件转换的 .NET 包装器,这就是我的试过:

I'm trying to create a .NET wrapper for media-file conversion using ffmepg, here is what I've tried:

static void Main(string[] args)
{
  if (File.Exists("sample.mp3")) File.Delete("sample.mp3");

  string result;

  using (Process p = new Process())
  {
    p.StartInfo.FileName = "ffmpeg";
    p.StartInfo.Arguments = "-i sample.wma sample.mp3";

    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;

    p.Start();

    //result is assigned with an empty string!
    result = p.StandardOutput.ReadToEnd();

    p.WaitForExit();
  }
}

实际发生的是 ffmpeg 程序的内容打印到控制台应用程序,但 result 变量是一个空字符串.我想以交互方式控制转换进度,因此用户甚至不必知道我正在使用 ffmpeg,但他仍然知道转换进度的详细信息以及应用程序达到的百分比等.

What actually happens is the content of the ffmpeg program is printed out to the Console app, but the result variable is an empty string. I want to control the conversion progress interactively, so the user doesn't even have to know I'm using ffmpeg, but he still knows the conversion progress' details and what percentage etc. the app is up to.

基本上,我也很高兴使用 .NET 包装器来实现 P/Invoke 到转换功能(我对整个外部库不感兴趣,除非我可以从中提取 PI 函数).

Basically I would also be happy with a .NET wrapper for a P/Invoke to conversion function ONLY (I am not interested in a whole external library, unless I can extract the PI function from it).

任何有 ffmpeg & 经验的人.NET?

Anyone with experience in ffmpeg & .NET?

更新请查看我的进一步问题,如何将输入写入正在运行的 ffmpeg 进程.

Update Please view my further question, how to write input to a running ffmpeg process.

推荐答案

答案如下:

static void Main()
{
  ExecuteAsync();
  Console.WriteLine("Executing Async");
  Console.Read();
}

static Process process = null;
static void ExecuteAsync()
{
  if (File.Exists("sample.mp3"))
    try
    {
      File.Delete("sample.mp3");
    }
    catch
    {
      return;
    }

  try
  {
    process = new Process();
    ProcessStartInfo info = new ProcessStartInfo("ffmpeg.exe",
        "-i sample.wma sample.mp3");

    info.CreateNoWindow = false;
    info.UseShellExecute = false;
    info.RedirectStandardError = true;
    info.RedirectStandardOutput = true;

    process.StartInfo = info;

    process.EnableRaisingEvents = true;
    process.ErrorDataReceived +=
        new DataReceivedEventHandler(process_ErrorDataReceived);
    process.OutputDataReceived +=
        new DataReceivedEventHandler(process_OutputDataReceived);
    process.Exited += new EventHandler(process_Exited);

    process.Start();

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
  }
  catch
  {
    if (process != null) process.Dispose();
  }
}

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

static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine("Output Data Received.");
}

static void process_Exited(object sender, EventArgs e)
{
  process.Dispose();
  Console.WriteLine("Bye bye!");
}

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

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