简体   繁体   中英

Use fluent-ffmpeg to tell if a file is a video or audio

I am using node-fluent-ffmpeg module in NodeJS. It is very good that fluent-ffmpeg provides functions to get the metadata of a video and audio file.

https://github.com/schaermu/node-fluent-ffmpeg#reading-video-metadata

I have tried on Mac OS to use the "resolution" attribute in the metadata to tell if a file is audio only or video, ie if both resolution.w and resolution.h are 0, then this file is an audio. This work fine on Mac OS. But some strange things happened that this doesn't work on Windows platform (I have tried Windows 7 64bit and Windows 2008) using the latest ffmpeg. Even though I put a .mp3 file through fluent-ffmpeg,the result looks something like this:

video:
{
  container:'mp3',
  ...
  resolution: {w:300,h:300},
  resolutionSquare: {w:300,h:300},
  aspectString: '1:1',
  ...
}
audio:
{
  codec:'mp3',
  bitrate:64,
  sample_rate:44100,
  stream:0,
  channels:1
}

I am not why there is a "resolution" since it is a pure audio file. So is there any solid way to find out if the file is audio only or video from the metadata? Or should I use ffmpeg commandline to find it out?

I would suggest using ffprobe for this, there's a nice module for it called node-ffprobe

Hope that helps!

You can use package 'node-ffprobe' for this.

Sample Code:

var probe = require('node-ffprobe');
var track = '<ENTER FILE PATH>';
probe(track, function(err, probeData) {
 console.log(probeData);
});

Since fluent-ffmpeg 2.0 you don't need another package for this.

The Metadata submodule was replaced by the ffprobe() method.

It is used like this:

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

ffmpeg('/path/to/file.avi').ffprobe(function(err, data) {
  console.dir(data.streams);
  console.dir(data.format);
});

ffmpeg.ffprobe('/path/to/file.avi', function(err, data) {
  console.dir(data.streams);
  console.dir(data.format);
});

This way you can conveniently check resolution/codec or other stream attribute to tell one from another.

Hope this is helpful for anybody using the updated library.

I am not sure what version of fluent-ffmpeg you are using, nor have I tested this on a Windows platform; I just started using fluent-ffmpeg today and the solution I am providing is based on my observations. First we use the code below to access the metadata.

var source = 'vids/source.mp3';

var ffmpeg = require('fluent-ffmpeg');
var Metalib = require('fluent-ffmpeg').Metadata;

var metaObject = new Metalib(source, function(metadata, err) {
  console.log(metadata);
});

Since it displays resolution no matter what, even if it is a pure audio file. From observing two files, an audio file and a video file, here is what I discovered about the audio file [I only pasted the audio output below] .

{ ffmpegversion: '1.2.1',
  title: 'juicy-r [the notorious b.i.g. vs. the xx]',
  artist: 'wait what',
  album: 'the notorious xx',
  track: '2/11',
  date: '2010',
  durationraw: '00:04:07.94',
  durationsec: 247,
  synched: true,
  major_brand: undefined,
  video: 
   { container: 'mp3',
     bitrate: 127,
     codec: 'mjpeg',
     resolution: { w: 1425, h: 1416 },
     resolutionSquare: { w: 1425, h: 1416 },
     rotate: 0,
     fps: 0,
     stream: 0,
     aspectString: '475:472',
     aspect: 1.00635593220339,
     pixelString: '1:1',
     pixel: 1 },
  audio: 
   { codec: 'mp3',
     bitrate: 128,
     sample_rate: 44100,
     stream: 0,
     channels: 2 } }

Basically, assuming if(metadata.video.fps) returns false , then you know it's an audio file. Also the audio file had an additional property 'channels', so you can try checking to see if it exists if(metadata.audio.channels)... and hope this returns true with the that test

If you do find a more clever solution please let me know! :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM