Created
June 8, 2010 03:01
-
-
Save don/429541 to your computer and use it in GitHub Desktop.
This file contains 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
# 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