即上一篇中上传涉及到的 一个视频生成截图的问题,这个很简单,这是上一篇中的代码片段

#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视屏上传生成截图的更多相关文章

  1. mvc5 + ef6 + autofac搭建项目(四)

    在列表页面,点击新增,弹出窗口实现视屏上传,这里存在一个问题,就是大文件上传的问题,iis出于安全问题,有限制,当然这不是大问题,解决也很容易: 见截图: 请忽略视屏文件,看得懂的请装作不懂. 源码 ...

  2. mvc5 + ef6 + autofac搭建项目(三)

    前面已经基本完成了框架的搭建,后面就是实现了,后面主要说下前端的东西bootstrap的使用和相关插件. 看图: 实现比较简单,在主页面只引入共用部分的 js等相关包,毕竟不是所有页面都需要列表以及其 ...

  3. mvc5 + ef6 + autofac搭建项目(repository+uow)(一)

    直奔主题了,不那么啰嗦. 整体框架的参考来源是  O# 的框架,在此感谢锋哥一直以来的开源,让我们有的学 如下图: (图一) 一下分三个步骤说明,分别为 dbContext,repository,uo ...

  4. mvc5 + ef6 + autofac搭建项目(repository+uow)(二)

    续上篇: DBContext 在上篇 图一类库根目录创建的 DbContextBase /// <summary> /// 数据库上下文基类 /// </summary> // ...

  5. Java Web 项目的文件/文件夹上传下载

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  6. Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作

    Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...

  7. Docker-生成镜像、服务搭建(redis集群、kibana、运行项目jar包)、上传镜像至阿里云

    目录 生成自己的镜像 1.下载官方tomcat镜像 2.运行镜像后将webapp目录里新增文件(官方镜像是没有页面的 具体操作见) 3.使用docker ps -a 查看刚刚修改后的容器id 4.执行 ...

  8. 搭建自己的NuGet服务器,上传自定义NuGet包

    第一步搭建NuGet服务器 创建空Web项目 安装Nuget服务,目前最新版本2.8.2 安装完成,会自动生产服务,出现如下界面 发布该网站,并部署至IIS 将.nupkg文件发布至网站的Packag ...

  9. Spring Boot2(十四):单文件上传/下载,文件批量上传

    文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...

随机推荐

  1. Android Http Server

    Android Http Server 1 引言          Android如何构建Http服务器呢?本文的小例子,约莫着,还是能做个参考的^^.恩,例子实现的是PC浏览手机文件,支持了下载和删 ...

  2. HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))

    Four Operations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. [PeterDLax著泛函分析习题参考解答]第4章 Hahn-Bananch 定理的应用

    1. 证明: 若在 4.1 节中取 $S=\sed{\mbox{正整数}}$, $Y$ 是收敛数列构成的空间, $\ell$ 由 (14) 式定义, 则由 (4) 给出的 $p$ 和由 (11) 定义 ...

  4. [转]NHibernate之旅(1):开篇有益

    本节内容 NHibernate是什么 NHibernate的架构 NHibernate资源 欢迎加入NHibernate中文社区 作者注:2009-11-06已更新 NHibernate开篇有益 学习 ...

  5. OracleParameter 的使用(参数名要以:开头,不允许包含@等特殊字符)[转]

    在使用OracleParameters时,CommandText 中的参数要以":"开头,不能包含@等特殊字符.而在其它地方引用到此参数时可以不必加上":",程 ...

  6. HDOJ 1004题 Let the Balloon Rise strcmp()函数

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  7. 是否存在两个树的和是固定数 hashmap使用 leecode

    two sum https://oj.leetcode.com/submissions/detail/8220548/ public class Solution { public int[] two ...

  8. 2D游戏编程6—windows程序模板

    // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN // just say ...

  9. CC2530芯片介绍

    CC2530,体积更小,功能全, C8051内核,使用起来跟平时的单片机没什么两样,就是程序复杂一些,需要一些时日才能看懂,吃透就更长了,而且对于CC2530TI公司提供了一套完整的解决方案,有布线图 ...

  10. OpenCL memory object 之 传输优化

    转载自:http://www.cnblogs.com/mikewolf2002/archive/2011/12/18/2291741.html 首先我们了解一些优化时候的术语及其定义: 1.defer ...