欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

需要先下載ffmpeg,這是開(kāi)源的,代碼如下所示:

復(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ù)庫(kù)的路徑,如:/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ù)庫(kù)的路徑,如:/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)試通過(guò)
        //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# 字符串處理小工具

    C# 字符串處理小工具

    本文主要介紹C#字符串處理小工具,實(shí)現(xiàn)功能包括:轉(zhuǎn)換為大寫;轉(zhuǎn)換為小寫;反轉(zhuǎn)字符串;匹配某字符串出現(xiàn)次數(shù);正則匹配;base64加密;base64解密;ROT13加密解密;MD5 32位加密。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決

    unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決

    這篇文章主要介紹了unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C#實(shí)現(xiàn)排列組合算法完整實(shí)例

    C#實(shí)現(xiàn)排列組合算法完整實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下
    2014-09-09
  • C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法

    C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法

    這篇文章主要介紹了C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法,涉及C#針對(duì)文件夾及圖片操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C#動(dòng)態(tài)調(diào)整數(shù)組大小的方法

    C#動(dòng)態(tài)調(diào)整數(shù)組大小的方法

    這篇文章主要介紹了C#動(dòng)態(tài)調(diào)整數(shù)組大小的方法,涉及C#中靜態(tài)方法CreateInstance的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C# 泛型字典 Dictionary的使用詳解

    C# 泛型字典 Dictionary的使用詳解

    本文主要介紹了C# 泛型字典 Dictionary的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 在C#項(xiàng)目中調(diào)用C++編寫的動(dòng)態(tài)庫(kù)的三種方式

    在C#項(xiàng)目中調(diào)用C++編寫的動(dòng)態(tài)庫(kù)的三種方式

    這篇文章給大家介紹了三種方式詳解如何在C#項(xiàng)目中調(diào)用C++編寫的動(dòng)態(tài)庫(kù),文中通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-01-01
  • unity實(shí)現(xiàn)UI元素跟隨3D物體

    unity實(shí)現(xiàn)UI元素跟隨3D物體

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)UI元素跟隨3D物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#特性之匿名方法和Lambda表達(dá)式

    C#特性之匿名方法和Lambda表達(dá)式

    這篇文章主要介紹了C#特性之匿名方法和Lambda表達(dá)式,需要的朋友可以參考下
    2014-12-12
  • C#實(shí)現(xiàn)讀取txt通用的方法小結(jié)

    C#實(shí)現(xiàn)讀取txt通用的方法小結(jié)

    這篇文章主要為大家詳細(xì)介紹了C#讀取txt通用的方法,兼容所有的UTF-8、Unicode(Little Endian)、BigEndianUnicode,有需要的小伙伴可以了解下
    2024-01-01

最新評(píng)論