已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 职业技术学校建设项目技术方案
- 光学平直仪项目可行性分析报告范文(总投资3000万元)
- 张凌赫发言徐若晗台下比心
- 包装工具及设备项目可行性分析报告范文(总投资7000万元)
- 中文翻译真的好有意境
- 南宁职业技术学院《证券实务模拟》2025-2026学年第一学期期末试卷
- 枣庄科技职业学院《小学信息技术活动与竞赛专题》2025-2026学年第一学期期末试卷
- 垃圾分类投放实际操作指南
- 店面管理人才选拔高级岗位面试攻略
- 岛津面试策略洞察行业趋势应对职业挑战
- 2025广东广州南沙区南沙街道社区专职工作人员招聘32人考试笔试参考题库及答案解析
- 2025版《煤矿安全规程》学习辅导课件(运输、提升和空压机)
- 平台合作协议书合同
- 护理学毕业论文
- 统编版(2024)八年级上册道德与法治第三单元《勇担社会责任》测试卷(含答案)
- 2026年企业人力资源管理师之四级人力资源管理师考试题库300道【考点梳理】
- 2025年福州市长乐市辅警招聘考试题库附答案解析
- 西藏自治区公务员2025年考试行测资料分析模拟试卷(含答案)
- 2025年法宣在线考试题库及答案
- 河南省文旅局的考试题及答案
- 五金模具自动报价表格
评论
0/150
提交评论