mvc5 + ef6 + autofac搭建项目(四).1视屏上传生成截图
即上一篇中上传涉及到的 一个视频生成截图的问题,这个很简单,这是上一篇中的代码片段
#region 视频上传,生成默认展示图片(自动剪切)
try
{
string fileSavePath = DateTime.Now.ToString("yyyyMMdd");//当天时间最为文件夹
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");//生成的文件名称 string videoPath = Consts.VIDEOINFOSAVEPATH + fileSavePath + "/";
string gengeratedPicPath = Consts.VIDEOPICTURESAVEPATH + fileSavePath + "/";
Thread.Sleep();
fileUpLoadVideo = Request.UpLoad(videoPath, 0, gengeratedPicPath, fileName, "480x360");
}
catch (Exception ex)
{
fileUpLoadVideo = new FileUpLoadResult()
{
Status = false,
FileSavePath = null,
ErrorMsg = ex.Message
};
}
#endregion
其中红色部分就是上传和截图的实现,以下脚本是上传:
/// <summary>
/// 文件上传
/// </summary>
/// <param name="httpRequestBase">request</param>
/// <param name="saveFilePath">文件保存路径</param>
/// <param name="saveNo">如果是多文件上传,用于指定,上传第几个元素 比如 0 ,1</param>
/// <returns></returns>
public static FileUpLoadResult UpLoad(this HttpRequestBase httpRequestBase, string saveFilePath, int? saveNo, string generatedPicturePath, string fileName, string generatedPicSize = "480x360")
{
FileUpLoadResult result = new FileUpLoadResult();
if (httpRequestBase.Files.Count > )
{
if (saveNo != null)
{
#region 单文件上传
HttpPostedFileBase file = httpRequestBase.Files[int.Parse(saveNo.ToString())];
if (file.ContentLength > )
{
int startIndex = file.FileName.LastIndexOf(".") + ;
string fileExtension = file.FileName.Substring(startIndex, file.FileName.Length - startIndex);//获取文件文件后缀
string path = HttpContext.Current.Server.MapPath(saveFilePath + fileName + "." + fileExtension); //判断文件路径是否存在,否,重新创建
if (!Directory.Exists(HttpContext.Current.Server.MapPath(saveFilePath)))
{
try
{ Directory.CreateDirectory(HttpContext.Current.Server.MapPath(saveFilePath)); }
catch { }
} //file.SaveAs(path); try
{
Thread.Sleep();
Stream stream = file.InputStream;
byte[] b = new byte[stream.Length];//新建一个字节数组
stream.Read(b, , b.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin); using (FileStream fstream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
fstream.Write(b, , b.Length);
}
stream.Close(); }
catch { } #region MyRegion
//如果是视频,生成截图
if (new string[] { "mp4", "flv" }.Contains(fileExtension))
{
result.ShowImagePath = CatchImage.CatchImg(path, generatedPicturePath, fileName, generatedPicSize); if (!string.IsNullOrEmpty(result.ShowImagePath))
{
//result.ShowImagePath = generatedPicturePath + title;
result.Status = true;
result.FileSavePath = new string[] { saveFilePath + fileName + "." + fileExtension };
result.ErrorMsg = "";
}
else { try { File.Delete(path); } catch { } result.Status = false;
result.FileSavePath = null;
result.ErrorMsg = "生成截图失败,视频上传失败!";
}
}
else {
result.Status = true;
result.FileSavePath = new string[] { saveFilePath + fileName + "." + fileExtension };
result.ErrorMsg = "图片上传成功";
}
#endregion
}
#endregion
}
else
{
#region 文件批量上传
string[] urllist = new string[httpRequestBase.Files.Count];
for (int i = ; i < httpRequestBase.Files.Count; i++)
{
HttpPostedFileBase file = httpRequestBase.Files[i];
if (file.ContentLength > )
{
int startIndex = file.FileName.LastIndexOf(".") + ;
string fileExtension = file.FileName.Substring(startIndex, file.FileName.Length - startIndex);//获取文件文件后缀
string title = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + fileExtension;
string path = HttpContext.Current.Server.MapPath(saveFilePath) + title; //判断文件路径是否存在,否,重新创建
if (!Directory.Exists(HttpContext.Current.Server.MapPath(saveFilePath)))
{
try
{ Directory.CreateDirectory(HttpContext.Current.Server.MapPath(saveFilePath)); }
catch { }
}
//file.SaveAs(path);
try
{
Stream stream = file.InputStream;
byte[] b = new byte[stream.Length];//新建一个字节数组
stream.Read(b, , b.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin); using (FileStream fstream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
fstream.Write(b, , b.Length);
}
stream.Close(); }
catch { } urllist[i] = saveFilePath + title;
}
}
#endregion
result = new FileUpLoadResult()
{
Status = true,
FileSavePath = urllist,
ErrorMsg = "图片上传成功"
};
}
}
else
{
result = new FileUpLoadResult()
{
Status = false,
FileSavePath = null,
ErrorMsg = "请选择上传文件!"
};
}
return result;
}
看一下部分片段(上段代码中的片段):
//如果是视频,生成截图
if (new string[] { "mp4", "flv" }.Contains(fileExtension))
{
result.ShowImagePath = CatchImage.CatchImg(path, generatedPicturePath, fileName, generatedPicSize); if (!string.IsNullOrEmpty(result.ShowImagePath))
{
//result.ShowImagePath = generatedPicturePath + title;
result.Status = true;
result.FileSavePath = new string[] { saveFilePath + fileName + "." + fileExtension };
result.ErrorMsg = "";
}
else { try { File.Delete(path); } catch { } result.Status = false;
result.FileSavePath = null;
result.ErrorMsg = "生成截图失败,视频上传失败!";
}
}
else {
result.Status = true;
result.FileSavePath = new string[] { saveFilePath + fileName + "." + fileExtension };
result.ErrorMsg = "图片上传成功";
}
这里只是对mp4 flv的格式视频做了判断,直接写死了,里面的生成切图的方法:CatchImage.CatchImg(path, generatedPicturePath, fileName, generatedPicSize);见下面的代码:
/// <summary>
/// 图片截取 或转换格式(flv)
/// </summary>
public class CatchImage
{
//转换电影
#region //运行FFMpeg的视频解码,(这里是绝对路径)
/// <summary>
/// 转换文件并保存在指定文件夹下面(这里是绝对路径)
/// </summary>
/// <param name="fileName">上传视频文件的路径(原文件)</param>
/// <param name="playFile">转换后的文件的路径(网络播放文件)</param>
/// <param name="imgFile">从视频文件中抓取的图片路径</param>
/// <param name="videoWidthAndHeight">视频 高度、宽度 格式: 480x360</param>
/// <param name="generatedPictureWidthAndHeight">生成图片的高宽 格式: 480x360</param>
/// <returns>成功:返回图片虚拟地址; 失败:返回空字符串</returns>
public static string ChangeFilePhy(string fileName, string playFile, string imgFile, string videoWidthAndHeight, string generatedPictureWidthAndHeight)
{
//取得ffmpeg.exe的路径,路径配置在Web.Config中,如:<add key="ffmpeg" value="E:\51aspx\ffmpeg.exe" />
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath("~/Tools/ffmpeg.exe");
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return "";
} //获得图片和(.flv)文件相对路径/最后存储到数据库的路径,如:/Web/User1/00001.jpg string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv"); //截图的尺寸大小,配置在Web.Config中,如:<add key="CatchFlvImgSize" value="240x180" />
System.Diagnostics.Process FilesProcess = new System.Diagnostics.Process();
FilesProcess.StartInfo.FileName = ffmpeg;
FilesProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilesProcess.StartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 30 -s " + videoWidthAndHeight + " " + flv_file;
try
{
FilesProcess.Start();
while (!FilesProcess.HasExited)
{ }
//截图
//CatchImg(fileName, imgFile, generatedPictureWidthAndHeight);
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
return ex.Message; }
return "";
}
#endregion //截图程序
public static string CatchImg(string fileName, string imgFile, string imgFileName, string generatedPictureWidthAndHeight)
{
//
string ffmpeg = System.Web.HttpContext.Current.Server.MapPath("~/Tools/ffmpeg.exe");
//
if (!Directory.Exists(HttpContext.Current.Server.MapPath(imgFile)))
{
try
{ Directory.CreateDirectory(HttpContext.Current.Server.MapPath(imgFile)); }
catch { }
} string flv_img = imgFile + imgFileName + ".jpg";
string cutImgFullPath = HttpContext.Current.Server.MapPath(flv_img);
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Thread.Sleep();
//
//可用参考指令
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + generatedPictureWidthAndHeight + " " + cutImgFullPath;//可用指令
//ImgstartInfo.Arguments = ("-i " + fileName + " -y -f image2 -ss 00:00:06 -t 0.001 -s " + generatedPictureWidthAndHeight + " " + flv_img); //return ExeCommand(ToolsPath + " -i " + strFullVideoFile + " -y -f image2 -ss " + StartPoint + " -t 0.001 -s " + CutImgWidth + "x" + CutImgHeight + " " + strFullOutputImg);
//ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + generatedPictureWidthAndHeight + " " + flv_img;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
Thread.Sleep();
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
return ex.Message;
}
//
if (System.IO.File.Exists(cutImgFullPath))
{
return flv_img;
} return "";
} }
这是一个完整的类,可以直接拿来使用,包含视频格式转换以及 视频剪切,剪切部分的指令,写法有好多种,目前未注释的指令测试百分百可用,但是使用有一个注意点,细心的 猿类同仁应该看得出来,指令 与指令之间有空格,那么,如此一来,
" -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + generatedPictureWidthAndHeight + " " + cutImgFullPath;//可用
这个命令中的参数,也就是说 加号中间的变量,对应的值不可以出现空格,之语中文可不可以,我已经忘记了,这是好几个月之前搞着玩的东西了。
process调用的工具 ffmpeg,一百度,一大把。
mvc5 + ef6 + autofac搭建项目(四).1视屏上传生成截图的更多相关文章
- mvc5 + ef6 + autofac搭建项目(四)
在列表页面,点击新增,弹出窗口实现视屏上传,这里存在一个问题,就是大文件上传的问题,iis出于安全问题,有限制,当然这不是大问题,解决也很容易: 见截图: 请忽略视屏文件,看得懂的请装作不懂. 源码 ...
- mvc5 + ef6 + autofac搭建项目(三)
前面已经基本完成了框架的搭建,后面就是实现了,后面主要说下前端的东西bootstrap的使用和相关插件. 看图: 实现比较简单,在主页面只引入共用部分的 js等相关包,毕竟不是所有页面都需要列表以及其 ...
- mvc5 + ef6 + autofac搭建项目(repository+uow)(一)
直奔主题了,不那么啰嗦. 整体框架的参考来源是 O# 的框架,在此感谢锋哥一直以来的开源,让我们有的学 如下图: (图一) 一下分三个步骤说明,分别为 dbContext,repository,uo ...
- mvc5 + ef6 + autofac搭建项目(repository+uow)(二)
续上篇: DBContext 在上篇 图一类库根目录创建的 DbContextBase /// <summary> /// 数据库上下文基类 /// </summary> // ...
- Java Web 项目的文件/文件夹上传下载
需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...
- Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作
Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...
- Docker-生成镜像、服务搭建(redis集群、kibana、运行项目jar包)、上传镜像至阿里云
目录 生成自己的镜像 1.下载官方tomcat镜像 2.运行镜像后将webapp目录里新增文件(官方镜像是没有页面的 具体操作见) 3.使用docker ps -a 查看刚刚修改后的容器id 4.执行 ...
- 搭建自己的NuGet服务器,上传自定义NuGet包
第一步搭建NuGet服务器 创建空Web项目 安装Nuget服务,目前最新版本2.8.2 安装完成,会自动生产服务,出现如下界面 发布该网站,并部署至IIS 将.nupkg文件发布至网站的Packag ...
- Spring Boot2(十四):单文件上传/下载,文件批量上传
文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...
随机推荐
- maven install 跳过 测试 test
你可能想要配置 Maven 使其完全跳过单元测试. 可能你有一个很大的系统,单元测试需要花好多分钟来完成,而你不想在生成最终输出前等单元测试完成. 你可能正工作在一个遗留系统上面,这个系统有一系列的失 ...
- Android4.0 -- UI控件之 Menu 菜单的的使用(二)
上一讲我们讲解了android中在代码或者xml文件中定义菜单,这一讲我们继续来讲解一下定义菜单的其他方式:创建上下文的菜单.查看API文档 Menus :Creating Contextual Me ...
- (转载)最黑的黑客米特尼克:多次耍FBI 终被高手擒
(转载)http://bbs.chinabyte.com/thread-816847-1-1.html 凯文·米特尼克 50岁 第一个被FBI通缉的黑客,被称为“头号电脑骇客”,曾入侵北美防空指挥系统 ...
- is not mapped 解决方法
1.确定是否已配置相关XML 2.注意类名大小写问题 3.如果是注解 第一种方式 @Entity(name = "Tb_User") public class User { ...
- 点分治练习: boatherds
[题面] 求一颗树上距离为K的点对是否存在 输入数据 n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行每行询问一个K 输出数据 对于每个K每行输出一个答案,存在输出“AYE ...
- Linux下SVN命令总结
目录 一.从版本库获取信息... 1 二.从版本库到本地... 2 三.从本地到版本库... 2 四.高级应用... 4 一.从版本库获取信息 svn help command 获取子命令说明 svn ...
- 一个好看的Input样式
<div class="search"> <input type="text"></div> .search{ text-a ...
- java数据结构-非线性结构之树
一.树状图 树状图是一种数据结构,它是由n(n>=1)个有限节点组成的具有层次关系的集合.因其结构看起来想个倒挂的树,即根朝上,叶子在下,故被称为"树". 特点: 1. 每个 ...
- 【JAVA - SSM】之MyBatis插入数据后获取自增主键
很多时候,我们都需要在插入一条数据后回过头来获取到这条数据在数据表中的自增主键,便于后续操作.针对这个问题,有两种解决方案: (1)先插入,后查询.我们可以先插入一条数据,然后根据插入的数据的各个字段 ...
- 在VS Nuget命令行下进行EF数据库迁移
找到项目中,用到数据库DLL的地方,然后选中该项目,打开Nuget命令行输入以下的命令: 其中cardId为迁移名称,自己取