Class: TimestampMaker::Handlers::Ffmpeg

Inherits:
Object
  • Object
show all
Defined in:
lib/timestamp_maker/handlers/ffmpeg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_zone_lookuper:) ⇒ Ffmpeg

Returns a new instance of Ffmpeg.



15
16
17
# File 'lib/timestamp_maker/handlers/ffmpeg.rb', line 15

def initialize(time_zone_lookuper:)
  @time_zone_lookuper = time_zone_lookuper
end

Instance Attribute Details

#time_zone_lookuperObject

Returns the value of attribute time_zone_lookuper.



13
14
15
# File 'lib/timestamp_maker/handlers/ffmpeg.rb', line 13

def time_zone_lookuper
  @time_zone_lookuper
end

Instance Method Details

#accept?(mime_type) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/timestamp_maker/handlers/ffmpeg.rb', line 19

def accept?(mime_type)
  mime_type.start_with?('video/')
end

#add_timestamp(input_path, output_path, time, format:, font_size:, font_family:, font_color:, background_color:, coordinate_origin:, x:, y:, font_padding:) ⇒ Object



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
# File 'lib/timestamp_maker/handlers/ffmpeg.rb', line 23

def add_timestamp(
  input_path,
  output_path,
  time,
  format:,
  font_size:,
  font_family:,
  font_color:,
  background_color:,
  coordinate_origin:,
  x:,
  y:,
  font_padding:
)
  creation_timestamp = time.to_i
  text = "%{pts:localtime:#{creation_timestamp}:#{escape_text_expansion_argument(format)}}"
  drawtext = +"#{coord_map(coordinate_origin, x, y)}:" << %W[
    font=#{escape_filter_description_value(font_family)}
    fontsize=#{font_size}
    fontcolor=#{font_color}
    box=1
    boxcolor=#{background_color}
    boxborderw=#{font_padding}
    text=#{escape_filter_description_value(text)}
  ].join(':')

  command = %W[
    ffmpeg -y
    -v warning
    -i #{input_path}
    -map_metadata 0
    -vf drawtext=#{escape_filter_description(drawtext)}
    #{output_path}
  ]

  tz = tz_env_string(time)
  raise "Command failed with exit #{$CHILD_STATUS.exitstatus}: #{command.first}" unless system({ 'TZ' => tz },
                                                                                               *command)
end

#creation_time(input_path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/timestamp_maker/handlers/ffmpeg.rb', line 63

def creation_time(input_path)
  command = %W[
    ffprobe -v warning -print_format json
    -show_entries format_tags=creation_time,com.apple.quicktime.creationdate,location
    #{input_path}
  ]
  stdout_string, status = Open3.capture2(*command)
  raise unless status.success?

  parsed = JSON.parse(stdout_string)
  iso8601_string = parsed['format']['tags']['com.apple.quicktime.creationdate'] || parsed['format']['tags']['creation_time']
  raise 'Cannot find creation time' if iso8601_string.nil?

  time = Time.iso8601(iso8601_string)

  iso6709_string = parsed['format']['tags']['location']
  if iso6709_string && (time_zone = retrieve_time_zone_from_iso6709(iso6709_string))
    begin
      return TZInfo::Timezone.get(time_zone).to_local(time)
    rescue TZInfo::InvalidTimezoneIdentifier
      warn "Can not find time zone: #{time_zone}"
    end
  end

  time
end