C#視頻轉(zhuǎn)換類分享
本文實(shí)例為大家分享了C#視頻轉(zhuǎn)換類的具體代碼,供大家參考,具體內(nèi)容如下
VideoConvert
using System.Web;
using System.Configuration;
namespace DotNet.Utilities
{
public class VideoConvert : System.Web.UI.Page
{
public VideoConvert()
{ }
string[] strArrMencoder = new string[] { "wmv", "rmvb", "rm" };
string[] strArrFfmpeg = new string[] { "asf", "avi", "mpg", "3gp", "mov" };
#region 配置
public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];
public static string mencodertool = ConfigurationManager.AppSettings["mencoder"];
public static string savefile = ConfigurationManager.AppSettings["savefile"] + "/";
public static string sizeOfImg = ConfigurationManager.AppSettings["CatchFlvImgSize"];
public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
#endregion
#region 獲取文件的名字
/// <summary>
/// 獲取文件的名字
/// </summary>
public static string GetFileName(string fileName)
{
int i = fileName.LastIndexOf("\\") + 1;
string Name = fileName.Substring(i);
return Name;
}
#endregion
#region 獲取文件擴(kuò)展名
/// <summary>
/// 獲取文件擴(kuò)展名
/// </summary>
public static string GetExtension(string fileName)
{
int i = fileName.LastIndexOf(".") + 1;
string Name = fileName.Substring(i);
return Name;
}
#endregion
#region 獲取文件類型
/// <summary>
/// 獲取文件類型
/// </summary>
public string CheckExtension(string extension)
{
string m_strReturn = "";
foreach (string var in this.strArrFfmpeg)
{
if (var == extension)
{
m_strReturn = "ffmpeg"; break;
}
}
if (m_strReturn == "")
{
foreach (string var in strArrMencoder)
{
if (var == extension)
{
m_strReturn = "mencoder"; break;
}
}
}
return m_strReturn;
}
#endregion
#region 視頻格式轉(zhuǎn)為Flv
/// <summary>
/// 視頻格式轉(zhuǎn)為Flv
/// </summary>
/// <param name="vFileName">原視頻文件地址</param>
/// <param name="ExportName">生成后的Flv文件地址</param>
public bool ConvertFlv(string vFileName, string ExportName)
{
if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName))))
{
return false;
}
vFileName = HttpContext.Current.Server.MapPath(vFileName);
ExportName = HttpContext.Current.Server.MapPath(ExportName);
string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\""; //Flv格式
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ffmpegtool;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/tools/");
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.BeginErrorReadLine();
p.WaitForExit();
p.Close();
p.Dispose();
return true;
}
#endregion
#region 生成Flv視頻的縮略圖
/// <summary>
/// 生成Flv視頻的縮略圖
/// </summary>
/// <param name="vFileName">視頻文件地址</param>
public string CatchImg(string vFileName)
{
if ((!System.IO.File.Exists(ffmpegtool)) || (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(vFileName)))) return "";
try
{
string flv_img_p = vFileName.Substring(0, vFileName.Length - 4) + ".jpg";
string Command = " -i " + HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + HttpContext.Current.Server.MapPath(flv_img_p);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ffmpegtool;
p.StartInfo.Arguments = Command;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
try
{
p.Start();
}
catch
{
return "";
}
finally
{
p.Close();
p.Dispose();
}
System.Threading.Thread.Sleep(4000);
//注意:圖片截取成功后,數(shù)據(jù)由內(nèi)存緩存寫到磁盤需要時(shí)間較長,大概在3,4秒甚至更長;
if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p)))
{
return flv_img_p;
}
return "";
}
catch
{
return "";
}
}
#endregion
#region 運(yùn)行FFMpeg的視頻解碼(絕對路徑)
/// <summary>
/// 轉(zhuǎn)換文件并保存在指定文件夾下
/// </summary>
/// <param name="fileName">上傳視頻文件的路徑(原文件)</param>
/// <param name="playFile">轉(zhuǎn)換后的文件的路徑(網(wǎng)絡(luò)播放文件)</param>
/// <param name="imgFile">從視頻文件中抓取的圖片路徑</param>
/// <returns>成功:返回圖片虛擬地址;失敗:返回空字符串</returns>
public string ChangeFilePhy(string fileName, string playFile, string imgFile)
{
string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
}
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
string FlvImgSize = VideoConvert.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;
try
{
System.Diagnostics.Process.Start(FilestartInfo);//轉(zhuǎn)換
CatchImg(fileName, imgFile); //截圖
}
catch
{
return "";
}
return "";
}
public string CatchImg(string fileName, string imgFile)
{
string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
string flv_img = imgFile + ".jpg";
string FlvImgSize = VideoConvert.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 "";
}
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
#endregion
#region 運(yùn)行FFMpeg的視頻解碼(相對路徑)
/// <summary>
/// 轉(zhuǎn)換文件并保存在指定文件夾下
/// </summary>
/// <param name="fileName">上傳視頻文件的路徑(原文件)</param>
/// <param name="playFile">轉(zhuǎn)換后的文件的路徑(網(wǎng)絡(luò)播放文件)</param>
/// <param name="imgFile">從視頻文件中抓取的圖片路徑</param>
/// <returns>成功:返回圖片虛擬地址;失敗:返回空字符串</returns>
public string ChangeFileVir(string fileName, string playFile, string imgFile)
{
string ffmpeg = Server.MapPath(VideoConvert.ffmpegtool);
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
}
string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), ".jpg");
string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
string FlvImgSize = VideoConvert.sizeOfImg;
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img;
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;
try
{
System.Diagnostics.Process.Start(FilestartInfo);
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
return "";
}
///注意:圖片截取成功后,數(shù)據(jù)由內(nèi)存緩存寫到磁盤需要時(shí)間較長,大概在3,4秒甚至更長;
///這兒需要延時(shí)后再檢測,我服務(wù)器延時(shí)8秒,即如果超過8秒圖片仍不存在,認(rèn)為截圖失敗;
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
#endregion
#region 運(yùn)行mencoder的視頻解碼器轉(zhuǎn)換(絕對路徑)
/// <summary>
/// 運(yùn)行mencoder的視頻解碼器轉(zhuǎn)換
/// </summary>
public string MChangeFilePhy(string vFileName, string playFile, string imgFile)
{
string tool = Server.MapPath(VideoConvert.mencodertool);
if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName)))
{
return "";
}
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
string FlvImgSize = VideoConvert.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.Start(FilestartInfo);
CatchImg(flv_file, imgFile);
}
catch
{
return "";
}
return "";
}
#endregion
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:WinForm通過操作注冊表實(shí)現(xiàn)限制軟件使用次數(shù)的方法
欄 目:C#教程
本文標(biāo)題:C#視頻轉(zhuǎn)換類分享
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5622.html
您可能感興趣的文章
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#通過Semaphore類控制線程隊(duì)列的方法
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#操作ftp類完整實(shí)例
- 01-10winform簡單緩存類實(shí)例
- 01-10C#實(shí)現(xiàn)控制攝像頭的類
- 01-10時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒)
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)


