7

ffmepgを使用してメディア ファイル変換用の .NET ラッパーを作成しようとしています。

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 を使用していることを知る必要さえありませんが、変換の進行状況の詳細と、アプリがどのくらいのパーセンテージに達しているかなどはまだわかっています。

基本的に、P/Invoke から変換関数への .NET ラッパーのみに満足しています (PI 関数を抽出できない限り、外部ライブラリ全体には興味がありません)。

ffmpeg と .NET の経験がある人はいますか?

更新 私の追加の質問、how to write input to a running ffmpeg process をご覧ください。

4

2 に答える 2

7

答えは次のとおりです。

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!");
}
于 2011-09-08T15:16:34.667 に答える
0

ffmpeg-sharpを使用してみてください。

于 2011-09-04T01:36:05.717 に答える