Skip to content

Instantly share code, notes, and snippets.

@cowboyrushforth
Created April 29, 2009 02:26
Show Gist options
  • Save cowboyrushforth/103539 to your computer and use it in GitHub Desktop.
Save cowboyrushforth/103539 to your computer and use it in GitHub Desktop.
def duration(filename)
f = File.open(filename, 'r')
raise "Could not open File!" unless f
#check for flv text
unless f.read(3) === 'FLV'
raise "This does not seem to be an FLV file."
end
#check to make sure we are a video
f.seek(3)
is_video = f.read(1).unpack("H*").first.hex
unless is_video == 1
raise "This FLV file does not contain video."
end
#seek to end, get size
f.seek(0, IO::SEEK_END)
length = f.tell
#calculate tag length
f.seek(-4, IO::SEEK_END)
taglen = f.read(4).unpack("H*").first.hex
#finally get duration
f.seek(length - taglen, IO::SEEK_SET)
duration = f.read(3).unpack("H*").first.hex.to_f/1000
f.close()
#duration returns in seconds
duration
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment