class Photein::Video

Constants

BITRATE_THRESHOLD
OPTIMIZATION_FORMAT_MAP
SUPPORTED_FORMATS
TARGET_CRF

Public Instance Methods

optimize() click to toggle source
# File lib/photein/video.rb, line 36
def optimize
  return if video.bitrate < BITRATE_THRESHOLD[Photein::Config.optimize_for]

  Photein::Logger.info("transcoding #{tempfile}")
  return if Photein::Config.dry_run

  video.transcode(
    tempfile.to_s,
    [
      '-map_metadata', '0', # https://video.stackexchange.com/a/26076
      '-movflags',     'use_metadata_tags',
      '-c:v',          'libx264',
      '-crf',          TARGET_CRF[Photein::Config.optimize_for],
    ],
    &method(:display_progress_bar)
  )
end

Private Instance Methods

corrupted?() click to toggle source
Calls superclass method Photein::MediaFile#corrupted?
# File lib/photein/video.rb, line 56
def corrupted?
  super(video.bitrate.nil?)
end
display_progress_bar(progress) click to toggle source
# File lib/photein/video.rb, line 94
def display_progress_bar(progress)
  return unless $stdout.tty?

  percentage   = "#{(progress * 100).to_i.to_s}%".rjust(5)
  window_width = Terminal.size[:width]
  bar_len      = window_width - 7
  progress_len = (bar_len * progress).to_i
  bg_len       = bar_len - progress_len
  progress_bar = "[#{'#' * progress_len}#{'-' * bg_len}]#{percentage}"
  print "#{progress_bar}\r"
end
filename_stamp() click to toggle source

NOTE: This may be largely unnecessary: metadata timestamps are generally present in all cases except WhatsApp

Calls superclass method Photein::MediaFile#filename_stamp
# File lib/photein/video.rb, line 77
def filename_stamp
  path.basename(path.extname).to_s.then do |filename|
    case filename
    when /^LINE_MOVIE_\d{13}$/ # LINE: UNIX time in milliseconds (at download)
      Time.strptime(filename[0..-4], 'LINE_MOVIE_%s')
    when /^VID-\d{8}-WA\d{4}$/ # WhatsApp: date + counter (at receipt)
      Time.strptime(filename, 'VID-%Y%m%d-WA%M%S')
    when /^VID_\d{8}_\d{6}_\d{3}$/ # Telegram: datetime in milliseconds (at download)
      Time.strptime(filename, 'VID_%Y%m%d_%H%M%S_%L')
    when /^signal-\d{4}-\d{2}-\d{2}-\d{6}( \(\d+\))?$/ # Signal: datetime + optional counter (at receipt)
      Time.strptime(filename[0, 24], 'signal-%F-%H%M%S')
    else
      super
    end
  end
end
metadata_stamp() click to toggle source
# File lib/photein/video.rb, line 67
def metadata_stamp
  # video timestamps are typically UTC
  MediaInfo.from(path.to_s).general.encoded_date&.getlocal
rescue MediaInfo::EnvironmentError
  Photein::Logger.error('mediainfo is required to read timestamp metadata')
  raise
end
video() click to toggle source
# File lib/photein/video.rb, line 60
def video
  @video ||= FFMPEG::Movie.new(path.to_s)
rescue Errno::ENOENT
  Photein::Logger.error('ffmpeg is required to manipulate video files')
  raise
end