Class: Ffmprb::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/ffmprb/process.rb,
lib/ffmprb/process/input.rb,
lib/ffmprb/process/output.rb

Defined Under Namespace

Classes: Input, Output

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **opts, &blk) ⇒ Process

Returns a new instance of Process.



62
63
64
65
# File 'lib/ffmprb/process.rb', line 62

def initialize(*args, **opts, &blk)
  @inputs = []
  @timeout = opts[:timeout] || self.class.timeout
end

Class Attribute Details

.duck_audio_silent_minObject

Returns the value of attribute duck_audio_silent_min.



8
9
10
# File 'lib/ffmprb/process.rb', line 8

def duck_audio_silent_min
  @duck_audio_silent_min
end

.duck_audio_transition_lengthObject

Returns the value of attribute duck_audio_transition_length.



8
9
10
# File 'lib/ffmprb/process.rb', line 8

def duck_audio_transition_length
  @duck_audio_transition_length
end

.duck_audio_volume_hiObject

Returns the value of attribute duck_audio_volume_hi.



7
8
9
# File 'lib/ffmprb/process.rb', line 7

def duck_audio_volume_hi
  @duck_audio_volume_hi
end

.duck_audio_volume_loObject

Returns the value of attribute duck_audio_volume_lo.



7
8
9
# File 'lib/ffmprb/process.rb', line 7

def duck_audio_volume_lo
  @duck_audio_volume_lo
end

.timeoutObject

Returns the value of attribute timeout.



10
11
12
# File 'lib/ffmprb/process.rb', line 10

def timeout
  @timeout
end

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



60
61
62
# File 'lib/ffmprb/process.rb', line 60

def timeout
  @timeout
end

Class Method Details

.duck_audio(av_main_i, a_overlay_i, silence, av_main_o, volume_lo: duck_audio_volume_lo, volume_hi: duck_audio_volume_hi, silent_min: duck_audio_silent_min, transition_length: duck_audio_transition_length, video: {resolution: Ffmprb::CGA, fps: 30}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffmprb/process.rb', line 24

def duck_audio(av_main_i, a_overlay_i, silence, av_main_o,
  volume_lo: duck_audio_volume_lo, volume_hi: duck_audio_volume_hi,
  silent_min: duck_audio_silent_min, transition_length: duck_audio_transition_length,
  video: {resolution: Ffmprb::CGA, fps: 30}  # XXX temporary
  )
  Ffmprb.process(av_main_i, a_overlay_i, silence, av_main_o) do |main_input, overlay_input, duck_data, main_output|

    in_main = input(main_input, **(video ? {} : {only: :audio}))
    in_over = input(overlay_input, only: :audio)
    output(main_output, **(video ? {resolution: video[:resolution], fps: video[:fps]} : {})) do
      roll in_main

      ducked_overlay_volume = {0.0 => volume_lo}
      duck_data.each do |silent|
        next  if silent.end_at && silent.start_at && (silent.end_at - silent.start_at) < silent_min

        ducked_overlay_volume.merge!(
          [silent.start_at - transition_length/2, 0.0].max => volume_lo,
          (silent.start_at + transition_length/2) => volume_hi
        )  if silent.start_at

        ducked_overlay_volume.merge!(
          [silent.end_at - transition_length/2, 0.0].max => volume_hi,
          (silent.end_at + transition_length/2) => volume_lo
        )  if silent.end_at
      end
      overlay in_over.volume ducked_overlay_volume

      Ffmprb.logger.debug "Ducking audio with volumes: {#{ducked_overlay_volume.map{|t,v| "#{t}: #{v}"}.join ', '}}"
    end

  end
end

.intermediate_channel_extname(*media) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ffmprb/process.rb', line 12

def intermediate_channel_extname(*media)
  if media == [:video]
    '.y4m'
  elsif media == [:audio]
    '.wav'
  elsif media.sort == [:audio, :video]
    '.flv'
  else
    fail Error, "I don't know how to channel [#{media.join ', '}]"
  end
end

Instance Method Details

#[](obj) ⇒ Object



94
95
96
97
98
99
# File 'lib/ffmprb/process.rb', line 94

def [](obj)
  case obj
  when Input
    @inputs.find_index(obj)
  end
end

#input(io, only: nil) ⇒ Object



67
68
69
70
71
# File 'lib/ffmprb/process.rb', line 67

def input(io, only: nil)
  Input.new(io, only: only).tap do |inp|
    @inputs << inp
  end
end

#output(io, only: nil, resolution: Ffmprb::CGA, fps: 30, &blk) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/ffmprb/process.rb', line 73

def output(io, only: nil, resolution: Ffmprb::CGA, fps: 30, &blk)
  fail Error, "Just one output for now, sorry."  if @output

  @output = Output.new(io, only: only, resolution: resolution, fps: fps).tap do |out|
    out.instance_exec &blk
  end
end

#run(limit: nil) ⇒ Object

NOTE the one and the only entry-point processing function which spawns threads etc



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ffmprb/process.rb', line 82

def run(limit: nil)  # (async: false)
  # NOTE this is both for the future async: option and according to
  # the threading policy (a parent death will be noticed and handled by children)
  thr = Util::Thread.new do
    # NOTE yes, an exception can occur anytime, and we'll just die, it's ok, see above
    Util.ffmpeg(*command, limit: limit, timeout: timeout).tap do |res|  # XXX just to return something
      Util::Thread.join_children! limit, timeout: timeout
    end
  end
  thr.value  if thr.join limit  # NOTE should not block for more than limit
end