class DuoSplitter::Services::Support::SplitTracks

Attributes

commands[R]

Public Class Methods

new(context:) click to toggle source
# File lib/duo_splitter/services/support/split_tracks.rb, line 9
def initialize(context:)
  @context = context

  raise Error, "temp_dir isn't set" unless @context.temp_dir

  @commands = build_commands
end

Public Instance Methods

run() click to toggle source
# File lib/duo_splitter/services/support/split_tracks.rb, line 17
def run
  return if @commands.empty?

  BatchProcessor.new(message: 'splitting...', show_progress: @context.show_progress).run(@commands)
end

Private Instance Methods

build_commands() click to toggle source
# File lib/duo_splitter/services/support/split_tracks.rb, line 25
def build_commands
  sentences = @context.album.sentences(intro: @context.output_intro).select {|sentence| sentence.section.audio_path }

  sentences.map do |sentence|
    output_path = @context.temp_dir.join(sentence.output_basename(prefix_section_number: @context.prefix_section_number, ext: 'wav'))

    sentence.source_audio_path = sentence.audio_path = output_path

    command = [@context.ffmpeg_path, '-y']
    command += ['-i', sentence.section.audio_path.to_s]
    command += ['-ss', sentence.start_pos, '-to', sentence.end_pos]
    command += %w[-codec:a pcm_s16le -ar 44100 -ac 2]
    command += [output_path.to_s]

    command
  end
end