用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。
第一个需要解决的问题是要在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。
相关文章推荐
- 用C#调用ffmpeg实现媒体类型转换(1)
- 用C#调用ffmpeg实现媒体类型转换
- C#调用VC的DLL的接口函数参数类型转换一览表
- ASP.NET下调用ffmpeg与mencoder实现视频转换截屏
- C#调用dll时的类型转换总结
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- C#调用cmd用ffmpeg实现视频截图,图片使用后删除
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#调用VC的DLL的接口函数参数类型转换一览表
- C#通过IConvertible接口来实现自定义类型转换和计算
- C#调用C++的DLL搜集整理的所有数据类型转换方式 (转)
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#中使用强制类型实现字符串和ASCII码之间的转换
- C#调用dll(C++(Win32))时的类型转换总结(转)
- C#调用PMAC运动控制卡的pcomm32动态链接库的数据类型转换
- ASP.NET下调用ffmpeg与mencoder实现视频转换截屏
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#调用dll时的类型转换总结
- C#调用VC的DLL的接口函数参数类型转换一览表
- C#调用VC的DLL的接口函数参数类型转换一览表