Skip to content

Instantly share code, notes, and snippets.

@leikind
Created May 27, 2010
Embed
What would you like to do?
#!/bin/env ruby
# Quick and dirty script to merge N small mp3 files in the current directory into files with duration ~30 minutes
# The power of Unix + the power of Ruby :)
# Question: how to reduce the number of used tools (mplayer, sox, and lame) ?
i = 1
threshold = 60 * 30
merge = lambda{|tuple|
duration, list = tuple
list = list.map{|f| "'#{f}'"}.join(' ')
STDERR.puts "#{list} => out/#{i}.mp3 (#{duration} seconds, #{duration*10/60} minutes)"
`sox #{list} -t wavpcm - | lame --silent - > out/#{i}.mp3`
i += 1
}
merge.call Dir['*.mp3'].map{|fn|
[fn, `mplayer -identify -ao null -vo null -frames 0 #{fn} | grep ^ID_LENGTH= | cut -d = -f 2`.to_f]
}.inject([0, []]){|memo, tuple|
res = [memo[0] + tuple[1], memo[1] + [tuple[0]]]
if res[0] > threshold
merge.call res
[0, []]
else
res
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment