Posted By

chrisaiv on 10/29/11


Tagged

ruby smpte


Versions (?)

Ruby: SMPTE Converter


 / Published in: Ruby
 

URL: http://ifhere.org/ffmpeg

If you need to do any sort of screen capturing using FFMPEG, you might need this one day

  1. =begin
  2. http://ifhere.org/ffmpeg
  3.  
  4. Example
  5.  
  6. SMPTECodes.standard= :pal #=> :pal
  7. SMPTECodes.fps #=> 25.0
  8.  
  9. t = "95:33:24.92" #=> "95:33:24.92"
  10. t.to_frame #=> 8256118
  11. t.to_frame.to_smpte #=> "95:33:24.92"
  12. t == t.to_frame.to_smpte #=> true
  13.  
  14. SMPTECodes.standard= :ntsc #=> :ntsc
  15. SMPTECodes.fps #=> 29.97002997003
  16.  
  17. t = "95:33:24.70" #=> "95:33:24.70"
  18. t.to_frame #=> 10309831
  19. t.to_frame.to_smpte #=> "95:33:24.70"
  20. t == t.to_frame.to_smpte #=> true
  21.  
  22. =end
  23.  
  24. class SMPTECodes
  25. #http://en.wikipedia.org/wiki/SMPTE_timecode
  26.  
  27. @@standard = :ntsc
  28.  
  29. def self.standard
  30. @@standard
  31. end
  32.  
  33. # Set the frequency standard, ie :ntsc, :pal, :dv
  34. def self.standard=(code)
  35. @@standard = code
  36. end
  37.  
  38. def self.fps
  39. case self.standard
  40. when :ntsc: 30/1.001
  41. when :pal: 25.0
  42. when :dv: 30.0
  43. end
  44. end
  45.  
  46. module String
  47. # Converts an SMPTE code into a frame number based upon the current FPS
  48. def to_frame
  49. t = self.split(/:|\./).map {|i| i.to_i}
  50. while t.length < 4 do
  51. t.insert 0, 0
  52. end
  53. # hours(?):Miutes:seconds.frames
  54. ((t[0]*(60*60*SMPTECodes.fps)) +
  55. (t[1]*(60*SMPTECodes.fps)) +
  56. (t[2] * SMPTECodes.fps) +
  57. ( (t[3].to_f*(SMPTECodes.fps/100)).round)).round
  58. end
  59. end
  60.  
  61. module Numeric
  62. # Converts an a frame number to an SMPTE string
  63. def to_smpte
  64. h, m = self.divmod(60 * 60* SMPTECodes.fps)
  65. m, s = m.divmod(60* SMPTECodes.fps)
  66. s, f = (s).divmod(SMPTECodes.fps)
  67. f = (f.round*(100.0/SMPTECodes.fps))
  68.  
  69. if f >= 100
  70. s = s+1;
  71. f = 0;
  72. end
  73.  
  74. "#{("%02d" % h.round)}:#{("%02d" % m.round)}:#{("%02d" % s.round)}.#{("%02d" % f.round)}"
  75. end
  76. end
  77. end
  78.  
  79. # Mixin the modules
  80. String.send :include, SMPTECodes::String
  81. Numeric.send :include, SMPTECodes::Numeric

Report this snippet  

You need to login to post a comment.