asp.net实现调用ffmpeg实现视频格式的转换
视频格式转换的函数
//视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = HttpContext.Current.Server.MapPath("~/UploadFiles/FLV/ffmpeg.exe"); FromName = HttpContext.Current.Server.MapPath(FromName); ExportName = HttpContext.Current.Server.MapPath(ExportName); string Command = " -i \"" + FromName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\""; //Flv格式 //string Command = " -i \"test.wmv\" -y -ab 32 -ar 22050 -b 800000 -s 320*240 \"2.flv\""; //string Command = "E:\\FFmpeg\\ffmpeg.exe -i E:\\ClibDemo\\VideoPath\\admin\\a.wmv -y -ab 56 -ar 22050 -b 500 -r 15 -s 320*240 " ExportName; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = ffmpeg; p.StartInfo.Arguments = Command; p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/UploadFiles/FLV/"); 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(); }
调用视频格式转换
protected void btnSaveUploadVod_Click(object sender, EventArgs e) { string fileName = txtATitle.Text;//上传文件的名称的命名规则是标题 bool flag = true; //视频格式是否正确的标志 if (fileName.Equals("")) { Response.Write("<script>alert(‘请先填写标题!‘)</script>"); } else { filePathTemp = "UploadFiles/武夷概况/视频"; string fpath = Path.Combine(Request.PhysicalApplicationPath, filePathTemp); UploadFile upFile = AspnetUpload.GetUploadFile("file2"); if (upFile != null) { string sourceFileName = Path.GetFileName(upFile.get_FileName()); //取出上传的视频的文件名,进而取出该文件的扩展名 string extendName = sourceFileName.Substring(sourceFileName.LastIndexOf(".") + 1); //上传后的文件名的命名规则是:标题+数字+后缀 if (!isAllowedVodType(extendName)) { flag = false; } else { VodWarehouseManage vodWarehouseBll = new VodWarehouseManage(); //先上传到服务器,然后转换格式,最后删掉原来非FLV格式的视频 //上传后的文件名的命名规则是:标题+后缀 string fileNameTemp = fileName + "." + extendName; string savePath = filePathTemp + "/" + fileNameTemp; upFile.SaveAs(Path.Combine(fpath, fileNameTemp)); if (!extendName.ToLower().Equals("flv")) {//进行视频转换 //调用视频转换函数将其它格式的视频转为FLV格式的视频 string fromName = "~/" + savePath; string exportName = "~/" + filePathTemp + "/" + fileName + ".flv"; // Response.Write("<script>alert(‘fromName=" + fromName + " and exportName=" + exportName + "‘)</script>"); //转换视频格式 VideoConvertFlv(fromName, exportName); //删除非FLV格式的视频 vodWarehouseBll.DeleteFile(Path.Combine(fpath, fileNameTemp)); savePath = filePathTemp + "/" + fileName + ".flv"; } //保存视频的信息 VodWarehouse vodWarehouse = new VodWarehouse(); vodWarehouse.VodSortId = 17;//1代表媒体的类型为武夷概况 vodWarehouse.VodPath = savePath; vodWarehouse.AttachId = Int32.Parse(articleId.Value);//隐藏域中的值 if (vodWarehouseBll.SaveVodsInfo(vodWarehouse) == false) { Response.Write("<script>alert(‘保存视频的过程出错!‘)</script>"); } else { //Response.Write("<script>alert(‘视频上传成功!‘)</script>"); //将上传的路径写入相应的上传文本框中 } } }//end foreach if (flag == true) { this.upLoadVodResult.Text = "视频上传成功"; this.vodPannel.Visible = false; } else { //this.upLoadVodResult.Text = "您选择的视频的格式不正确,请确保您的视频的格式是:flv/mov/wmv/avi/mp4"; Response.Write("<script>alert(‘您选择的视频的格式不正确,请确保您的视频的格式是:flv/mov/wmv/avi/mp4!‘)</script>"); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。