Class: Wavesync::FFMPEG

Inherits:
Object
  • Object
show all
Defined in:
lib/wavesync/ffmpeg.rb,
lib/wavesync/ffmpeg/probe.rb

Defined Under Namespace

Classes: Error, Probe

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFFMPEG

: () -> void



22
23
24
25
26
# File 'lib/wavesync/ffmpeg.rb', line 22

def initialize
  @inputs = [] #: Array[Hash[Symbol, String?]]
  @options = {} #: Hash[Symbol, untyped]
  @metadata_pairs = [] #: Array[[String, String]]
end

Class Method Details

.binaryObject

: () -> String



12
13
14
# File 'lib/wavesync/ffmpeg.rb', line 12

def self.binary
  @binary ||= locate_binary('ffmpeg')
end

.ffplay_binaryObject

: () -> String



17
18
19
# File 'lib/wavesync/ffmpeg.rb', line 17

def self.ffplay_binary
  @ffplay_binary ||= binary.sub('ffmpeg', 'ffplay')
end

Instance Method Details

#audio_bitrate(bitrate) ⇒ Object

: (String bitrate) -> self



47
48
49
50
# File 'lib/wavesync/ffmpeg.rb', line 47

def audio_bitrate(bitrate)
  @options[:audio_bitrate] = bitrate
  self
end

#audio_codec(codec) ⇒ Object

: (String codec) -> self



35
36
37
38
# File 'lib/wavesync/ffmpeg.rb', line 35

def audio_codec(codec)
  @options[:audio_codec] = codec
  self
end

#audio_filter(filter) ⇒ Object

: (String filter) -> self



53
54
55
56
# File 'lib/wavesync/ffmpeg.rb', line 53

def audio_filter(filter)
  @options[:audio_filter] = filter
  self
end

#copy_streamsObject

: () -> self



77
78
79
80
# File 'lib/wavesync/ffmpeg.rb', line 77

def copy_streams
  @options[:copy_streams] = true
  self
end

#duration(seconds) ⇒ Object

: (Numeric seconds) -> self



71
72
73
74
# File 'lib/wavesync/ffmpeg.rb', line 71

def duration(seconds)
  @options[:duration] = seconds
  self
end

#filter_complex(graph) ⇒ Object

: (String graph) -> self



59
60
61
62
# File 'lib/wavesync/ffmpeg.rb', line 59

def filter_complex(graph)
  @options[:filter_complex] = graph
  self
end

#input(source, format: nil) ⇒ Object

: (String source, ?format: String?) -> self



29
30
31
32
# File 'lib/wavesync/ffmpeg.rb', line 29

def input(source, format: nil)
  @inputs << { source: source, format: format }
  self
end

#map_metadata(source_index) ⇒ Object

: (Integer source_index) -> self



83
84
85
86
# File 'lib/wavesync/ffmpeg.rb', line 83

def (source_index)
  @options[:map_metadata] = source_index
  self
end

#metadata(key, value) ⇒ Object

: (String key, String value) -> self



89
90
91
92
# File 'lib/wavesync/ffmpeg.rb', line 89

def (key, value)
  @metadata_pairs << [key, value]
  self
end

#movflags(flags) ⇒ Object

: (String flags) -> self



95
96
97
98
# File 'lib/wavesync/ffmpeg.rb', line 95

def movflags(flags)
  @options[:movflags] = flags
  self
end

#output_format(format) ⇒ Object

: (String format) -> self



65
66
67
68
# File 'lib/wavesync/ffmpeg.rb', line 65

def output_format(format)
  @options[:output_format] = format
  self
end

#run(output_path) ⇒ Object

: (String output_path) -> void

Raises:



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
132
# File 'lib/wavesync/ffmpeg.rb', line 107

def run(output_path)
  args = ['-y']

  @inputs.each do |input|
    args += ['-f', input[:format]] if input[:format]
    args += ['-i', input[:source]]
  end

  args += ['-loglevel', 'warning', '-nostats', '-hide_banner']
  args += ['-c', 'copy'] if @options[:copy_streams]
  args += ['-map_metadata', @options[:map_metadata].to_s] if @options.key?(:map_metadata)
  @metadata_pairs.each { |key, value| args += ['-metadata', "#{key}=#{value}"] }
  args += ['-filter_complex', @options[:filter_complex]] if @options[:filter_complex]
  args += ['-af', @options[:audio_filter]] if @options[:audio_filter]
  args += ['-acodec', @options[:audio_codec]] if @options[:audio_codec]
  args += ['-b:a', @options[:audio_bitrate]] if @options[:audio_bitrate]
  args += ['-ar', @options[:sample_rate].to_s] if @options[:sample_rate]
  args += ['-t', @options[:duration].to_s] if @options[:duration]
  args += ['-movflags', @options[:movflags]] if @options[:movflags]
  args += ['-write_id3v2', @options[:write_id3v2].to_s] if @options[:write_id3v2]
  args += ['-f', @options[:output_format]] if @options[:output_format]
  args << output_path

  _stdout, stderr, status = Open3.capture3(self.class.binary, *args)
  raise Error, "ffmpeg failed: #{stderr}" unless status.success?
end

#sample_rate(rate) ⇒ Object

: (Integer rate) -> self



41
42
43
44
# File 'lib/wavesync/ffmpeg.rb', line 41

def sample_rate(rate)
  @options[:sample_rate] = rate
  self
end

#write_id3v2(version) ⇒ Object

: (Integer version) -> self



101
102
103
104
# File 'lib/wavesync/ffmpeg.rb', line 101

def write_id3v2(version)
  @options[:write_id3v2] = version
  self
end