Tag: flv


FLV Duration in Ruby

There is a lack of decent open source FLV manipulation software out there. There is Yamdi which is a seriously awesome metadata injector, written in C, and then there is FLVTool2, which is a ruby based injector tool. FLVTool2 is really awesome, but it is a serious memory hog and 90% of the files I work with are >1gb size.

I am doing some house cleaning on one project and I have a need to quickly get the duration of an flv video file, but without loading the whole thing into memory, or even reading in the whole file. As an advantage to me, all the files in question have already been injected by yamdi, so the duration is already calculated and inserted into the files. I just need to read it, and be on my merry way.

Here it is:

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


I ripped this out of another one of my classes that is already dealing with the flv file in other ways, so there is more validation in place than you see here. This has only been tested with files injected with yamdi 1.3 and 1.4, I have not tested with anything else, but if anyone does please let me know!

If you are interested in doing this in php there is similar scripts here which helped me greatly.

FLV Files in C++

This took me a bit of stumbling around before I found it, but if anyone is working with raw flv (Flash video) files, here is how you could print a header. I am using this in a cgi program that is designed to stream flv files from any point in the file. If it seeks to a point that is not the beginning, then it has to print this header so that the flash player is not confused about what kind of file it is:

cout << "FLV";
cout << '\x01';
cout << '\x01';
cout << '\x00';
cout << '\x00';
cout << '\x00';
cout << '\x09';
cout << '\x00';
cout << '\x00';
cout << '\x00';
cout << '\x09';

Tag cloud

  1. 1 entries are tagged with 2007
  2. 1 entries are tagged with 2008
  3. 1 entries are tagged with 2009
  4. 2 entries are tagged with 2010
  5. 2 entries are tagged with applevalley
  6. 1 entries are tagged with archlinux
  7. 1 entries are tagged with artichoke
  8. 1 entries are tagged with automation
  9. 1 entries are tagged with batcountry
  10. 1 entries are tagged with beats
  11. 1 entries are tagged with bigsur
  12. 3 entries are tagged with bm2009
  13. 8 entries are tagged with burningman
  14. 1 entries are tagged with christmas
  15. 5 entries are tagged with code
  16. 1 entries are tagged with cplusplus
  17. 1 entries are tagged with desert
  18. 1 entries are tagged with drinks
  19. 1 entries are tagged with dspam
  20. 2 entries are tagged with dunes
  21. 1 entries are tagged with energy
  22. 1 entries are tagged with esplanade
  23. 5 entries are tagged with europe
  24. 1 entries are tagged with evdo
  25. 2 entries are tagged with flv
  26. 1 entries are tagged with gadgets
  27. 1 entries are tagged with government
  28. 1 entries are tagged with haiku
  29. 1 entries are tagged with icecast
  30. 1 entries are tagged with internetradio
  31. 1 entries are tagged with iphone
  32. 2 entries are tagged with losangeles
  33. 1 entries are tagged with math
  34. 3 entries are tagged with mix
  35. 1 entries are tagged with motivation
  36. 1 entries are tagged with moving
  37. 3 entries are tagged with music
  38. 1 entries are tagged with newserver
  39. 1 entries are tagged with obama
  40. 1 entries are tagged with oil
  41. 1 entries are tagged with productivity
  42. 1 entries are tagged with radio
  43. 1 entries are tagged with rails
  44. 3 entries are tagged with recipe
  45. 1 entries are tagged with redmine
  46. 2 entries are tagged with reflections
  47. 1 entries are tagged with rmine
  48. 8 entries are tagged with ruby
  49. 1 entries are tagged with salmon
  50. 1 entries are tagged with salsa
  51. 1 entries are tagged with shoutcast
  52. 3 entries are tagged with site
  53. 2 entries are tagged with snow
  54. 1 entries are tagged with spill
  55. 1 entries are tagged with spotmanradio
  56. 1 entries are tagged with streamtranscoder
  57. 1 entries are tagged with summer
  58. 1 entries are tagged with tecate
  59. 1 entries are tagged with thanksgiving
  60. 6 entries are tagged with travel
  61. 1 entries are tagged with trip
  62. 1 entries are tagged with turkey
  63. 3 entries are tagged with twitter
  64. 1 entries are tagged with website
  65. 3 entries are tagged with work
  66. 1 entries are tagged with x4200
  67. 3 entries are tagged with yz250

Twitter Updates

Links to check out

Subscribe RSS