Class: Av::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/av/commands/base.rb

Overview

Common features across commands

Direct Known Subclasses

Avconv, Ffmpeg

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
# File 'lib/av/commands/base.rb', line 19

def initialize(options = {})
  reset_input_filters
  reset_output_filters
  reset_default_filters
  @options = options
end

Instance Attribute Details

#audio_filtersObject

Returns the value of attribute audio_filters.



12
13
14
# File 'lib/av/commands/base.rb', line 12

def audio_filters
  @audio_filters
end

#command_nameObject

Returns the value of attribute command_name.



8
9
10
# File 'lib/av/commands/base.rb', line 8

def command_name
  @command_name
end

#default_paramsObject

Returns the value of attribute default_params.



14
15
16
# File 'lib/av/commands/base.rb', line 14

def default_params
  @default_params
end

#destinationObject

Returns the value of attribute destination.



17
18
19
# File 'lib/av/commands/base.rb', line 17

def destination
  @destination
end

#input_paramsObject

Returns the value of attribute input_params.



9
10
11
# File 'lib/av/commands/base.rb', line 9

def input_params
  @input_params
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/av/commands/base.rb', line 7

def options
  @options
end

#output_format(format) ⇒ Object

Returns the value of attribute output_format.



11
12
13
# File 'lib/av/commands/base.rb', line 11

def output_format
  @output_format
end

#output_paramsObject

Returns the value of attribute output_params.



10
11
12
# File 'lib/av/commands/base.rb', line 10

def output_params
  @output_params
end

#sourceObject

Returns the value of attribute source.



16
17
18
# File 'lib/av/commands/base.rb', line 16

def source
  @source
end

#video_filtersObject

Returns the value of attribute video_filters.



13
14
15
# File 'lib/av/commands/base.rb', line 13

def video_filters
  @video_filters
end

Instance Method Details

#add_destination(dest) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/av/commands/base.rb', line 30

def add_destination dest
  # infer format from extension unless format has already been set
  if @output_format.nil?
    output_format File.extname(dest)
  end
  @destination = dest
end

#add_input_param(*param) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/av/commands/base.rb', line 52

def add_input_param *param
  p = parse_param(param)
  ::Av.log "Adding input parameter #{p}"
  @input_params[p[0]] = [] unless @input_params.has_key?(p[0])
  @input_params[p[0]] << p[1]
  self
end

#add_output_param(*param) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/av/commands/base.rb', line 64

def add_output_param *param
  p = parse_param(param)
  ::Av.log "Adding output parameter #{p}"
  @output_params[p[0]] = [] unless @output_params.has_key?(p[0])
  @output_params[p[0]] << p[1]
  self
end

#add_source(src) ⇒ Object



26
27
28
# File 'lib/av/commands/base.rb', line 26

def add_source src
  @source = src
end

#filter_rotate(degrees) ⇒ Object

Children should override the following methods



154
155
156
# File 'lib/av/commands/base.rb', line 154

def filter_rotate degrees
  raise ::Av::FilterNotImplemented, 'rotate'
end

#filter_seek(seek) ⇒ Object

ffmpeg and avconf both have the same seeking params



164
165
166
167
# File 'lib/av/commands/base.rb', line 164

def filter_seek seek
  add_input_param ss: seek
  self
end

#filter_volume(vol) ⇒ Object

Children should override the following methods



159
160
161
# File 'lib/av/commands/base.rb', line 159

def filter_volume vol
  raise ::Av::FilterNotImplemented, 'volume'
end

#identify(path) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/av/commands/base.rb', line 94

def identify path
  meta = {}
  command = %Q(#{@command_name} -i "#{File.expand_path(path)}" 2>&1)
  out = ::Av.run(command, [0,1])
  out.split("\n").each do |line|
    if line =~ /(([\d\.]*)\s.?)fps,/
      meta[:fps] = $1.to_i
    end
    # Matching lines like:
    # Video: h264, yuvj420p, 640x480 [PAR 72:72 DAR 4:3], 10301 kb/s, 30 fps, 30 tbr, 600 tbn, 600 tbc
    if line =~ /Video:(.*)/
      size = $1.to_s.match(/\d{3,5}x\d{3,5}/).to_s
      meta[:size] = size unless size.empty?
      if meta[:size]
        meta[:width], meta[:height] = meta[:size].split('x').map(&:to_i)
        meta[:aspect] = meta[:width].to_f / meta[:height].to_f
      end
    end
    # Matching Stream #0.0: Audio: libspeex, 8000 Hz, mono, s16
    if line =~ /Audio:(.*)/
      meta[:audio_encode], meta[:audio_bitrate], meta[:audio_channels] = $1.to_s.split(',').map(&:strip)
    end
    # Matching Duration: 00:01:31.66, start: 0.000000, bitrate: 10404 kb/s
    if line =~ /Duration:(\s.?(\d*):(\d*):(\d*\.\d*))/
      meta[:length] = $2.to_s + ":" + $3.to_s + ":" + $4.to_s
      meta[:duration] = $2.to_i * 3600 + $3.to_i * 60 + $4.to_f
    end
    if line =~ /rotate\s*:\s(\d*)/
      meta[:rotate] = $1.to_i
    end
  end
  if meta.empty?
    ::Av.log "Empty metadata from #{path}. Got the following output: #{out}"
  else
    return meta
  end
  nil
end

#parse_param(param) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/av/commands/base.rb', line 169

def parse_param param
  list = []
  if param.count == 2
    list = param
  elsif param.count == 1
    case param[0].class.to_s
    when 'Hash'
      list[0], list[1] = param[0].to_a.flatten!
    when 'Array'
      list = param[0]
    end
  end
  list
end

#reset_default_filtersObject



48
49
50
# File 'lib/av/commands/base.rb', line 48

def reset_default_filters
  @default_params = ParamHash.new
end

#reset_input_filtersObject



38
39
40
41
42
# File 'lib/av/commands/base.rb', line 38

def reset_input_filters
  @input_params = ParamHash.new
  @audio_filters = ParamHash.new
  @video_filters = ParamHash.new
end

#reset_output_filtersObject



44
45
46
# File 'lib/av/commands/base.rb', line 44

def reset_output_filters
  @output_params = ParamHash.new
end

#runObject

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/av/commands/base.rb', line 76

def run
  raise Av::CommandError if (@source.nil? && @destination.nil?) || @command_name.nil?

  parameters = []
  parameters << @command_name
  parameters << @default_params if @default_params
  if @input_params
    parameters << @input_params.to_s
  end
  parameters << %Q(-i "#{@source}") if @source
  if @output_params
    parameters << @output_params.to_s
  end
  parameters << %Q(-y "#{@destination}") if @destination
  command_line = parameters.flatten.compact.join(" ").strip.squeeze(" ")
  ::Av.run(command_line)
end

#set_input_params(hash) ⇒ Object



60
61
62
# File 'lib/av/commands/base.rb', line 60

def set_input_params hash
  @input_params = hash
end

#set_output_params(hash) ⇒ Object



72
73
74
# File 'lib/av/commands/base.rb', line 72

def set_output_params hash
  @output_params = hash
end