Module: Tlapse::Capture

Defined in:
lib/tlapse/capture.rb

Constant Summary collapse

CAPTURE_FILENAME =
"%Y-%m-%d_%H-%M-%S.jpg"
CAPTURE_DIRNAME =
"%Y-%m-%d"

Class Method Summary collapse

Class Method Details

.capture_dirnameObject



13
14
15
# File 'lib/tlapse/capture.rb', line 13

def self.capture_dirname
  CAPTURE_DIRNAME
end

.capture_filenameObject



9
10
11
# File 'lib/tlapse/capture.rb', line 9

def self.capture_filename
  CAPTURE_FILENAME
end

.capture_singleObject



17
18
19
# File 'lib/tlapse/capture.rb', line 17

def self.capture_single
  `gphoto2 --capture-image-and-download --filename '#{capture_filename}'`
end

.captures_needed(start_time: Time.now, end_time: Time.now, duration: nil, interval:) ⇒ Object

Calculate how many captures are needed given a duration of capture time and a desired interval between captures.

Specify either ‘start_time` and `end_time` or `duration`. If both are given, `duration` is preferred.

Examples:

captures_needed duration: 5.minutes, interval: 30.seconds # => 10

Parameters:

  • start_time: (Time) (defaults to: Time.now)

    when to start capturing

  • end_time: (Time) (defaults to: Time.now)

    when to end capturing

  • duration: (Duration) (defaults to: nil)

    duration over which images are captured. If specifying, do not also specify ‘end_time

  • interval: (Duration)

    time between each capture

Returns:

  • how many captures will be taken over the duration and interval



37
38
39
40
41
42
43
44
45
46
# File 'lib/tlapse/capture.rb', line 37

def self.captures_needed(start_time: Time.now, end_time: Time.now,
                         duration: nil, interval:)
  unless duration
    start_time = start_time.to_i
    end_time   = end_time.to_i
    duration   = end_time - start_time
  end

  duration / interval
end

.timelapse_command(from: Time.now, to:, interval:) ⇒ String

Capture a series of timelapse images over the given duration and at the given interval.

Returns:

  • (String)

    a gphoto2 command with the desired arguments



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tlapse/capture.rb', line 53

def self.timelapse_command(from: Time.now, to:, interval:)
  captures = self.captures_needed(
    start_time: from,
    end_time:   to,
    interval:   interval
  )

  command = "gphoto2 --capture-image-and-download"
  command += " -I #{interval}"
  command += " -F #{captures}"
  command += " --filename '#{capture_filename}'"
end

.timelapse_command_while_sun_is_up(interval: 5.minutes) ⇒ String

Capture a timelapse image once per interval while the sun is up.

Parameters:

  • interval (Duration) (defaults to: 5.minutes)

    how frequently to capture images

Returns:

  • (String)

    a gphoto2 command with the desired arguments



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tlapse/capture.rb', line 71

def self.timelapse_command_while_sun_is_up(interval: 5.minutes)
  sunrise = SolarEvent.sunrise
  sunset  = SolarEvent.sunset
  now     = Time.now

  # Use now as starting time if sunrise has passed
  sunrise = now > sunrise ? now : sunrise

  captures = self.captures_needed(
    start_time: sunrise,
    end_time:   sunset,
    interval:   interval
  )
  "gphoto2 --capture-image-and-download -I #{interval} -F #{captures} --filename '#{capture_filename}'"
end