Module: Captions::Util
Constant Summary collapse
- TIMECODE_REGEX =
TC should be HH:MM:SS:FF (frames) or HH:MM:SS.MSC (milliseconds). FF(2 digits) and MSC(3 digits) are optional.
/^-?([01]\d|2[0-3]):[0-5]\d:[0-5]\d(:\d{2}|\.\d{3})?$/
Instance Method Summary collapse
-
#convert_frame_rate(msec, old_fps, new_fps) ⇒ Object
Converts milliseconds calculated in one frame-rate to another frame-rate.
-
#convert_to_msec(tc, ms_per_frame = 40) ⇒ Object
Currently considering frame rate as 25 Converts time-code in HH:MM:SS.MSEC (or) HH:MM:SS:FF (or) MM:SS.MSEC to milliseconds.
-
#msec_to_timecode(milliseconds) ⇒ Object
Converts milliseconds to timecode format Currently returns HH:MM:SS.MSEC Supports upto 60 hours.
-
#sanitize(time, frame_rate) ⇒ Object
Parses time-code and converts it to milliseconds.
Instance Method Details
#convert_frame_rate(msec, old_fps, new_fps) ⇒ Object
Converts milliseconds calculated in one frame-rate to another frame-rate
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/captions/util.rb', line 46 def convert_frame_rate(msec, old_fps, new_fps) old_ms_per_frame = (1000.0 / old_fps) new_ms_per_frame = (1000.0 / new_fps) frames = (msec / old_ms_per_frame).round # Number of frames in old fps sec = frames / old_fps frames = frames % old_fps new_frames = sec * new_fps new_frames += frames # Number of frames in new fps return (new_frames * new_ms_per_frame).round # MSEC in new fps end |
#convert_to_msec(tc, ms_per_frame = 40) ⇒ Object
Currently considering frame rate as 25 Converts time-code in HH:MM:SS.MSEC (or) HH:MM:SS:FF (or) MM:SS.MSEC to milliseconds.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/captions/util.rb', line 11 def convert_to_msec(tc, ms_per_frame=40) msec = 0 negative_multiplier = 1 if tc[0] == '-' tc = tc[1..-1] # remove -ve sign negative_multiplier = -1 end tc_split = tc.split('.') time_split = tc_split[0].split(':') # To handle MM:SS.MSEC format if time_split.length == 2 time_split.unshift('00') end if tc_split[1] # msec component exists msec = tc_split[1].ljust(3, '0').to_i # pad with trailing 0s to make it 3 digit elsif time_split.length == 4 # FF (frame) component exists msec = time_split[-1].to_i * ms_per_frame.to_f time_split.pop # so that below code can work from last index end min = 60 hour = min * 60 # Get HH:MM:SS in seconds sec = time_split[-1].to_i sec += time_split[-2].to_i * min sec += time_split[-3].to_i * hour msec += sec * 1000 return (negative_multiplier * msec.round) # to be consistent with tc_to_frames which also rounds end |
#msec_to_timecode(milliseconds) ⇒ Object
Converts milliseconds to timecode format Currently returns HH:MM:SS.MSEC Supports upto 60 hours
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/captions/util.rb', line 60 def msec_to_timecode(milliseconds) seconds = milliseconds / 1000 msec = milliseconds % 1000 secs = seconds % 60 seconds = seconds / 60 mins = seconds % 60 seconds = seconds / 60 hours = seconds % 60 format("%02d:%02d:%02d.%03d",hours, mins, secs ,msec) end |
#sanitize(time, frame_rate) ⇒ Object
Parses time-code and converts it to milliseconds. If time cannot be converted to milliseconds, it throws InvalidInput Error
77 78 79 80 81 82 83 84 85 |
# File 'lib/captions/util.rb', line 77 def sanitize(time, frame_rate) if time.is_a?(String) if TIMECODE_REGEX.match(time) time = convert_to_msec(time, frame_rate) end end raise InvalidInput, 'Input should be in Milliseconds or Timecode' unless time.is_a? (Fixnum) return time end |