14

I have been trying to get an answer to this without really finding any. Excuse me if this sounds stupid or obvious.

I have a nodejs application and basically I would like to simply get the resolution of a video. Imagine I have film stored on disk and I would like to be able to know if it is in 720p or 1080p or anything else.

I understood that I might need to use ffmpeg to do so, but then I also understood that ffmpeg was mostly used to "record, convert and stream audio and video files". That does not mean retrieve video resolution.

Thank you for your help

Edit 1: The node.js app is a desktop app and needs to be portable to Linux, windows and OS X. If possible a portable answer would be more appreciated but of course any answer is welcome.

Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38

5 Answers5

18

To be honest I think the best method I found was to use fluent-ffmpeg with ffprobe as you are able to set the the path to the executable. The only problem is that ffmpeg has to be shipped with the app. So different executables have to be shipped, one for each distribution/os/derivation. If anyone has anything better I am open to answers.

Getting the width, height and aspect ratio using fluent-ffmpeg is done like so:

var ffmpeg = require('fluent-ffmpeg');

ffmpeg.setFfprobePath(pathToFfprobeExecutable);

ffmpeg.ffprobe(pathToYourVideo, function(err, metadata) {
    if (err) {
        console.error(err);
    } else {
        // metadata should contain 'width', 'height' and 'display_aspect_ratio'
        console.log(metadata);
    }
});
Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
  • I have used this package for a while now and i noticed a bug with it,sometimes it switches video height and width. Use it at your own risk. – Chidozie Duru Sep 23 '22 at 21:29
  • @ChidozieDuru is it possible that you are neglecting the "rotate" tag that results in the provided width and height values being reversed? Many videos are saved with the camera sensor's orientation and a rotation metadata tag similar to EXIF for images. For example, if I record a portrait video with my iPhone: ffprobe reports a landscape width and height (because that is how the sensor is oriented on the phone) and then includes a "rotation": "-90" field to indicate the intended orientation for viewing. – willbattel Jun 12 '23 at 08:12
  • @willbattel i actually added it – Chidozie Duru Jun 14 '23 at 10:39
8

There's a npm package called get-video-dimensions that also use ffprobe and it's much easier to use. It also support promises and async/await.

import getDimensions from 'get-video-dimensions';

Using promise:

getDimensions('video.mp4').then(dimensions => {
  console.log(dimensions.width);
  console.log(dimensions.height);
})

or async/await:

const dimensions = await getDimensions('video.mp4');
console.log(dimensions.width);
console.log(dimensions.height);
Sinandro
  • 2,426
  • 3
  • 21
  • 36
3

I use node-ffprobe to accomplish this for images:

var probe = require('/usr/lib/node_modules/node-ffprobe');
probe(filePath, function (err, data) {
    //the 'data' variable contains the information about the media file
});
jrkt
  • 2,615
  • 5
  • 28
  • 48
2

fileMetaData will have width, height, codec info, aspect ratio etc ...

const ffprobe = require('ffprobe')
const ffprobeStatic = require('ffprobe-static')
const fileMetaData = await ffprobe(fileName, { path: ffprobeStatic.path })

fileName could be video('webm', 'mov', 'wmv', 'mpg', 'mpeg', 'mp4','flv' etc..) or image(jpg, gif, png etc..) path. fileName example: /path/to/video.mp4 or http://example.com/video.mp4

Sande
  • 313
  • 3
  • 7
1

One way to do this would be to to run another application as a child process, and get the resolution from std out. I'm not aware of any pure node.js solution for this.

See child_process.exec https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

and ffprobe How can I get the resolution (width and height) for a video file from a linux command line?

Community
  • 1
  • 1
Hyo Byun
  • 1,196
  • 9
  • 18