Created
May 27, 2010 16:02
-
-
Save leikind/415983 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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