Class: AudioDicer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/audio_dicer/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/audio_dicer/runner.rb', line 5

def initialize(ctx)
  @ctx = ctx
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



3
4
5
# File 'lib/audio_dicer/runner.rb', line 3

def ctx
  @ctx
end

Class Method Details

.bin_available?(name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
# File 'lib/audio_dicer/runner.rb', line 9

def self.bin_available?(name)
end

Instance Method Details

#expand_relative_file(*args) ⇒ Object

kind of lame but w/e



13
14
15
# File 'lib/audio_dicer/runner.rb', line 13

def expand_relative_file(*args)
  AudioDicer.expand_relative_file(*args)
end

#runObject



17
18
19
20
21
22
23
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/audio_dicer/runner.rb', line 17

def run
  src_opts = ctx.source_options

  # First convert the video to a wav
  puts '[*] Extracting audio'
  system "mplayer -really-quiet -vc null -vo null -nocorrect-pts -ao pcm:waveheader #{expand_relative_file src_opts[:source]}"

  abort '[!] mplayer failed' unless File.exist?(expand_relative_file('audiodump.wav'))

  # Now convert that to one big mp3
  puts '[*] Convert WAV to MP3'
  system "lame -h -b #{src_opts[:bitrate] || 192} audiodump.wav audiodump.mp3"

  abort '[!] lame failed' unless File.exist?(expand_relative_file('audiodump.mp3'))

  # Now split that mp3 using out data
  ctx.albums.each do |album|
    opts = album.options

    puts "[*] Spliting #{album.tracks.size} tracks"

    # TODO allow stop to be optional (even splits)
    # TODO allow last track to use EOF
    album.tracks.each.with_index do |track, index|
      start, stop = track[:time].map { |t| t.sub ':', '.' }
      tags = "@a=#{opts[:artist].inspect},@b=#{opts[:name].inspect},@t=#{track[:title].inspect}"

      system "mp3splt -Qf -g [#{tags}] -o '#{index+1} @a - @t' audiodump.mp3 #{start} #{stop}"
    end
  end

  # Now cleanup
  puts '[*] Complete!'

rescue SystemExit => e
  # Always clean up potental trash
  File.unlink(*%w[wav mp3].map { |ext| "audiodump.#{ext}" }) rescue nil

  raise e # raise to exit
end