Finding the length/duration of an MP3

5,082 views
Skip to first unread message

Robertjd

unread,
Oct 27, 2011, 2:33:39 PM10/27/11
to nodejs
Hello,

Our node app needs to determine the length, in seconds, of an MP3
file. Has anyone else tried to do this? Digging into the binary
contents of a file is not my forte and would take some time :)

I see that there are some wrappers for FFmpeg, but I'd prefer to not
have to use ffmpeg: https://github.com/tommedema/NodeBasicFFmpeg

I also found this, which I think might have some clues if I need to
write something from scratch: https://github.com/Tim-Smart/node-id3

Any thoughts appreciated.

Thanks,
Robert

Mark Hahn

unread,
Oct 31, 2011, 9:27:48 PM10/31/11
to nod...@googlegroups.com
 I'd prefer to not have to use ffmpeg

Can I ask why?  I'm using it now and it is going smoothly.

Ryan Schmidt

unread,
Oct 31, 2011, 9:48:20 PM10/31/11
to nod...@googlegroups.com

On Oct 31, 2011, at 20:27, Mark Hahn wrote:

>> 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.

Mark Hahn

unread,
Oct 31, 2011, 10:17:17 PM10/31/11
to nod...@googlegroups.com
The module list is here ...


and this looks like something you need ...

https://github.com/leetreveil/node-musicmetadata


Mark Hahn

unread,
Oct 31, 2011, 10:22:19 PM10/31/11
to nod...@googlegroups.com
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'm using ffmpeg for some heavy lifting of video: getting frames as jpeg, transcoding arbitrary user-uploaded video to iPad format etc., and the number of lines is just a handful, but it did take me a day to get all of that working right.

Jorge

unread,
Oct 31, 2011, 11:15:56 PM10/31/11
to nod...@googlegroups.com


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

Ryan Schmidt

unread,
Oct 31, 2011, 11:31:48 PM10/31/11
to nod...@googlegroups.com

On Oct 31, 2011, at 21:22, Mark Hahn wrote:

>> 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.


Nikhil Marathe

unread,
Nov 1, 2011, 12:28:13 AM11/1/11
to nod...@googlegroups.com
On Fri, Oct 28, 2011 at 12:03 AM, Robertjd <rjda...@gmail.com> wrote:
> Hello,
>
> Our node app needs to determine the length, in seconds, of an MP3
> file.  Has anyone else tried to do this?  Digging into the binary
> contents of a file is not my forte and would take some time :)

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

Reply all
Reply to author
Forward
0 new messages