Created
December 6, 2011 14:58
-
-
Save meleyal/1438465 to your computer and use it in GitHub Desktop.
Get duration of all MP3s in a directory (recursively)
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
# INSTRUCTIONS | |
# - Puth this file into a directory containing mp3s | |
# - Open in TextMate + run it (cmd+r) | |
# - get_audio_length method via: http://bit.ly/sIKObq | |
dir = File.dirname(__FILE__) | |
mp3s = File.join(dir, "**", "*.mp3") | |
def get_audio_length(filepath) | |
pipe = "ffmpeg -i "+ filepath.to_s+" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//" | |
command = `#{pipe}` | |
if command =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/ | |
#convert the result to only secs | |
duration = ($2.to_i * 60) + $3.to_i | |
end | |
#return and array containing the seconds and the human readable time length, ["6453","03:54"] | |
return "#{duration.to_s},#{$2}:#{$3}".split(",") | |
end | |
Dir.glob(mp3s) do |f| | |
p f | |
p get_audio_length(f) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment