zoukankan      html  css  js  c++  java
  • 用C#调用ffmpeg实现媒体类型转换(1)

    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。

  • 相关阅读:
    火狐浏览器推荐插件原创
    vue转义字符转换成普通字符
    vue中关于插槽的使用
    vue调取摄像头的方法(可以直接使用)
    vue调用andriod方法
    vue中格式化时间戳(可直接使用)
    vue关于$router的使用
    轻松了解Spring中的控制反转和依赖注入(二)
    更加优雅地配置Spring Securiy(使用Java配置和注解)
    更加优雅地搭建SSH框架(使用java配置)
  • 原文地址:https://www.cnblogs.com/TianFang/p/1280741.html
Copyright © 2011-2022 走看看