Class: CueSnap::Splitter

Inherits:
Object
  • Object
show all
Defined in:
lib/cuesnap/splitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mp3_file, cue_file = nil, options = {}) ⇒ Splitter

Public: Loads an mp3 and a RubyCue cuesheet.

mp3_file - String file path to an mp3 file. cue_file - The path to a cuesheet for the target cue file (default: the

name of the mp3, with .cue added).

options - Hash of options.

no_numbers - No number prefix for tracks.
output_folder - The output folder to use (default: the name
  of the mp3).

Returns the initalized object.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cuesnap/splitter.rb', line 29

def initialize(mp3_file, cue_file = nil, options = {})
  raise CueSnap::MP3FileNotFound, mp3_file unless File.exists? mp3_file

  @mp3_file = mp3_file
  if cue_file and cue_file.strip != ''
    @cue_file = cue_file
  else
    @cue_file = File.expand_path("#{mp3_filename}.cue", File.dirname(@mp3_file))
  end

  raise CueSnap::CueFileNotFound, @cue_file unless File.exists? @cue_file

  # If cue file is larger than 1MB, raise an error.
  raise CueSnap::CueFileTooLarge if (File.size(@cue_file).to_f / 2**20) > 1

  @options = Hashie::Mash.new options
  @output_folder = @options.output_folder
  @output_folder ||= mp3_filename
end

Instance Attribute Details

#cue_fileObject (readonly)

Returns the value of attribute cue_file.



16
17
18
# File 'lib/cuesnap/splitter.rb', line 16

def cue_file
  @cue_file
end

#mp3_fileObject (readonly)

Returns the value of attribute mp3_file.



16
17
18
# File 'lib/cuesnap/splitter.rb', line 16

def mp3_file
  @mp3_file
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/cuesnap/splitter.rb', line 16

def options
  @options
end

#output_folderObject (readonly)

Returns the value of attribute output_folder.



16
17
18
# File 'lib/cuesnap/splitter.rb', line 16

def output_folder
  @output_folder
end

Instance Method Details

#escaped_cue_fileObject

Public: The space-escaped cue file path.



116
117
118
# File 'lib/cuesnap/splitter.rb', line 116

def escaped_cue_file
  escape_path @cue_file
end

#escaped_mp3_fileObject

Public: The space-escaped mp3 file path.



106
107
108
# File 'lib/cuesnap/splitter.rb', line 106

def escaped_mp3_file
  escape_path @mp3_file
end

#escaped_output_folderObject

Public: The space-escaped output folder path.



111
112
113
# File 'lib/cuesnap/splitter.rb', line 111

def escaped_output_folder
  escape_path @output_folder
end

#mp3_filenameObject

Public: The filename for the mp3 file with the .mp3 extension removed.



101
102
103
# File 'lib/cuesnap/splitter.rb', line 101

def mp3_filename
  File.basename(@mp3_file, '.mp3')
end

#parse_cue_fileObject

Internal: Parses the cue file using RubyCue and sets the @cuesheet variable.

Returns nothing.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cuesnap/splitter.rb', line 53

def parse_cue_file
  file_contents = File.read @cue_file

  # Try to fix unicode problems
  # use iconv if on Ruby 1.8
  # From: bit.ly/bGmrCnCOPY
  require 'iconv' unless String.method_defined?(:encode)
  if String.method_defined?(:encode)
    file_contents.encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '')
    file_contents.encode!('UTF-8', 'UTF-16')
  else
    ic = Iconv.new('UTF-8', 'UTF-8//IGNORE')
    file_contents = ic.iconv(file_contents)
  end

  @cuesheet = RubyCue::Cuesheet.new file_contents
  @cuesheet.parse!
end

#split!Object

Public: Splits the mp3 into files based on track_names and saves them to the output folder.

Returns nothing.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cuesnap/splitter.rb', line 76

def split!
  # Wait until the last second to parse the cue file, in case the user
  # changes it before we split.
  parse_cue_file

  format = "@p - @t"

  song_count_length = (@cuesheet.songs.length + 1).to_s.length
  number_format = "@N#{song_count_length > 1 ? song_count_length : ''}"
  format = "#{number_format} #{format}" unless @options.no_numbers

  # Got to esape the spaces for the shell
  format = Shellwords.escape format

  command = ['mp3splt',
             "-d #{escaped_output_folder}",
             "-o #{format}",
             "-c #{escaped_cue_file}"]
  command.push '-Q' if @options.quiet
  command.push escaped_mp3_file

  system command.join(' ')
end