Skip to content

Instantly share code, notes, and snippets.

@don
Created June 8, 2010 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save don/429541 to your computer and use it in GitHub Desktop.
Save don/429541 to your computer and use it in GitHub Desktop.
# parse the duration of a FLV file from the metadata
def duration(file_name)
return -1 unless FileTest.exist?(file_name)
metadata = File.open(file_name, 'rb') do |file|
file.read(400) # bytes
end
offset = metadata =~ /duration/
if offset
start = offset + "duration".size + 1
size = 8 # bytes
buffer = metadata[start...(start + size)]
seconds = buffer.unpack('G').first unless buffer.nil?
"#{sprintf("%02i", seconds / 60)}:#{sprintf("%02i", seconds % 60)}" unless seconds.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment