ASP.NET MVC 上传视频文件并将其从 HttpPostedFileBase 转换为图像文件(帧)
ASP.NET MVC upload video file and convert it from HttpPostedFileBase to image files(frames)
我想上传一个视频文件,并在我的 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?
// (I upload file to Microsoft Azure Blob)
}
据我了解,您可以使用利用 FFmpeg 库的包装器 VideoFileReader 来读取视频文件。这是官方文档中的示例:
// 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 or this similar issue.
回到您的场景,您可能需要将 HttpPostedFileBase.InputStream 临时存储到临时文件中,然后使用此临时文件初始化 VideoFileReader
实例。
要将文件上传到 Azure Blob 存储,您可以利用 UploadFromFile
或 UploadFromStream
等。详细教程,可以关注here.
更新:
我检查了AForge.Video.FFMPEG,发现它不能再工作了。根据我的测试,您可以安装 Accord.Video.FFMPEG 包,它源自 AForge.NET 框架并且是 Accord.NET 框架的一部分来处理视频。您只需要更改引用的命名空间,这是我在控制台应用程序上的测试代码:
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();
}
}
}
结果:
为了获取快照,我使用了FFmpeg tool。
这是一个用于演示的控制台应用程序,同样的逻辑也可以用于 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可以从here.
下载
我想上传一个视频文件,并在我的 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?
// (I upload file to Microsoft Azure Blob)
}
据我了解,您可以使用利用 FFmpeg 库的包装器 VideoFileReader 来读取视频文件。这是官方文档中的示例:
// 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 or this similar issue.
回到您的场景,您可能需要将 HttpPostedFileBase.InputStream 临时存储到临时文件中,然后使用此临时文件初始化 VideoFileReader
实例。
要将文件上传到 Azure Blob 存储,您可以利用 UploadFromFile
或 UploadFromStream
等。详细教程,可以关注here.
更新:
我检查了AForge.Video.FFMPEG,发现它不能再工作了。根据我的测试,您可以安装 Accord.Video.FFMPEG 包,它源自 AForge.NET 框架并且是 Accord.NET 框架的一部分来处理视频。您只需要更改引用的命名空间,这是我在控制台应用程序上的测试代码:
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();
}
}
}
结果:
为了获取快照,我使用了FFmpeg tool。
这是一个用于演示的控制台应用程序,同样的逻辑也可以用于 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可以从here.
下载