C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
本文實(shí)例講述了C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法。分享給大家供大家參考。具體分析如下:
需要先下載ffmpeg,這是開(kāi)源的,代碼如下所示:
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)文章
unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決
這篇文章主要介紹了unity 切換場(chǎng)景不銷毀物體問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法
這篇文章主要介紹了C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法,涉及C#針對(duì)文件夾及圖片操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05C#動(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#項(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-01C#實(shí)現(xiàn)讀取txt通用的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了C#讀取txt通用的方法,兼容所有的UTF-8、Unicode(Little Endian)、BigEndianUnicode,有需要的小伙伴可以了解下2024-01-01