Class: CutCut::Media

Inherits:
Base
  • Object
show all
Defined in:
lib/cutcut/media.rb

Overview

Media

Instance Attribute Summary collapse

Attributes inherited from Base

#input, #output

Instance Method Summary collapse

Methods inherited from Base

#original_date_time

Constructor Details

#initialize(options = {}) ⇒ Media



6
7
8
9
10
# File 'lib/cutcut/media.rb', line 6

def initialize(options = {})
  @input = options.delete(:input)
  @output = options.delete(:output)
  @output_path = options.delete(:output_path) || File.dirname(@input)
end

Instance Attribute Details

#output_pathObject (readonly)

Returns the value of attribute output_path.



4
5
6
# File 'lib/cutcut/media.rb', line 4

def output_path
  @output_path
end

Instance Method Details

#convert(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/cutcut/media.rb', line 12

def convert(options = {})
   = options[:copy_metadata] || false
  output = options[:output] || @output || File.join(@output_path, '__' + File.basename(@input))

  execute_ffmpeg_command(input: @input, output: output, raw_options: { output: extract_output_raw_options(options) })

  Helpers.(@input, output) if 
  output
end

#copy_metadata_to_screenshots(basename, fps) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/cutcut/media.rb', line 55

def (basename, fps)
  Dir.glob("#{output_path}/#{basename}*.jpg").sort.each do |file|
    seconds = screenshot_basename_time(basename, file, fps)
    exif = MiniExiftool.new(file)
    exif.create_date = original_date_time + seconds.seconds
    exif.save
  end
end

#cut(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cutcut/media.rb', line 43

def cut(options = {})
  starts_at = options[:start] || '00:00'
  time = options[:time] || 1
  output = options[:output] || "#{File.basename(@input, '.MP4')}_#{starts_at}.mp4"

  execute_ffmpeg_command(
    input: @input,
    output: "#{output_path}/#{output}",
    raw_options: { output: "-ss #{starts_at} -t #{time}" }
  )
end

#extract_output_raw_options(options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/cutcut/media.rb', line 22

def extract_output_raw_options(options = {})
  scale = options[:scale]
  speed = "-filter:v \"setpts=#{options[:speed]}*PTS\"" if options[:speed]
  remove_audio = options[:remove_audio] == true ? '-an' : nil
  "-movflags +faststart -vf scale=#{scale} -c:v libx264 -crf 20 -preset ultrafast  #{speed} #{remove_audio}"
end

#extract_screenshots(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cutcut/media.rb', line 29

def extract_screenshots(options = {})
  fps = options[:fps] || 1
  basename = options[:basename] || File.basename(@input, '.MP4') + '_screenshot'
   = options[:copy_metadata]

  execute_ffmpeg_command(
    input: @input,
    output: "#{output_path}/#{basename}%d.jpg",
    raw_options: { output: "-vf fps=#{fps}" }
  )

  (basename, fps) if 
end

#screenshot_basename_time(basename, file, fps) ⇒ Object



64
65
66
# File 'lib/cutcut/media.rb', line 64

def screenshot_basename_time(basename, file, fps)
  (1.0 / fps.to_f) * File.basename(file, '.jpg').gsub(basename, '').to_i - (1.0 / fps.to_f)
end