# Looking for code to determine length of audio files

**URL:** <https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530>\
**Category:** Ruby\
**Created:** [January 19, 2008, 2:48am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530 "2008-01-19T02:48:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 19, 2008, 2:48am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/1 "2008-01-19T02:48:18Z")

</div>

I’m working on a project which has the requirement to automatically  
determine the runtime of audio files in several formats, right now mp3  
and aac.

I googled around to see if there might be some existing code,  
hopefully ruby code which can determine this, but I’m coming up dry.

Any ideas?

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 19, 2008, 3:07am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/2 "2008-01-19T03:07:34Z")

</div>

On Jan 18, 2008, at 6:47 PM, Rick DeNatale wrote:

> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

cfp:~/src/ruby \> mp3info -p “%S seconds\n” /Users/ahoward/mp3/hot  
chip\ -\ crap\ kraft\ dinner.mp3  
394 seconds

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 19, 2008, 3:11am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/3 "2008-01-19T03:11:07Z")

</div>

I got curious and decided to look at RAA.  
[http://raa.ruby-lang.org/](http://raa.ruby-lang.org/)  
mp3info claims to be able to give mp3 length.  
-Carlos

On Sat, 19 Jan 2008 10:47:47 +0900, “Rick DeNatale”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 19, 2008, 3:41am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/4 "2008-01-19T03:41:33Z")

</div>

Quoth Rick DeNatale:

> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

Take a look at the metadata gem. It harnesses several libraries to  
provide  
meta-information from a wide variety of formats. Not sure if runtime  
length  
is stored in the file’s metadata but it may serve your needs.

HTH,

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 19, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/5 "2008-01-19T17:45:36Z")

</div>

I wrote this to determine how much dictation there is to do in my office  
to see how backed up the typist is. It’s a quick and dirty program but  
it works  
#---------------------------------------

# 

# Compute time of unfinished dictation

# 

#--------------------------------------

# Version 1.1 1/18/07

require ‘find’  
file\_tb = Array.new  
Find.find(".\trdict",".\Dictations") do |f|  
if f =~ /DONE/  
Find.prune  
elsif f =~ /wav/  
fsize = File.stat(f).size  
time = fsize.to\_f \* 0.00157 / 1000.0 #minutes  
file\_tb.push([f,fsize,time])  
elsif f =~ /WMA/  
fsize = File.stat(f).size  
time = f.size.to\_f \* 0.008123 / 1000.0  
file\_tb.push([f,fsize,time])  
end  
end

totSize = 0  
totTime = 0  
#file\_tb.each {|x| p “#{x[0]} #{x[1]} #{x[2]}”}  
file\_tb.each {|x| totSize += x[1]; totTime += x[2]}  
p “# Files=#{file\_tb.size}, File Size=#{totSize/1000000} MB, Total  
Time=#{totTime} Min”  
sleep 10

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 24, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/6 "2008-01-24T17:51:33Z")

</div>

On 1/19/08, Tom R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I wrote this to determine how much dictation there is to do in my office  
> to see how backed up the typist is. It’s a quick and dirty program but  
> it works

Except that it doesn’t handle the file types I need.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 24, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/7 "2008-01-24T17:56:15Z")

</div>

On 1/18/08, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 18, 2008, at 6:47 PM, Rick DeNatale wrote:
> 
> > I’m working on a project which has the requirement to automatically  
> > determine the runtime of audio files in several formats, right now mp3  
> > and aac.

> cfp:~/src/ruby \> mp3info -p “%S seconds\n” /Users/ahoward/mp3/hot  
> chip\ -\ crap\ kraft\ dinner.mp3  
> 394 seconds

Where does this mp3info come from? I installed mp3info from  
[http://ruby-mp3info.rubyforge.org/](http://ruby-mp3info.rubyforge.org/) following Carlo’s lead, but it  
doesn’t seem to have a command line component.

But looking at an example of the podcast I need to handle, in both mp3  
and aac format, neither seems to have any duration related tags.

So I guess I need to figure out if there’s any way to compute the  
duration.

Any ideas anyone?

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 24, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/8 "2008-01-24T20:10:52Z")

</div>

I have just yesterday tried a script using mp3info, which worked a treat  
identifying some music files and giving their length  
even though their was no tag data. Hope this helps.

require ‘mp3info’

Dir.chdir(“C:/Mymusic)  
musicdir=Dir.glob(”\*.mp3")  
musicdir.sort  
out=""  
musicdir.each do |f|  
Mp3Info.open(f) do |mp3|  
l=mp3.length  
mins=l.divmod(60)  
if mins[0]==0  
ln=mins[1].round.to\_s+" secs"  
else  
ln=mins[0].to\_s+ " minutes, “+mins[1].round.to\_s+” seconds"  
end  
fl=mp3.filename

```
out+= fl + " length: "+ln+"\n\n"

end

```

end

f= File.open(“musicinfo.txt”,“w”)  
f.write(out)  
f.close

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 25, 2008, 1:20am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/9 "2008-01-25T01:20:37Z")

</div>

On 1/24/08, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 24, 2008, at 9:55 AM, Rick DeNatale wrote:
> 
> > Where does this mp3info come from? I installed mp3info from  
> > [http://ruby-mp3info.rubyforge.org/](http://ruby-mp3info.rubyforge.org/) following Carlo’s lead, but it  
> > doesn’t seem to have a command line component.
> 
> osx? port install mp3info
> 
> otherwise: [MP3Info](http://www.ibiblio.org/mp3info/)

Ara,

Thanks!

We’d found another program called sox as an alternative, but at least  
on OSX, it’s rather slow, it’s really an audio file transcoder and it  
seems to be doing quite a bit of work to discover the playing time,  
several seconds for a 30 minute MP3.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 24, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/10 "2008-01-24T19:21:58Z")

</div>

On Jan 24, 2008, at 9:55 AM, Rick DeNatale wrote:

> Where does this mp3info come from? I installed mp3info from  
> [http://ruby-mp3info.rubyforge.org/](http://ruby-mp3info.rubyforge.org/) following Carlo’s lead, but it  
> doesn’t seem to have a command line component.

osx? port install mp3info

otherwise: [MP3Info](http://www.ibiblio.org/mp3info/)

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [January 25, 2008, 2:50am UTC](https://www.ruby-forum.com/t/looking-for-code-to-determine-length-of-audio-files/126530/11 "2008-01-25T02:50:44Z")

</div>

Rick DeNatale wrote:

> But looking at an example of the podcast I need to handle, in both mp3  
> and aac format, neither seems to have any duration related tags.
> 
> So I guess I need to figure out if there’s any way to compute the duration.  
> Any ideas anyone?

mp3info tries to estimate the running time unless you run it with the -F  
option,  
which tells it to scan the whole file and count frames etc.

I have a command-line version installed in Debian using apt-get, and the  
same  
version installed in Windows. I haven’t tried the Mac version yet.

Clifford H…
