如何获取 mp4 视频文件的尺寸?

发布于 2024-10-19 03:45:24 字数 246 浏览 1 评论 0 原文

我有一个可以接受并保存 MP4 视频文件的表单。我还需要能够获取视频的尺寸。这将在运行 ASP.NET 2.0 的服务器上运行,因此任何外部库都必须能够放置在 Bin 文件夹中,因为它们无法安装在服务器上。

有什么想法如何获取信息吗?如果同一个库可以让我将视频转码为 flv,那将是一个巨大的好处。

更新:服务器是带有.NET Framework的XP Service Pack 2 (2,0,50727,0)

I have a form that will accept and save a MP4 video file. I need to be able to get the dimensions of the video as well. This will be running on a server running ASP.NET 2.0 so any external libs must be able to be placed in the Bin folder as they can not be installed on the server.

Any ideas how to get the information? If the same lib would let me transcode the video to flv that would be a huge bonus.

Update: The server is XP Service Pack 2 with .NET Framework (2,0,50727,0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-10-26 03:45:24

有几种方法可以做到这一点,但没有库本身可以与 C# 一起使用任何/所有 .mp4 视频文件。这没有什么是100%可靠的。

两者都是命令行选项(某种意义上)。基本上,您要做的就是使用 System.Diagnostics.Process 从 ASP.NET 应用程序运行一个进程。

其中之一是使用 ffmpeg。对于 ffmpeg,如果您只是给它一个文件(作为命令行参数,如果将返回有关该文件的各种元数据。可以解析此信息以提取维度。

另一种是使用 MediaInfo。这是一个很棒的工具。但同样,您必须使用命令行版本(CLI 版本)并为其提供文件名作为命令行参数。它有一个选项来生成 xml 响应,因此您可以轻松解析此信息和其他信息(如果可以提供的话),

尽管我看不到从 mp4 进行转码的意义。 ?

There are a few ways to do this, but no library per-se that will work with C# for any/all .mp4 video files. That is nothing that is 100% reliable.

Both are command line options (in a sense). Basically, what you'd be doing is running a process from your ASP.NET application using System.Diagnostics.Process.

On of them is to use ffmpeg. With ffmpeg if you just give it a file (as a command line argument if will return various metadata about the file. This information can be parsed to extract the dimensions.

The other is to use MediaInfo. It's a great tool for this. But again, you'll have to use the Command line version (CLI version) and pretty much give it the file name as a command line argument. It has an otion to produce and xml response so you can easily parse this and other information if can provide.

ffmpeg can transcode your video as well. Though I don't see the point of transcoding from mp4. flv?

吻风 2024-10-26 03:45:24

Alturos.VideoInfo 是 ffprobe 的包装器,可以轻松集成到 C# 项目中。

它可以像这样从 NuGet 安装:

PM> Install-Package Alturos.VideoInfo

要使用它,您需要 ffprobe.exe。如果它不在您的项目中,您可以运行以下代码来下载它:

var ffmpegDownloader = new FileDownloader();
var ffmpegUrl = ffmpegDownloader.GetFfmpegPackageUrl();
var ffmpegDownloadResult = await ffmpegDownloader.DownloadAsync(ffmpegUrl, @"ffprobe\ffprobe.exe");
var ffprobePath = ffmpegDownloadResult.FfprobePath;

然后可以通过获取视频流轻松检索视频的尺寸:

var videoAnalyzer = new VideoAnalyzer(ffprobePath);
var analyzeResult = videoAnalyzer.GetVideoInfo(/* your video file path here */);

if (analyzeResult.Successful)
{
    var videoStream = analyzeResult.VideoInfo.Streams.Single(o => o.CodecType == "video");

    var width = videoStream.Width;
    var height = videoStream.Height;
}

Alturos.VideoInfo is a wrapper for ffprobe that makes it rather easy to integrate into a C# project.

It can be installed from NuGet like this:

PM> Install-Package Alturos.VideoInfo

To use it, you need ffprobe.exe. If it's not in your project already, you can run this code to download it:

var ffmpegDownloader = new FileDownloader();
var ffmpegUrl = ffmpegDownloader.GetFfmpegPackageUrl();
var ffmpegDownloadResult = await ffmpegDownloader.DownloadAsync(ffmpegUrl, @"ffprobe\ffprobe.exe");
var ffprobePath = ffmpegDownloadResult.FfprobePath;

The dimensions of the video can then easily be retrieved by getting the video stream:

var videoAnalyzer = new VideoAnalyzer(ffprobePath);
var analyzeResult = videoAnalyzer.GetVideoInfo(/* your video file path here */);

if (analyzeResult.Successful)
{
    var videoStream = analyzeResult.VideoInfo.Streams.Single(o => o.CodecType == "video");

    var width = videoStream.Width;
    var height = videoStream.Height;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文