上传视频到服务器并转换成flv格式实现在线播放功能1.doc_第1页
上传视频到服务器并转换成flv格式实现在线播放功能1.doc_第2页
上传视频到服务器并转换成flv格式实现在线播放功能1.doc_第3页
上传视频到服务器并转换成flv格式实现在线播放功能1.doc_第4页
上传视频到服务器并转换成flv格式实现在线播放功能1.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

上传视频到服务器并转换成flv格式实现在线播放功能1最近公司有一个需求:用户注册了账号后,可以上传自己的视频,可供自己或他人在线观看,并带有图片可以预览视频内容。而有些格式的视频是浏览器不能解析的格式。只能将其转换成可以在线播放的格式如flv格式。所以在用户上传完视频后必须将其他格式转换成flv格式。这样再网上搜集了一下资料后最终做出了一个例子。基本原理是在服务器端调用ffmepg转换其他格式的视频成flv格式的视频。写了一个类如下:/ /VideoToFLV 的摘要说明/视频转换和存档/ public class VideoToFLV:Page public VideoToFLV() / / 检查视频格式是否合法 / / 视频文件名 / public static bool CheckVideoType(string fileName) . / / 判断视频格式是否是flv格式 / / 视频文件名 / public static bool IsFLV(string fileName) . / / 将其他视频格式转换成flv格式 / / ffmpeg工具的绝对路径 / 要转换的视频的绝对路径 / 要导出的flv文件的绝对路径 / 要导出的图片的绝对路径 / ffmpeg工具的工作目录 public static void ConvertToFLV(string ffmpeg, string FromName, string ExportFlvName, string ExportImgeName,string WorkingDirectory) . / / 抓图 / / ffmpeg工具的绝对路径 / 要导出的图片的绝对路径 / flv文件的绝对路径 / ffmpeg工具的工作目录 public static void CathImage(string ffmpeg, string ExportImgName, string flv, string WorkingDirectory) . public static bool CheckVideoType(string fileName) string extendName = Path.GetExtension(fileName); string strex = .asx, .asf, .mpg, .wmv, .mp4, .mov, .avi, .flv ; foreach (string type in strex) if (extendName = type) return true; return false; 2:public static bool IsFLV(string fileName) string extendName = Path.GetExtension(fileName); if (extendName = flv) return true; else return false; 3:public static void ConvertToFLV(string ffmpeg, string FromName, string ExportFlvName, string ExportImgeName,string WorkingDirectory) string Command = -i + FromName + -y -ab 32 -ar 22050 -b 800000 -s 480*360 + ExportFlvName + ; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = HttpContext.Current.Server.MapPath(ffmpeg); p.StartInfo.Arguments = Command; p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath(WorkingDirectory);/ffmpeg/ p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; try p.Start(); catch (Exception ex) throw ex; finally p.BeginErrorReadLine(); p.WaitForExit(); p.Close(); p.Dispose(); /抓图 CathImage(ffmpeg, ExportImgeName, ExportFlvName, WorkingDirectory); 4:public static void CathImage(string ffmpeg, string ExportImgName, string flv, string WorkingDirectory) string Command = -i + flv + -y -f image2 -ss 60 -t 0.001 -s 350x240 + ExportImgName + ; System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = HttpContext.Current.Server.MapPath(ffmpeg); p.StartInfo.Arguments = Command; p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath(WorkingDirectory);/ffmpeg/ p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; try p.Start(); catch (Exception ex) throw ex; finally p.BeginErrorReadLine(); p.WaitForExit(); p.Close(); p.Dispose(); 有如下的页面:(部分)点击上传按钮:/上传 protected void Button1_Click(object sender, EventArgs e) if (this.IsValid) string fileName = this.FileUpload1.FileName.Trim(); /上传的文件格式检查 if (VideoToFLV.CheckVideoType(fileName) /检查文件大小 int filesize = this.FileUpload1.PostedFile.ContentLength; if (filesize = 10485760) this.Execute(); /方法见下 else this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), sizefail, alert(文件大小不能超过10M);, true); else this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), typefail, alert(不支持本格式的文件,请您将文件转换成(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv)再进行上传);, true); / / 转换文件格式或者上传 / / / / / public void Execute() string fileName = this.FileUpload1.FileName.Trim(); string ffmpeg = ConfigurationManager.AppSettingsffmpeg.ToString();/web.config中有相关的配置 string WorkingDirectory = ConfigurationManager.AppSettingsWorkingDirectory.ToString();/web.config中有相关的配置 if (VideoToFLV.IsFLV(fileName) /flv格式(直接上传到flv文件夹中) string guid = Guid.NewGuid().ToString().Trim(); string flvurl = ./interviewing/videoflv/ + guid + .flv;/视频路径(保存到数据库) string flvpath = Server.MapPath(flvurl); string imgurl = ./interviewing/videoimg/ + Guid.NewGuid().ToString() + .jpg;/图片路径(保存到数据库) string imgpath = Server.MapPath(imgurl);/图片上传的绝对路径 this.FileUpload1.SaveAs(flvpath); try VideoToFLV.CathImage(ffmpeg, imgpath, flvpath, WorkingDirectory); catch (Exception) this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), failaaa, alert(操作失败);, true); /存档:将相关数据存入数据库 else /先上传原格式 string guid = Guid.NewGuid().ToString().Trim(); string name = guid + System.IO.Path.GetExtension(fileName);/不重复的视频名称 string uppath = ./interviewing/video/ + name; string mappath = Server.MapPath(uppath);/视频上传的绝对路径 this.FileUpload1.SaveAs(mappath); /其他格式(转换成flv) string imgurl = ./interviewing/videoimg/ + Guid.NewGuid().ToString() + .jpg;/图片路径(保存到数据库) string imgpath = Server.MapPath(imgurl);/图片上传的绝对路径 string flvurl = ./interviewing/videoflv/ + guid + .flv;/视频路径(保存到数据库) string flvpath = Server.MapPath(flvurl); / /将原文件转换成flv try VideoToFLV.ConvertToFLV(ffmpeg, mappath, flvpath, imgpath, WorkingDirectory); catch (Exception) this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), fail, alert(操作失败);, true); /存档 if (存档成功) this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), uploadz, alert(恭喜您上传成功);window.location.href=resume.aspx;, true); else this.Page.ClientScript.RegisterClientScriptBlock

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论