2011-09-04 64 views
5

我试图创建使用ffmepg媒体文件转换一个.NET包装,这里是我已经试过:从.NET程序与ffmpeg交互?

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

下面是答案:

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!"); 
} 
+0

没有使用StringBuilder sb。 –

+0

@aaaa bbbb:删除,谢谢。它仍然从以前的尝试,无论如何,我添加了一些重要的功能,我的答案。 **你能否看看[这](http://stackoverflow.com/questions/7296901)**? – Shimmy

0

尝试使用ffmpeg-sharp

+0

我不想使用其他外部工具。我很想听听如何为转换支持创建一些基本简单的P/Invokes。有没有办法像我的例子那样使用后台进程来执行这样的程序?我错过了什么? – Shimmy

+0

退房http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea8b0fd5-a660-46f9-9dcb-d525cc22dcbd你可以隐藏窗口,但我相信你可以读取输出它仍然。 –

+0

http://stackoverflow.com/questions/7296901 – Shimmy