c#调用mmpeg进行各种视频转换的类实例 -九游会真人第一品牌

本文实例讲述了c#调用mmpeg进行各种视频转换的类。分享给大家供大家参考。具体如下:

这个c#类封装了视频转换所需的各种方法,基本上是围绕着如何通过mmpeg工具来进行视频转换

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 获取文件的名字
    /// 
    /// 获取文件的名字
    /// 
    public static string getfilename(string filename)
    {
      int i = filename.lastindexof("\\")   1;
      string name = filename.substring(i);
      return name;
    }
    #endregion
    #region 获取文件扩展名
    /// 
    /// 获取文件扩展名
    /// 
    public static string getextension(string filename)
    {
      int i = filename.lastindexof(".")   1;
      string name = filename.substring(i);
      return name;
    }
    #endregion
    #region 获取文件类型
    /// 
    /// 获取文件类型
    /// 
    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 视频格式转为flv
    /// 
    /// 视频格式转为flv
    /// 
    /// 原视频文件地址
    /// 生成后的flv文件地址
    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视频的缩略图
    /// 
    /// 生成flv视频的缩略图
    /// 
    /// 视频文件地址
    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);
        //注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
        if (system.io.file.exists(httpcontext.current.server.mappath(flv_img_p)))
        {
          return flv_img_p;
        }
        return "";
      }
      catch
      {
        return "";
      }
    }
    #endregion
    #region 运行ffmpeg的视频解码(绝对路径)
    /// 
    /// 转换文件并保存在指定文件夹下
    /// 
    /// 上传视频文件的路径(原文件)
    /// 转换后的文件的路径(网络播放文件)
    /// 从视频文件中抓取的图片路径
    /// 成功:返回图片虚拟地址;失败:返回空字符串
    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);//转换
        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的视频解码(相对路径)
    /// 
    /// 转换文件并保存在指定文件夹下
    /// 
    /// 上传视频文件的路径(原文件)
    /// 转换后的文件的路径(网络播放文件)
    /// 从视频文件中抓取的图片路径
    /// 成功:返回图片虚拟地址;失败:返回空字符串
    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 "";
      }
      ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长; 
      ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;  
      if (system.io.file.exists(flv_img))
      {
        return flv_img;
      }
      return "";
    }
    #endregion
    #region 运行mencoder的视频解码器转换(绝对路径)
    /// 
    /// 运行mencoder的视频解码器转换
    /// 
    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
  }
}

希望本文所述对大家的c#程序设计有所帮助。

声明:本文内容来源于网络,j9九游会老哥俱乐部交流区的版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌j9九游会老哥俱乐部交流区的版权的内容,欢迎发送邮件至:notice#www.elefans.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

网站地图