截取视频文件第一桢的图片和转换视频格式文件为flv(支持文件格式:asf,avi,mpg,flv,3gp,mov,wmv)...

前段时间写过一篇抓取视频文件中图片的帖子,如下 痛并努力着-在asp.net中如何从视频文件中抓取一桢并生成图像文件
另外一种方法是采用ffmpeg解码器,来实现如功能。
代码如下:
  1 None.gif   public   class  CatchFlv
  2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  3InBlock.gif       public CatchFlv()
  4ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
  5ExpandedSubBlockEnd.gif       }

  6ExpandedSubBlockStart.gifContractedSubBlock.gif       /**//// <summary>
  7InBlock.gif       /// @从视频文件截图,生成在视频文件所在文件夹
  8InBlock.gif       /// 支持文件格式:asf,avi,mpg,flv,3gp,mov,wmv
  9InBlock.gif       /// 在Web.Config 中需要两个前置配置项:
 10InBlock.gif       /// 1.ffmpeg.exe文件的路径
 11InBlock.gif       /// <add key="ffmpeg" value="\thread\ffmpeg\ffmpeg.exe" />
 12InBlock.gif       /// 2.截图的尺寸大小
 13InBlock.gif       /// <add key="CatchFlvImgSize" value="140x110" />
 14InBlock.gif       /// 3.视频处理程序ffmpeg.exe
 15InBlock.gif       /// </summary>
 16InBlock.gif       /// <param name="vFileName">视频文件绝对或相对地址,如:(..)/Web/FlvFile/User1/001.avi</param> 
 17ExpandedSubBlockEnd.gif       /// <returns>成功:返回图片绝对/相对地址; 失败:返回空字符串</returns>

 18InBlock.gif       public static string CatchImg(string vFileName)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 20InBlock.gif           try
 21ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
 22InBlock.gif               string ffmpeg = System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];
 23InBlock.gif               ffmpeg = HttpContext.Current.Server.MapPath(ffmpeg);
 24InBlock.gif
 25InBlock.gif               if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(vFileName))))
 26ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
 27InBlock.gif                   return "";
 28ExpandedSubBlockEnd.gif               }

 29InBlock.gif
 30InBlock.gif               //获得图片相对路径/最后存储到数据库的路径,如:/Web/FlvFile/User1/00001.jpg
 31InBlock.gif               string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
 32InBlock.gif
 33InBlock.gif               //图片绝对路径,如:D:\Video\Web\FlvFile\User1\0001.jpg
 34InBlock.gif               string flv_img_p = HttpContext.Current.Server.MapPath(flv_img);
 35InBlock.gif
 36InBlock.gif               //截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="140x110" /> 
 37InBlock.gif               string FlvImgSize = System.Configuration.ConfigurationSettings.AppSettings["CatchFlvImgSize"];
 38InBlock.gif
 39InBlock.gif               System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
 40InBlock.gif               startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
 41InBlock.gif
 42InBlock.gif               //此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
 43InBlock.gif               startInfo.Arguments = " -i " + System.Web.HttpContext.Current.Server.MapPath(vFileName) + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img_p;
 44InBlock.gif               try
 45ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
 46InBlock.gif                   System.Diagnostics.Process.Start(startInfo);
 47ExpandedSubBlockEnd.gif               }

 48InBlock.gif               catch
 49ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
 50InBlock.gif                   return "";
 51ExpandedSubBlockEnd.gif               }

 52InBlock.gif               System.Threading.Thread.Sleep(4000);
 53ExpandedSubBlockStart.gifContractedSubBlock.gif               /**////注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
 54InBlock.gif               if (System.IO.File.Exists(flv_img_p))
 55ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
 56InBlock.gif                   return flv_img;
 57ExpandedSubBlockEnd.gif               }

 58InBlock.gif               return "";
 59ExpandedSubBlockEnd.gif           }

 60InBlock.gif           catch
 61ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
 62InBlock.gif               return "";
 63ExpandedSubBlockEnd.gif           }

 64InBlock.gif
 65ExpandedSubBlockEnd.gif       }

 66InBlock.gif
 67ExpandedSubBlockStart.gifContractedSubBlock.gif       /**//// <summary>
 68InBlock.gif       /// @视频文件格式转换,生成在视频文件所在文件夹
 69InBlock.gif       /// 支持文件格式:asf,avi,mpg,flv,3gp,mov,wmv
 70InBlock.gif       /// 在Web.Config 中需要两个前置配置项:
 71InBlock.gif       /// 1.ffmpeg.exe文件的路径
 72InBlock.gif       /// <add key="ffmpeg" value="\thread\ffmpeg\ffmpeg.exe" />
 73InBlock.gif       /// 2.截图的尺寸大小
 74InBlock.gif       /// <add key="CatchFlvSize" value="240x180" /> 
 75InBlock.gif       /// 3.视频处理程序ffmpeg.exe
 76InBlock.gif       /// </summary>
 77InBlock.gif       /// <param name="vFileName">视频文件绝对或相对地址,如:(..)/Web/FlvFile/User1/001.avi</param> 
 78ExpandedSubBlockEnd.gif       /// <returns>成功:返回flv文件绝对/相对地址; 失败:返回空字符串</returns>

 79InBlock.gif       public static string ChangeMediaFormat(string vFileName)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{
 81InBlock.gif           try
 82ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
 83InBlock.gif               string ffmpeg = System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];
 84InBlock.gif               string wks_url = System.Configuration.ConfigurationSettings.AppSettings["wks_local"];
 85InBlock.gif               ffmpeg = wks_url+ffmpeg;
 86InBlock.gif               if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(wks_url+vFileName)))
 87ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
 88InBlock.gif                   return "";
 89ExpandedSubBlockEnd.gif               }

 90InBlock.gif
 91InBlock.gif               string flv_fot = System.IO.Path.ChangeExtension(vFileName, ".flv");
 92InBlock.gif               string flv_fot_p = wks_url+flv_fot;
 93InBlock.gif
 94InBlock.gif               //尺寸大小,配置在Web.Config中,如:<add key="CatchFlvSize" value="240x180" /> 
 95InBlock.gif               string FlvImgSize = System.Configuration.ConfigurationSettings.AppSettings["CatchFlvSize"];
 96InBlock.gif
 97InBlock.gif               System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
 98InBlock.gif               startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
 99InBlock.gif
100InBlock.gif               //此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
101InBlock.gif               startInfo.Arguments = " -i " + wks_url+vFileName + " -ab 56 -ar 22050 -b 500 -r 15 -s  " + FlvImgSize + " " + flv_fot_p;
102InBlock.gif
103InBlock.gif               try
104ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
105InBlock.gif                   System.Diagnostics.Process.Start(startInfo);
106ExpandedSubBlockEnd.gif               }

107InBlock.gif               catch
108ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
109InBlock.gif                   return "";
110ExpandedSubBlockEnd.gif               }

111InBlock.gif
112ExpandedSubBlockStart.gifContractedSubBlock.gif               /**////注意:文件格式转换成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
113InBlock.gif               System.Threading.Thread.Sleep(6000);
114InBlock.gif               if (System.IO.File.Exists(flv_fot_p))
115ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
116InBlock.gif                   WKS.Common.ImagesSet.DeleteOleImg(wks_url+vFileName);
117InBlock.gif                   return flv_fot;
118ExpandedSubBlockEnd.gif               }

119InBlock.gif
120InBlock.gif               return "";
121ExpandedSubBlockEnd.gif           }

122InBlock.gif           catch
123ExpandedSubBlockStart.gifContractedSubBlock.gif           dot.gif{
124InBlock.gif               return "";
125ExpandedSubBlockEnd.gif           }

126ExpandedSubBlockEnd.gif       }
     
127ExpandedBlockEnd.gif    }

128 None.gif    
但是,这种方法不知道ffmpeg什么时候生成结果,生成什么样的结果,因此也存在一定局限性.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值