asp.net-mvc-5 - ASP.NET MVC上传视频文件并将其从HttpPostedFileBase转换为图像文件(帧)

标签 asp.net-mvc-5 video-processing azure-blob-storage

我想上传视频文件并获取第一帧(或可能指定的帧)作为我的 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 存储,您可以利用 UploadFromFileUploadFromStream 等。详细教程,可以关注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();
        }
    }
}

结果:

enter image description here

关于asp.net-mvc-5 - ASP.NET MVC上传视频文件并将其从HttpPostedFileBase转换为图像文件(帧),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503131/

相关文章:

Azure 应用服务诊断 blob 不记录基于 nlog 的日志

FFMPEG zoompan 仅适用并显示众多图像中的 3 个

video - 是否有任何算法可以从视频中去除灯光?

azure - 将 Azure 存储 Blob 作为文件系统访问

c# - 我如何创建一个具有 Int 值的 SelectListItem()

android - MediaMuxer 视频文件大小减小(重新压缩,降低分辨率)

c# - 如何在Azure blob存储中保留空目录?

jquery - Ajax.BeginForm 在 chrome MVC 5 中不起作用

asp.net - 使用路由或其他方法强制使用 http 或 https?

asp.net-mvc - 更改为发布时无法加载文件或程序集 (nopcommerce 3.5)