module MovieMasher::Info

get and set meta information about a file

Constants

AT
AUDIO
AUDIO_DURATION
AUDIO_REGEX
DIMENSIONS
DIMENSIONS_REGEX
DOWNLOADED
DURATION
DURATION_REGEX
EXTENSION
FPS
FPS_REGEX
FPS_TB_REGEX
TYPE
VIDEO_DURATION

Public Class Methods

get(path, type) click to toggle source
# File lib/constant/info.rb, line 23
def get(path, type)
  result = nil
  __raise_if_empty(path, type)
  if File.size?(path)
    meta = meta_path(type, path)
    result = (File.exist?(meta) ? File.read(meta) : __get(path, type))
  end
  result
end
meta_path(type, path) click to toggle source
# File lib/constant/info.rb, line 33
def meta_path(type, path)
  file_name = "#{File.basename(path, '.*')}.#{type}.#{EXTENSION}"
  Path.concat(File.dirname(path), file_name)
end
parse(type, ffmpeg_output) click to toggle source
# File lib/constant/info.rb, line 38
def parse(type, ffmpeg_output)
  result = nil
  unless ffmpeg_output.to_s.empty?
    case type
    when AUDIO
      AUDIO_REGEX.match(ffmpeg_output) do |match|
        result = 1 if match[1] != 'none'
      end
    when DIMENSIONS
      DIMENSIONS_REGEX.match(ffmpeg_output) do |match|
        result = "#{match[1]}x#{match[2]}"
      end
    when DURATION
      DURATION_REGEX.match(ffmpeg_output) do |match|
        result = 60 * 60 * match[1].to_i
        result += 60 * match[2].to_i
        result = result.to_f + match[3].to_f
      end
    when FPS
      match = FPS_REGEX.match(ffmpeg_output)
      match ||= FPS_TB_REGEX.match(ffmpeg_output)
      result = match[1].to_f.round
    end
  end
  result
end
set(path, type, data) click to toggle source
# File lib/constant/info.rb, line 65
def set(path, type, data)
  if type && path
    File.open(meta_path(type, path), 'w') { |f| f.write(data) }
  end
  data
end
set_if(path, type, result) click to toggle source
# File lib/constant/info.rb, line 72
def set_if(path, type, result)
  set(path, type, result) if result
end
type(path) click to toggle source
# File lib/constant/info.rb, line 76
def type(path)
  result = nil
  if path
    result = get(path, 'type')
    unless result
      mime = get(path, 'Content-Type')
      mime ||= __mime(path)
      result = mime.split('/').shift if mime
      set(path, 'type', result) if result
    end
  end
  result
end