Skip to content

Instantly share code, notes, and snippets.

@nukturnal
Created January 18, 2010 22:54
Show Gist options
  • Save nukturnal/280455 to your computer and use it in GitHub Desktop.
Save nukturnal/280455 to your computer and use it in GitHub Desktop.
Extracting audio length with Rails and ffmpeg
def self.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment