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

C#調(diào)用mmpeg進(jìn)行各種視頻轉(zhuǎn)換的類實(shí)例

 更新時(shí)間:2015年03月25日 12:19:53   作者:feige  
這篇文章主要介紹了C#調(diào)用mmpeg進(jìn)行各種視頻轉(zhuǎn)換的類,實(shí)例分析了C#調(diào)用mmpeg操作視頻文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#調(diào)用mmpeg進(jìn)行各種視頻轉(zhuǎn)換的類。分享給大家供大家參考。具體如下:

這個(gè)C#類封裝了視頻轉(zhuǎn)換所需的各種方法,基本上是圍繞著如何通過mmpeg工具來進(jìn)行視頻轉(zhuǎn)換

using System.Web;
using System.Configuration;
namespace DotNet.Utilities
{
  //if (this.fload.HasFile)
  //{
  //  string upFileName = HttpContext.Current.Server.MapPath("~/savefile") + "\\" + this.fload.PostedFile.FileName;
  //  string saveName  = DateTime.Now.ToString("yyyyMMddHHmmssffff");
  //  string playFile  = Server.MapPath(VideoConvert.savefile + saveName);
  //  string imgFile  = Server.MapPath(VideoConvert.savefile + saveName);
  //  VideoConvert pm = new VideoConvert();
  //  string m_strExtension = VideoConvert.GetExtension(this.fload.PostedFile.FileName).ToLower();
  //  if (m_strExtension == "flv")
  //  {
  //    System.IO.File.Copy(upFileName, playFile + ".flv");
  //    pm.CatchImg(upFileName, imgFile);
  //  }
  //  string Extension = pm.CheckExtension(m_strExtension);
  //  if (Extension == "ffmpeg")
  //  {
  //    pm.ChangeFilePhy(upFileName, playFile, imgFile);
  //  }
  //  else if (Extension == "mencoder")
  //  {
  //    pm.MChangeFilePhy(upFileName, playFile, imgFile);
  //  }
  //}
  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的視頻解碼(絕對(duì)路徑)
    /// <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的視頻解碼(相對(duì)路徑)
    /// <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)換(絕對(duì)路徑)
    /// <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
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • c#?定期重啟程序操作的實(shí)現(xiàn)

    c#?定期重啟程序操作的實(shí)現(xiàn)

    本文主要介紹了c#?定期重啟程序操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • C# 用什么方法將BitConverter.ToString產(chǎn)生字符串再轉(zhuǎn)換回去

    C# 用什么方法將BitConverter.ToString產(chǎn)生字符串再轉(zhuǎn)換回去

    這篇文章主要介紹了C# 用什么方法將BitConverter.ToString產(chǎn)生字符串再轉(zhuǎn)換回去,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • C#實(shí)現(xiàn)快速排序算法

    C#實(shí)現(xiàn)快速排序算法

    本文詳細(xì)講解了C#實(shí)現(xiàn)快速排序算法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效

    unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效

    這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#實(shí)現(xiàn)NPOI的Excel導(dǎo)出詳解

    C#實(shí)現(xiàn)NPOI的Excel導(dǎo)出詳解

    這篇文章主要介紹了C#實(shí)現(xiàn)NPOI的Excel導(dǎo)出的示例代碼,文中的實(shí)現(xiàn)過程講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-01-01
  • C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法

    C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法,涉及C#多線程操作文件讀寫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C# 減少嵌套循環(huán)的兩種方法

    C# 減少嵌套循環(huán)的兩種方法

    最近在解決性能優(yōu)化的問題,看到了一堆嵌套循環(huán),四五層級(jí)的循環(huán)真的有點(diǎn)過分了,在數(shù)據(jù)量成萬,十萬級(jí)別的時(shí)候,真的非常影響性能。本文介紹了C# 減少嵌套循環(huán)的兩種方法,幫助各位選擇適合自己的優(yōu)化方案,優(yōu)化程序性能
    2021-06-06
  • C#?EF?Core可視化工具的使用及EF?Core入門語句操作代碼

    C#?EF?Core可視化工具的使用及EF?Core入門語句操作代碼

    EF?Core?可用作對(duì)象關(guān)系映射程序?(O/RM),以便于?.NET?開發(fā)人員能夠使用?.NET?對(duì)象來處理數(shù)據(jù)庫,這樣就不必經(jīng)常編寫大部分?jǐn)?shù)據(jù)訪問代碼了,接下來通過本文給大家介紹C#?EF?Core可視化工具的使用及EF?Core入門語句,感興趣的朋友一起看看吧
    2022-02-02
  • C#?Winform消息通知系統(tǒng)托盤氣泡提示框ToolTip控件

    C#?Winform消息通知系統(tǒng)托盤氣泡提示框ToolTip控件

    這篇文章主要為大家介紹了C#或Winform中的消息通知之系統(tǒng)托盤的氣泡提示框窗口(系統(tǒng)toast通知)、ToolTip控件和ToolTipText屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 簡單實(shí)現(xiàn)C#窗體程序判斷是否閏年

    簡單實(shí)現(xiàn)C#窗體程序判斷是否閏年

    這篇文章主要介紹了簡單實(shí)現(xiàn)C#窗體程序判斷是否閏年的相關(guān)代碼,禁止窗體調(diào)整大小,關(guān)閉窗體前的判斷,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評(píng)論