public
Description: Given an Avid ALE file, it will adjust the start, end, and duration times a given amount
Homepage: http://www.bjeanes.com/
Clone URL: git://github.com/bjeanes/ale-adjust.git
ale-adjust / frametime.rb
100644 64 lines (54 sloc) 1.299 kb
1
2
3
4
5
6
7
8
9
10
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'time'
 
class FrameTime
  def initialize(time, frame, frames=25)
    @time = time
    @frame = frame.to_i
    @frames = frames.to_i
  end
  
  def self.parse(str, frames=25)
    if str =~ /(\d\d:\d\d:\d\d):(\d\d)/
      time = Time.parse($1)
      frame = $2
      new(time, frame, frames)
    else
      nil
    end
  end
  
  def +(timecode)
    seconds, frames = to_sec_and_frame(timecode)
    self.add!(seconds, frames)
  end
  
  def -(timecode)
    seconds, frames = to_sec_and_frame(timecode)
    self.add!(-seconds, -frames)
  end
  
  # this should be named nicer
  def add!(s, f=0)
    @frame = @frame + f
    @time += (s + @frame / @frames) #if the frames tick over to a new second, compensate
    @frame %= @frames
self
end
# this should be named nicer
def minus!(s, f=0)
    self.add!(-s, -f)
  end
  
  def to_s; @time.strftime("%H:%M:%S:#{@frame.to_s.rjust(2,'0')}"); end
  def inspect; self.to_s; end
  
  class << self
    private :new
  end
  
  protected
  def to_sec_and_frame(timecode)
    if timecode =~ /(\d\d):(\d\d):(\d\d):(\d\d)/
      hours = $1.to_i
      minutes = $2.to_i
      seconds = $3.to_i
      frames = $4.to_i
      
      seconds = seconds + minutes*60 + hours*3600
      return seconds, frames
    else
      return 0,0
    end
  end
end