How do I get the dimensions of a mp4 video file?
Asked Answered
A

2

8

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)

Altazimuth answered 23/2, 2011 at 0:53 Comment(0)
R
3

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?

Replacement answered 23/2, 2011 at 3:27 Comment(4)
I take it Microsoft doesn't have a standard set of API for reading media files then?Altazimuth
I don't think it's Microsoft's duty to have such as API. They do have the ability to do this obviously since you can see it being used in Windows Explorer. Having dealt with hundreds of thousands (if not millions) of videos files I can tell you something as simple as this is apparently not that easy, since there is no one tool that does this perfectly.Replacement
Thats disappointing. I can't install anything on the server, just the website so I wont be able to install ffmpeg or MediaInfo to use them. Wish there was a nice clean set of API to use.Altazimuth
Neither ffmpeg nor Media info need to be "installed". Just copy the exe files along with you all files. In media info's case you'll need a dll and exe. But really, it's not an install per-se. They need no other supporting files. So I think you should be able to use them. I'd go with MediaInfo because I've found it to be almost 100% in terms of being able to handle the variety of files.Replacement
M
1

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;
}
Meltwater answered 26/9, 2019 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.