[.NET]c#.net程序中使用ffmpeg.exe来处理视频并生成上传视频的截图
添加如下前台代码:
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传视频" />
- .cs中代码:
- protected void Button1_Click(object sender, EventArgs e) //按钮事件
- {
- string imgfileExp = this.FileUpload1.PostedFile.FileName.Substring (this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1);
- string filename = "11223344";
- if (imgfileExp.ToLower() == "flv")
- {
- this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/uploadfile") + "\\" + filename + "." + imgfileExp);
- string ffmpeg = Server.MapPath("ffmpeg.exe"); //ffmpeg执行文件的路径
- string filenames = Server.MapPath("uploadfile") + "\\" + filename + "." + imgfileExp; //上传文件存放路径
- string catchImg = CatchImg("uploadfile/" + filename + "." + imgfileExp);
- }
- }
- public string CatchImg(string vFileName)
- {
- try
- {
- string ffmpeg = "ffmpeg.exe";
- ffmpeg = Server.MapPath(ffmpeg);
- if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(vFileName))))
- {
- return "";
- }
- //获得图片相对路径/最后存储到数据库的路径,如:uploadfile/11223344.jpg
- string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
- //图片绝对路径,如:D:\Video\Web\FlvFile\User1\0001.jpg
- string flv_img_p = Server.MapPath("uploadfile/1.jpg");
- //截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="140x110" />
- string FlvImgSize = "140*110";
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics. ProcessStartInfo(ffmpeg);
- startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
- //此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
- startInfo.Arguments = " -i " + Server.MapPath(vFileName) + " -y -f image2 -t 0.1 -s " + FlvImgSize + " " + flv_img_p;
- try
- {
- System.Diagnostics.Process.Start(startInfo);
- }
- catch
- {
- return "";
- }
- System.Threading.Thread.Sleep(4000);
- return "";
- }
- catch
- {
- return "";
- }
- }
注意:1、startInfo.Arguments中的3个字符串参数中不能出现空格,否则生成图片失败。
2、在web.config中的 <system.web>里添加 <httpRuntime maxRequestLength="2097151" executionTimeout="60"/>.
3、需要下载的东西有ffmpeg.exe和pthreadGC2.dll.