C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
本文實(shí)例講述了C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法。分享給大家供大家參考。具體分析如下:
需要先下載ffmpeg,這是開源的,代碼如下所示:
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ì)有所幫助。
- 解決C# 截取當(dāng)前程序窗口指定位置截圖的實(shí)現(xiàn)方法
- c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
- C#截圖程序類似騰訊QQ截圖實(shí)現(xiàn)代碼
- 解決C#全屏幕截圖的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能
- C#實(shí)現(xiàn)屬于自己的QQ截圖工具
- c# 控件截圖的簡(jiǎn)單實(shí)例
- 對(duì)指定的網(wǎng)頁(yè)進(jìn)行截圖的效果 C#版
- C#中的FileUpload 選擇后的預(yù)覽效果具體實(shí)現(xiàn)
- C# DirectShow預(yù)覽攝像頭并截圖
相關(guān)文章
詳解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 類型是一個(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ù)覽直接下載)
這篇文章主要給大家介紹了關(guān)于C#導(dǎo)出pdf的實(shí)現(xiàn)方法,實(shí)現(xiàn)后瀏覽器不預(yù)覽就可以直接下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
C#控件picturebox實(shí)現(xiàn)畫圖功能
這篇文章主要為大家詳細(xì)介紹了C#控件picturebox實(shí)現(xiàn)畫圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
C#使用LINQ中Enumerable類方法的延遲與立即執(zhí)行的控制
這篇文章主要介紹了C#的LINQ查詢中Enumerable類方法的延遲與立即執(zhí)行,LINQ語(yǔ)言集成查詢可以讓C#和VB以查詢數(shù)據(jù)庫(kù)相同的方式操作內(nèi)存數(shù)據(jù),需要的朋友可以參考下2016-03-03
C#實(shí)現(xiàn)文件上傳及文件下載功能實(shí)例代碼
文件上傳文件下載需求在項(xiàng)目中經(jīng)常會(huì)遇到,今天小編給大家分享C#實(shí)現(xiàn)文件上傳及文件下載功能實(shí)例代碼,需要的朋友參考下吧2017-08-08
WPF實(shí)現(xiàn)圓形進(jìn)度條的示例代碼
這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)圓形的進(jìn)度條,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-01-01

