欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现通过ffmpeg从flv视频文件中截图的方法

程序员文章站 2023-12-13 10:09:10
本文实例讲述了c#实现通过ffmpeg从flv视频文件中截图的方法。分享给大家供大家参考。具体分析如下: 需要先下载ffmpeg,这是开源的,代码如下所示: 复制代码...

本文实例讲述了c#实现通过ffmpeg从flv视频文件中截图的方法。分享给大家供大家参考。具体分析如下:

需要先下载ffmpeg,这是开源的,代码如下所示:

复制代码 代码如下:
using system;
using system.configuration;
public class publicmethod:system.web.ui.page
{
    public publicmethod()
    {
    }
    //文件路径
    public static string ffmpegtool = "ffmpeg/ffmpeg.exe";        
    public static string mencodertool = "mencoder/mencoder.exe";
    public static string flvtool = "flvtool/flvtool2.exe";//flv标记工具
    public static string upfile = "upfiles" + "/";//上传文件夹
    public static string imgfile = "imgfile" + "/";//图片文件夹
    public static string playfile = "playfiles" + "/";//flv文件夹
    public static string xmlfile = "xmlfiles" + "/";//xml文件夹
    public static string sizeofimg = "240x180";//图片的宽与高
    public static string widthoffile = "400";//flv文件的宽度
    public static string heightoffile = "350";//flv文件的高度
    //public static string ffmpegtool = configurationmanager.appsettings["ffmpeg"];
    //public static string mencodertool = configurationmanager.appsettings["mencoder"];
    //public static string upfile = configurationmanager.appsettings["upfile"] + "/";
    //public static string imgfile = configurationmanager.appsettings["imgfile"] + "/";
    //public static string playfile = configurationmanager.appsettings["playfile"] + "/";
    //文件图片大小
    //public static string sizeofimg = configurationmanager.appsettings["catchflvimgsize"];
    //文件大小
    //public static string widthoffile = configurationmanager.appsettings["widthsize"];
    //public static string heightoffile = configurationmanager.appsettings["heightsize"];
    //   // //获取文件的名字
    private system.timers.timer mytimer = new system.timers.timer(3000);//记时器
    public static string flvname = "";
    public static string imgname = "";
    public static string flvxml = "";
    public static int pid = 0;
    public static string getfilename(string filename)
    {
        int i = filename.lastindexof("\") + 1;
        string name = filename.substring(i);
        return name;
    }
    //获取文件扩展名
    public static string getextension(string filename)
    {
        int i = filename.lastindexof(".")+1;
        string name = filename.substring(i);
        return name;
    }
    //
    #region //运行ffmpeg的视频解码,(这里是绝对路径)
    /// <summary>
    /// 转换文件并保存在指定文件夹下面(这里是绝对路径)
    /// </summary>
    /// <param name="filename">上传视频文件的路径(原文件)</param>
    /// <param name="playfile">转换后的文件的路径(网络播放文件)</param>
    /// <param name="imgfile">从视频文件中抓取的图片路径</param>
    /// <returns>成功:返回图片虚拟地址;   失败:返回空字符串</returns>
    public void changefilephy(string filename, string playfile, string imgfile)
    {
        //取得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;
        }
        //获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/web/user1/00001.jpg 
        string flv_file = system.io.path.changeextension(playfile, ".flv");
        //截图的尺寸大小,配置在web.config中,如:<add   key="catchflvimgsize"   value="240x180"   /> 
        string flvimgsize = publicmethod.sizeofimg;
        system.diagnostics.processstartinfo filestartinfo = new system.diagnostics.processstartinfo(ffmpeg);
        filestartinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;
        filestartinfo.arguments = " -i " + filename + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthoffile + "x" + heightoffile + " " + flv_file;
        //imgstartinfo.arguments = "   -i   " + filename + "   -y   -f   image2   -t   0.05   -s   " + flvimgsize + "   " + flv_img;
        try
        {
            //转换
            system.diagnostics.process.start(filestartinfo);
            //截图
            catchimg(filename, imgfile);
            //system.diagnostics.process.start(imgstartinfo);
        }
        catch
        {
        }
    }
    #endregion
    #region 截图
    public string catchimg(string filename,string imgfile)
    {
        //
        string ffmpeg = server.mappath(publicmethod.ffmpegtool);
        //
        string flv_img =imgfile+".jpg";
        //
        string flvimgsize = publicmethod.sizeofimg;
        //
        system.diagnostics.processstartinfo imgstartinfo = new system.diagnostics.processstartinfo(ffmpeg);
        imgstartinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;
        //
        imgstartinfo.arguments = "   -i   " + filename + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + flvimgsize + "   " + flv_img;
        try
        {
            system.diagnostics.process.start(imgstartinfo);
        }
        catch
        {
            return "";
        }
        //
        catchflvtool(filename);
        if (system.io.file.exists(flv_img))
        {
            return flv_img;
        }
        return "";
    }
    #endregion
    #region //运行ffmpeg的视频解码,(这里是(虚拟)相对路径)
    /// <summary>
    /// 转换文件并保存在指定文件夹下面(这里是相对路径)
    /// </summary>
    /// <param name="filename">上传视频文件的路径(原文件)</param>
    /// <param name="playfile">转换后的文件的路径(网络播放文件)</param>
    /// <param name="imgfile">从视频文件中抓取的图片路径</param>
    /// <returns>成功:返回图片虚拟地址;   失败:返回空字符串</returns>
    public void changefilevir(string filename, string playfile, string imgfile)
    {
        //取得ffmpeg.exe的路径,路径配置在web.config中,如:<add   key="ffmpeg"   value="e:\aspx1\ffmpeg.exe"   /> 
        string ffmpeg = server.mappath(publicmethod.ffmpegtool);
        if ((!system.io.file.exists(ffmpeg)) || (!system.io.file.exists(filename)))
        {
            return;
        }
        //获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/web/user1/00001.jpg 
        string flv_img = system.io.path.changeextension(server.mappath(imgfile), ".jpg");
        string flv_file = system.io.path.changeextension(server.mappath(playfile), ".flv");
        //截图的尺寸大小,配置在web.config中,如:<add   key="catchflvimgsize"   value="240x180"   /> 
        string flvimgsize = publicmethod.sizeofimg;
        system.diagnostics.processstartinfo filestartinfo = new system.diagnostics.processstartinfo(ffmpeg);
        filestartinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;
        //此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg   0.4.9调试通过
        //ffmpeg -i f:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
        filestartinfo.arguments = " -i " + filename + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthoffile + "x" + heightoffile + " " + flv_file;
        try
        {
            system.diagnostics.process ps = new system.diagnostics.process();
            ps.startinfo = filestartinfo;
            ps.start();
            session.add("processid", ps.id);
            session.add("flv", flv_file);
            session.add("img", imgfile);
            mytimer.elapsed += new system.timers.elapsedeventhandler(mytimer_test);
            mytimer.enabled = true;
        }
        catch
        {
        }
    }
    #endregion
    #region //运行mencoder的视频解码器转换(这里是(绝对路径))
    public void mchangefilephy(string vfilename, string playfile, string imgfile)
    {
        string tool = server.mappath(publicmethod.mencodertool);
        //string mplaytool = server.mappath(publicmethod.ffmpegtool);
        if ((!system.io.file.exists(tool)) || (!system.io.file.exists(vfilename)))
        {
            return;
        }
        string flv_file = system.io.path.changeextension(playfile, ".flv");
        //截图的尺寸大小,配置在web.config中,如:<add   key="catchflvimgsize"   value="240x180"   /> 
        string flvimgsize = publicmethod.sizeofimg;
        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 ps = new system.diagnostics.process();
            ps.startinfo = filestartinfo;
            ps.start();
            session.add("processid", ps.id);
            session.add("flv", flv_file);
            session.add("img", imgfile);
            //pid = ps.id;
            //flvname = flv_file;
            //imgname = imgfile;
            mytimer.elapsed += new system.timers.elapsedeventhandler(mytimer_test);
            mytimer.enabled = true;
        }
        catch
        {
        }
    }
    /// <summary>
    /// 记时器功能,自动保存截图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void mytimer_test(object sender, system.timers.elapsedeventargs e)
    {
        if (!object.equals(null, session["processid"]))
        {
            try
            {
                system.diagnostics.process prs = system.diagnostics.process.getprocessbyid(int.parse(session["processid"].tostring()));
                if (prs.hasexited)
                {
                    catchimg(session["flv"].tostring(), session["img"].tostring());
                    catchflvtool(session["flv"].tostring());
                    mytimer.enabled = false;
                    mytimer.close();
                    mytimer.dispose();
                    session.abandon();
                }
            }
            catch
            {
                catchimg(session["flv"].tostring(), session["img"].tostring());
                catchflvtool(session["flv"].tostring());
                mytimer.enabled = false;
                mytimer.close();
                mytimer.dispose();
                session.abandon();
            }
        }
    }
    #endregion
    public string catchflvtool(string filename)
    {
        //
        string flvtools = server.mappath(publicmethod.flvtool);
        //
        string flv_xml = filename.replace(".flv", ".xml").replace(publicmethod.upfile.replace("/", ""), publicmethod.xmlfile.replace("/", ""));
        //
        system.diagnostics.processstartinfo imgstartinfo = new system.diagnostics.processstartinfo(flvtools);
        imgstartinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;
        //
        imgstartinfo.arguments = "   " + filename + "   -upx   " + filename + "  >  " + flv_xml;
        try
        {
            system.diagnostics.process.start(imgstartinfo);
        }
        catch
        {
            return "";
        }
        //
        if (system.io.file.exists(flv_xml))
        {
            return flv_xml;
        }
        return "";
    }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: