>> I'd prefer to not have to use ffmpeg
>
> Can I ask why? I'm using it now and it is going smoothly.
Maybe because ffmpeg is a huge library with a ton of dependencies, whose API changes all the time, and one would hope the task of finding the length of an MP3 would be a simple operation one could do in a handful of lines of code. That would be my reason anyway.
I'd use ffmpeg to do it, just type:
$ ffmpeg -i /test.m4a
And it gives you all this info:
FFmpeg version SVN-r26402, Copyright (c) 2000-2011 the FFmpeg developers
built on Nov 1 2011 03:35:10 with gcc 4.2.1 (Apple Inc. build 5666) (dot 3)
configuration: --enable-shared --disable-mmx --arch=x86_64
libavutil 50.36. 0 / 50.36. 0
libavcore 0.16. 1 / 0.16. 1
libavcodec 52.108. 0 / 52.108. 0
libavformat 52.93. 0 / 52.93. 0
libavdevice 52. 2. 3 / 52. 2. 3
libavfilter 1.74. 0 / 1.74. 0
libswscale 0.12. 0 / 0.12. 0
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/test.m4a':
Metadata:
major_brand : M4A
minor_version : 0
compatible_brands: M4A mp42isom
creation_time : 2007-05-13 08:42:19
title : The Sky Is Crying
artist : George Thorogood & The Destroyers
album : Anthology
track : 5
date : 2000-08-29T07:00:00Z
copyright : ℗ Compilation (P) 2000 Capitol Records, LLC. All rights reserved. Unauthorized reproduction is a violation of applicable laws. Manufactured by Capitol Catalog, 1750 North Vine Street, Hollywood, CA 90028.
Duration: 00:05:17.25, start: 0.000000, bitrate: 262 kb/s
Stream #0.0(und): Audio: aac, 44100 Hz, stereo, s16, 255 kb/s
Metadata:
creation_time : 2007-05-13 08:42:19
At least one output file must be specified
Simply do an exec to grab it easily:
$ node mp3_duration.js
Duration: 00:05:17.25
require('child_process').exec('ffmpeg -i /test.m4a', cb);
function cb (error, stdout, stderr) {
stdout+= stderr;
stdout= stdout.split('Duration: ')[1].split(', start: ')[0];
console.log('Duration: '+ stdout);
}
https://github.com/xk/nodeSnippets/blob/master/mp3_duration.js
--
Jorge
>> one would hope the task of finding the length of an MP3 would be a simple operation one could do in a handful of lines of code
>
> Well I'm sure the ffmpeg module could do what you want in just a handful of lines of code, but figuring out that those lines should be is the hard part.
I meant that ffmpeg and all of its dependencies probably amount to hundreds of thousands of lines of code, and that determining the length of an mp3 does not require that much code. My thought is that using ffmpeg for this task would be overkill and that a smaller simpler stabler library to do it would exist.
Try node-taglib, https://github.com/nikhilm/node-taglib
It uses TagLib which is a well proven and widely used C++ tagging
library under the hood.
You can use the AudioProperties to extract the length of the song.
Regards,
Nikhil