我想上传视频文件并获取第一帧(或可能指定的帧)作为我的 ASP.NET MVC 项目中的缩略图。
Controller :
[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)
}
最佳答案
根据我的理解,您可以使用包装器 VideoFileReader利用FFmpeg库读取视频文件。这是官方文档中的示例:
// create instance of video reader
VideoFileReader reader = new VideoFileReader();
// open video file
reader.Open("test.avi");
// check some of its attributes
Console.WriteLine("width: " + reader.Width);
Console.WriteLine("height: " + reader.Height);
Console.WriteLine("fps: " + reader.FrameRate);
Console.WriteLine("codec: " + reader.CodecName);
// read 100 video frames out of it
for(int i = 0; i < 100; i++)
{
Bitmap videoFrame = reader.ReadVideoFrame();
// process the frame somehow
// ...
// dispose the frame when it is no longer required
videoFrame.Dispose();
}
reader.Close();
注意:您可能需要将 FFmpeg 二进制文件 (DLL) 复制到您的 Web 应用程序中。对于AForge.Video.FFMPEG包,您可以按照here或类似的issue .
回到你的场景,你可能需要临时存储HttpPostedFileBase.InputStream到临时文件中,然后使用该临时文件来初始化 VideoFileReader
实例。
要将文件上传到 Azure Blob 存储,您可以利用 UploadFromFile
或 UploadFromStream
等。详细教程,可以关注here .
更新:
我检查了AForge.Video.FFMPEG,发现它无法再工作了。根据我的测试,您可以安装 Accord.Video.FFMPEG包源自 AForge.NET Framework,是 Accord.NET Framework 的一部分,用于处理视频。您只需要更改引用的命名空间,这是我在控制台应用程序上的测试代码:
using Accord.Video.FFMPEG;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleFfmpeg
{
class Program
{
static void Main(string[] args)
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader();
// open video file
reader.Open(AppDomain.CurrentDomain.BaseDirectory+ @"Videos\FlickAnimation.avi");
// check some of its attributes
Console.WriteLine("width: " + reader.Width);
Console.WriteLine("height: " + reader.Height);
Console.WriteLine("fps: " + reader.FrameRate);
Console.WriteLine("codec: " + reader.CodecName);
//read video frames
for (int i = 0; i < reader.FrameCount; i++)
{
Bitmap videoFrame = reader.ReadVideoFrame();
// process the frame somehow
videoFrame.Save(AppDomain.CurrentDomain.BaseDirectory + $"Videos\\{i}.bmp");
// dispose the frame when it is no longer required
videoFrame.Dispose();
}
reader.Close();
Console.ReadLine();
}
}
}
结果:
关于asp.net-mvc-5 - ASP.NET MVC上传视频文件并将其从HttpPostedFileBase转换为图像文件(帧),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503131/