最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)通過ffmpeg從flv視頻文件中截圖的方法

 更新時(shí)間:2015年03月23日 16:09:52   作者:niuniu  
這篇文章主要介紹了C#實(shí)現(xiàn)通過ffmpeg從flv視頻文件中截圖的方法,實(shí)例分析了C#使用ffmpeg操作flv文件的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)通過ffmpeg從flv視頻文件中截圖的方法。分享給大家供大家參考。具體分析如下:

需要先下載ffmpeg,這是開源的,代碼如下所示:

復(fù)制代碼 代碼如下:
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標(biāo)記工具
    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);//記時(shí)器
    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;
    }
    //獲取文件擴(kuò)展名
    public static string GetExtension(string fileName)
    {
        int i = fileName.LastIndexOf(".")+1;
        string Name = fileName.Substring(i);
        return Name;
    }
    //
    #region //運(yùn)行FFMpeg的視頻解碼,(這里是絕對(duì)路徑)
    /// <summary>
    /// 轉(zhuǎn)換文件并保存在指定文件夾下面(這里是絕對(duì)路徑)
    /// </summary>
    /// <param name="fileName">上傳視頻文件的路徑(原文件)</param>
    /// <param name="playFile">轉(zhuǎn)換后的文件的路徑(網(wǎng)絡(luò)播放文件)</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)文件相對(duì)路徑/最后存儲(chǔ)到數(shù)據(jù)庫的路徑,如:/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
        {
            //轉(zhuǎn)換
            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 //運(yùn)行FFMpeg的視頻解碼,(這里是(虛擬)相對(duì)路徑)
    /// <summary>
    /// 轉(zhuǎn)換文件并保存在指定文件夾下面(這里是相對(duì)路徑)
    /// </summary>
    /// <param name="fileName">上傳視頻文件的路徑(原文件)</param>
    /// <param name="playFile">轉(zhuǎn)換后的文件的路徑(網(wǎng)絡(luò)播放文件)</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)文件相對(duì)路徑/最后存儲(chǔ)到數(shù)據(jù)庫的路徑,如:/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文件需要的參數(shù)即可,此處命令在ffmpeg   0.4.9調(diào)試通過
        //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 //運(yùn)行mencoder的視頻解碼器轉(zhuǎn)換(這里是(絕對(duì)路徑))
    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>
    /// 記時(shí)器功能,自動(dòng)保存截圖
    /// </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 "";
    }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 詳解C#實(shí)現(xiàn)在Excel單元格中應(yīng)用多種字體格式

    詳解C#實(shí)現(xiàn)在Excel單元格中應(yīng)用多種字體格式

    在Excel中,可對(duì)單元格中的字符串設(shè)置多種不同樣式。本文,將以C#及VB.NET代碼為例,介紹如何在Excel同一個(gè)單元格中應(yīng)用多種字體樣式,感興趣的可以了解一下
    2022-05-05
  • C#中dynamic的使用方法及應(yīng)用場(chǎng)景

    C#中dynamic的使用方法及應(yīng)用場(chǎng)景

    在 C# 編程中,dynamic 類型是一個(gè)非常特殊的類型,它在編譯時(shí)并不會(huì)進(jìn)行類型檢查,而是在運(yùn)行時(shí)才進(jìn)行類型解析,本文將詳細(xì)講解 dynamic 的使用方法、優(yōu)缺點(diǎn)以及一些實(shí)際應(yīng)用場(chǎng)景,需要的朋友可以參考下
    2024-08-08
  • C#導(dǎo)出pdf的實(shí)現(xiàn)方法(瀏覽器不預(yù)覽直接下載)

    C#導(dǎo)出pdf的實(shí)現(xiàn)方法(瀏覽器不預(yù)覽直接下載)

    這篇文章主要給大家介紹了關(guān)于C#導(dǎo)出pdf的實(shí)現(xiàn)方法,實(shí)現(xiàn)后瀏覽器不預(yù)覽就可以直接下載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • C#分屏控件用法實(shí)例

    C#分屏控件用法實(shí)例

    這篇文章主要介紹了C#分屏控件用法實(shí)例,需要的朋友可以參考下
    2014-08-08
  • C#10的13個(gè)特性

    C#10的13個(gè)特性

    本文詳細(xì)講解了C#10的13個(gè)特性,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • C#控件picturebox實(shí)現(xiàn)畫圖功能

    C#控件picturebox實(shí)現(xiàn)畫圖功能

    這篇文章主要為大家詳細(xì)介紹了C#控件picturebox實(shí)現(xiàn)畫圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • C#實(shí)現(xiàn)讀寫CSV文件的方法詳解

    C#實(shí)現(xiàn)讀寫CSV文件的方法詳解

    項(xiàng)目中經(jīng)常遇到CSV文件的讀寫需求,其中的難點(diǎn)主要是CSV文件的解析。本文會(huì)介紹CsvHelper、TextFieldParser、正則表達(dá)式三種解析CSV文件的方法,需要的可以參考一下
    2022-06-06
  • C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制

    C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制

    這篇文章主要介紹了C#的LINQ查詢中Enumerable類方法的延遲與立即執(zhí)行,LINQ語言集成查詢可以讓C#和VB以查詢數(shù)據(jù)庫相同的方式操作內(nèi)存數(shù)據(jù),需要的朋友可以參考下
    2016-03-03
  • C#實(shí)現(xiàn)文件上傳及文件下載功能實(shí)例代碼

    C#實(shí)現(xiàn)文件上傳及文件下載功能實(shí)例代碼

    文件上傳文件下載需求在項(xiàng)目中經(jīng)常會(huì)遇到,今天小編給大家分享C#實(shí)現(xiàn)文件上傳及文件下載功能實(shí)例代碼,需要的朋友參考下吧
    2017-08-08
  • WPF實(shí)現(xiàn)圓形進(jìn)度條的示例代碼

    WPF實(shí)現(xiàn)圓形進(jìn)度條的示例代碼

    這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)圓形的進(jìn)度條,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2023-01-01

最新評(píng)論

怀集县| 西吉县| 偏关县| 临邑县| 荔浦县| 余干县| 白水县| 巩义市| 鹤庆县| 虎林市| 阿拉善右旗| 桓仁| 永定县| 平武县| 铜川市| 体育| 大关县| 东乡族自治县| 久治县| 宁乡县| 苍山县| 彰化县| 改则县| 玛曲县| 东山县| 呼和浩特市| 嘉兴市| 固安县| 惠来县| 孝义市| 娄底市| 灵寿县| 沈丘县| 湾仔区| 徐闻县| 峨边| 双鸭山市| 双柏县| 龙山县| 海晏县| 报价|