ASP.NET下调用ffmpeg与mencoder实现视频转换截屏

最近要做一个视频播放的系统,用到了ffmpeg和mencoder两个工具,查了一些资料,发现这方面的资料还挺多的,但是就是乱了一点,我自己从头整理了一下,和大家分享一下:

1、ffmpeg实现视频(avi,wmv等格式)转换为flv格式:

/// <summary>
    /// 转换视频为flv
    /// </summary>
    /// <param name="fileName">上传视频文件的路径(原文件)</param>
    /// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
    /// <returns>成功:返回1 失败:0</returns>
    public int FChangeFileVir(string fileName, string playFile)
    {
        //转换后视频高宽
        string widthOfFile = "320";
        string heightOfFile = "240";
        //取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add   key="ffmpeg"   value="E:aspx1ffmpeg.exe"   />   
        string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
        if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
        {
            return 0;
        }

        //获得(.flv)文件相对路径 
        string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
        //建立ffmpeg进程
        System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
        //设置后台运行,不显示窗口
        FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        //运行参数,此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9以上调试通过 
        //ffmpeg -i F:1.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:	est.flv
        FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
        try
        {
            //开始转换
            System.Diagnostics.Process.Start(FilestartInfo);
        }
        catch
        {
            return 0;
        }

        return 1;
    }

2、mencoder实现视频(avi,wmv等格式)转换为flv格式:

    /// <summary>
    /// 转换视频为flv
    /// </summary>
    /// <param name="fileName">上传视频文件的路径(原文件)</param>
    /// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
    /// <returns>成功:返回1 失败:0</returns>
    public int MChangeFileVir(string fileName, string playFile)
    {
        //转换后视频高宽
        string widthOfFile = "320";
        string heightOfFile = "240";
        //取得mencoder.exe的路径,路径配置在Web.Config中,如:<add   key="mencoder"   value="E:aspx1mencoder.exe"   /> 
        string tool = Server.MapPath(PublicMethod.mencodertool);
        //判断mencoder工具与文件是否存在
        if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
        {
            return 0;
        }

        //获得(.flv)文件相对路径 
        string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
        //建立工具进程
        System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
        //设置后台运行
        FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        //运行参数
        FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale=" + widthOfFile + ":" + heightOfFile + " -ofps 12 -srate 22050";
        try
        {
            //开始转换
            System.Diagnostics.Process.Start(FilestartInfo);
        }
        catch
        {
            return 0;
        }

        return 1;
    }

3、ffmpeg实现视频内容:

    //视频截图,fileName视频地址,imgFile图片地址
    public string CatchImg(string fileName, string imgFile)
    {
        //使用ffmpeg抓图
        string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
        //图片名称
        string flv_img = imgFile + ".jpg";
        //图片大小
        string FlvImgSize = PublicMethod.sizeOfImg;
        //建立ffmpeg进程
        System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
        //后台运行
        ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        //进程参数
        //ffmpeg -i input.flv -y -f image2 -ss 10.11 -t 0.001 -s 240x180 catchimg.jpg;
        ImgstartInfo.Arguments = "   -i   " + fileName + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + FlvImgSize + "   " + flv_img;
        try
        {
            //开始抓图
            System.Diagnostics.Process.Start(ImgstartInfo);
            //睡眠一下,等待截图完成,按需设置
            System.Threading.Thread.Sleep(5000);
        }
        catch
        {
            return "";
        }
        //
        if (System.IO.File.Exists(flv_img))
        {
            return flv_img;
        }

        return "";
    }

原理很简单,其实就是使用System.Diagnostics.Process调用ffmpeg和mencoder这两个现成的工具来进行工作,关键是参数传递,主要还是看咱对ffmpeg和mencoder使用的熟悉程度。

原文地址:https://www.cnblogs.com/superfeeling/p/4512847.html