Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 568
Justin808 Avatar asked Feb 23 '11 00:02

Justin808


People also ask

What is the dimension of a video?

Understanding Video DimensionsThe width and height of videos are usually measured in pixels and are collectively termed as the "dimensions" of the video. Thus, if a video is 320 pixels wide and 240 pixels in height, it is said to have dimensions of 320 x 240 pixels.

How big is a 1080p MP4 file?

One minute of video is approximately: 60 MB with 720p at 30 fps. 130 MB with 1080p at 30 fps. 175 MB with 1080p at 60fps.


2 Answers

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?

like image 52
Shiv Kumar Avatar answered Sep 28 '22 17:09

Shiv Kumar


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;
}
like image 36
L. Kneringer Avatar answered Sep 28 '22 19:09

L. Kneringer