Asp.net mvc 5 ASP.NET MVC上载视频文件并将其从HttpPostedFileBase转换为图像文件(帧)
我想上传一个视频文件,并在我的ASP.NET MVC项目中获取第一帧(或者指定帧)作为缩略图 控制器:Asp.net mvc 5 ASP.NET MVC上载视频文件并将其从HttpPostedFileBase转换为图像文件(帧),asp.net-mvc-5,video-processing,azure-blob-storage,Asp.net Mvc 5,Video Processing,Azure Blob Storage,我想上传一个视频文件,并在我的ASP.NET MVC项目中获取第一帧(或者指定帧)作为缩略图 控制器: [HttpPost] public ActionResult Upload(HttpPostedFileBase file) { // How to get the video file frames(or first frame) here // or Microsoft Azure Blob Storage provide the service to do this?
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
// How to get the video file frames(or first frame) here
// or Microsoft Azure Blob Storage provide the service to do this?
// (I upload file to Microsoft Azure Blob)
}
据我所知,您可以使用FFmpeg库的包装器来读取视频文件。以下是官方文件中的示例:
//创建视频阅读器实例
VideoFileReader=新的VideoFileReader();
//打开视频文件
reader.Open(“test.avi”);
//检查它的一些属性
Console.WriteLine(“宽度:+reader.width”);
控制台.WriteLine(“高度:+reader.height”);
Console.WriteLine(“fps:+读卡器.FrameRate”);
Console.WriteLine(“编解码器:+reader.CodecName”);
//从中读取100个视频帧
对于(int i=0;i<100;i++)
{
位图视频帧=reader.ReadVideoFrame();
//以某种方式处理帧
// ...
//不再需要机架时,请将其弃置
videoFrame.Dispose();
}
reader.Close();
注意:您可能需要将FFmpeg二进制文件(DLL)复制到web应用程序中。对于a org.Video.FFMPEG包,您可以遵循或类似的方法
回到您的场景,您可能需要临时存储到临时文件中,然后使用此临时文件初始化VideoFileReader
实例
要将文件上载到Azure Blob存储,您可以利用UploadFromFile
或UploadFromStream
等。您可以按照详细教程进行操作
更新: 我查看了一个org.Video.FFMPEG,发现它无法再工作了。根据我的测试,您可以安装源于AForge.NET框架的包,它是Accord.NET框架的一部分,用于处理视频。您只需更改引用的名称空间,以下是我在控制台应用程序上的测试代码:
使用Accord.Video.FFMPEG;
使用制度;
使用System.Collections.Generic;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台
{
班级计划
{
静态void Main(字符串[]参数)
{
//创建视频阅读器的实例
VideoFileReader=新的VideoFileReader();
//打开视频文件
reader.Open(AppDomain.CurrentDomain.BaseDirectory+@“Videos\FlickAnimation.avi”);
//检查它的一些属性
Console.WriteLine(“宽度:+reader.width”);
控制台.WriteLine(“高度:+reader.height”);
Console.WriteLine(“fps:+读卡器.FrameRate”);
Console.WriteLine(“编解码器:+reader.CodecName”);
//读取视频帧
对于(int i=0;i
结果:
对于获取快照,我使用了 它是一个用于演示的控制台应用程序,同样的逻辑也可以用于Web
public class Ffmpeg
{
Process _ffmpeg;
private void Exec(string input, string output, string duration)
{
_ffmpeg = new Process();
_ffmpeg.StartInfo.Arguments = $"-ss {duration} -i {input} -vframes 1 {output}";
// Path of Exe which will be in folder Q:\\ConsoleVideo\\ConsoleVideo\\utils\\ffmpeg.exe
_ffmpeg.StartInfo.FileName = "Q:\\ConsoleVideo\\ConsoleVideo\\utils\\ffmpeg.exe";
_ffmpeg.StartInfo.UseShellExecute = false;
_ffmpeg.StartInfo.RedirectStandardOutput = true;
_ffmpeg.StartInfo.RedirectStandardError = true;
_ffmpeg.StartInfo.CreateNoWindow = true;
_ffmpeg.Start();
_ffmpeg.WaitForExit();
_ffmpeg.Close();
}
public void GetThumbnail(string video, string jpg, string duration)
{
Exec(video, jpg, duration); //hh:mm:ss.fff
}
}
class Program
{
static void Main(string[] args)
{
Ffmpeg f = new Ffmpeg();
// GetThumbnail ("Video Input Path" , "Image OutPut Path" , "Time Frame of Snapshot" ) [hh:mm:ss.fff]
f.GetThumbnail("Q:\\ConsoleVideo\\ConsoleVideo\\videos\\Wildlife.wmv", "C:\\Users\\1438\\Downloads\\thumb.jpg", "00:00:25.000");
}
}
项目结构
存储在utils文件夹中的FFmpeg exe可以从下载。检查此帖子:我将“externals”文件夹中的所有dll复制到我的项目中,但它仍然显示关于依赖性的错误。我刚刚检查了它,请查看我关于新解决方案的更新部分。我可以将帧保存到MemoryStream,而不是另存为文件吗?我尝试了$“-ss{duration}-I{input}-vframes 1-f{extension}管道:“引用:但输出流colud不使用。@gary它将根据您传递给它的时间从视频中获取快照,不返回memoryStream所以…,如果我想将快照(缩略图)上载到azure blob存储,我必须先保存到web服务器,然后读取要上载的文件?@gary right您需要在服务器上存储获取缩略图,然后获取缩略图的base64string格式并推送到您要存储的位置。