Method: Timecode.parse
- Defined in:
- lib/timecode.rb
.parse(spaced_input, with_fps = DEFAULT_FPS) ⇒ Object
Parse timecode entered by the user. Will raise if the string cannot be parsed. The following formats are supported:
-
10h 20m 10s 1f (or any combination thereof) - will be disassembled to hours, frames, seconds and so on automatically
-
123 - will be parsed as 00:00:01:23
-
00:00:00:00 - will be parsed as zero TC
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/timecode.rb', line 158 def parse(spaced_input, with_fps = DEFAULT_FPS) input = spaced_input.strip # 00:00:00;00 if (input =~ DF_TC_RE) atoms_and_fps = input.scan(DF_TC_RE).to_a.flatten.map{|e| e.to_i} + [with_fps, true] return at(*atoms_and_fps) # 00:00:00:00 elsif (input =~ COMPLETE_TC_RE) atoms_and_fps = input.scan(COMPLETE_TC_RE).to_a.flatten.map{|e| e.to_i} + [with_fps] return at(*atoms_and_fps) # 00:00:00+00 elsif (input =~ COMPLETE_TC_RE_24) atoms_and_fps = input.scan(COMPLETE_TC_RE_24).to_a.flatten.map{|e| e.to_i} + [24] return at(*atoms_and_fps) # 00:00:00.0 elsif input =~ FRACTIONAL_TC_RE parse_with_fractional_seconds(input, with_fps) # 00:00:00:000 elsif input =~ TICKS_TC_RE parse_with_ticks(input, with_fps) # 10h 20m 10s 1f 00:00:00:01 - space separated is a sum of parts elsif input =~ /\s/ parts = input.gsub(/\s/, ' ').split.reject{|e| e.strip.empty? } raise CannotParse, "No atoms" if parts.empty? parts.map{|part| parse(part, with_fps) }.inject{|sum, p| sum + p.total } # 10s elsif input =~ /^(\d+)s$/ return new(input.to_i * with_fps, with_fps) # 10h elsif input =~ /^(\d+)h$/i return new(input.to_i * 60 * 60 * with_fps, with_fps) # 20m elsif input =~ /^(\d+)m$/i return new(input.to_i * 60 * with_fps, with_fps) # 60f - 60 frames, or 2 seconds and 10 frames elsif input =~ /^(\d+)f$/i return new(input.to_i, with_fps) # Only a bunch of digits, treat 12345 as 00:01:23:45 elsif (input =~ /^(\d+)$/) atoms_len = 2 * 4 # left-pad input AND truncate if needed padded = input[0..atoms_len].rjust(8, "0") atoms = padded.scan(/(\d{2})/).flatten.map{|e| e.to_i } + [with_fps] return at(*atoms) else raise CannotParse, "Cannot parse #{input} into timecode, unknown format" end end |