pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

C#視頻轉(zhuǎn)換類分享

這篇文章主要為大家分享了C#視頻轉(zhuǎn)換類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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 獲取文件擴展名
  /// <summary>
  /// 獲取文件擴展名
  /// </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)存緩存寫到磁盤需要時間較長,大概在3,4秒甚至更長;
    if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(flv_img_p)))
    {
     return flv_img_p;
    }
    return "";
   }
   catch
   {
    return "";
   }
  }
  #endregion

  #region 運行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 運行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)存緩存寫到磁盤需要時間較長,大概在3,4秒甚至更長; 
   ///這兒需要延時后再檢測,我服務(wù)器延時8秒,即如果超過8秒圖片仍不存在,認(rèn)為截圖失敗; 
   if (System.IO.File.Exists(flv_img))
   {
    return flv_img;
   }
   return "";
  }
  #endregion

  #region 運行mencoder的視頻解碼器轉(zhuǎn)換(絕對路徑)
  /// <summary>
  /// 運行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í)有所幫助,也希望大家多多支持html5模板網(wǎng)。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

這篇文章主要為大家詳細(xì)介紹了C# SendMail發(fā)送郵件功能實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了C#實現(xiàn)的SQL備份與還原功能,結(jié)合具體實例形式分析了C#操作數(shù)據(jù)庫實現(xiàn)SQL備份與還原相關(guān)的控件、SQL連接、文件等操作技巧,需要的朋友可以參考下
這篇文章主要介紹了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法,結(jié)合具體實例形式分析了數(shù)據(jù)庫的創(chuàng)建及checkedListBox1控件連接數(shù)據(jù)庫的相關(guān)操作技巧,需要的朋友可以參考下
這篇文章主要介紹了C#實現(xiàn)的sqlserver操作類,結(jié)合具體實例形式分析了C#針對sqlserver數(shù)據(jù)庫進(jìn)行連接、查詢、更新、關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下
這篇文章主要為大家詳細(xì)介紹了C#多線程數(shù)組模擬socket的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)http和ftp圖片地址獲取對應(yīng)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 哈尔滨发电机,黑龙江柴油发电机组-北方星光 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 通用磨耗试验机-QUV耐候试验机|久宏实业百科 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 电动不锈钢套筒阀-球面偏置气动钟阀-三通换向阀止回阀-永嘉鸿宇阀门有限公司 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | IP检测-检测您的IP质量 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 十二星座查询(性格特点分析、星座运势解读) - 玄米星座网 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 精雕机-火花机-精雕机 cnc-高速精雕机-电火花机-广东鼎拓机械科技有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 托利多电子平台秤-高精度接线盒-托利多高精度电子秤|百科 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 上海APP开发-APP制作-APP定制开发-上海APP开发制作公司-咏熠科技 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 防水套管厂家_刚性防水套管_柔性防水套管_不锈钢防水套管-郑州中泰管道 | 睿婕轻钢别墅_钢结构别墅_厂家设计施工报价 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 破碎机_上海破碎机_破碎机设备_破碎机厂家-上海山卓重工机械有限公司 |