您的位置:首页 > 编程语言 > C#

用C#调用ffmpeg实现媒体类型转换(1)

2008-08-31 20:13 615 查看
ffmpeg是一个视频转换利器,可是它是一个控制台程序,界面不是很友好。我准备用C#给它些个界面。

第一个需要解决的问题是要在C#中调用ffmpeg,代码如下:

class Program

{

static void Main(string[] args)

{

ExcuteProcess("ffmpeg.exe", "-y -i 1.flv 1.mp3", (s, e) => Console.WriteLine(e.Data));

}

static void ExcuteProcess(string exe, string arg, DataReceivedEventHandler output)

{

using (var p = new Process())

{

p.StartInfo.FileName = exe;

p.StartInfo.Arguments = arg;

p.StartInfo.UseShellExecute = false; //输出信息重定向

p.StartInfo.CreateNoWindow = true;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.RedirectStandardOutput = true;

p.OutputDataReceived += output;

p.ErrorDataReceived += output;

p.Start(); //启动线程

p.BeginOutputReadLine();

p.BeginErrorReadLine();

p.WaitForExit(); //等待进程结束

}

}

}

这个其实是一个通用的在C#中调用外部程序的方法,并不局限于ffmpeg,需要注意的是,由于要获取ffmpeg转换过程中的输出信息,故需要把它的输出信息重定向一下。

代码非常基础,就不多介绍了,关于ffmpeg的命令行参数请参看相关说明文档。下一篇文章介绍一下如何获取媒体信息和转换进度,来实现一个媒体转换器的ui。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐