Class: MusicBox::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/musicbox/exporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(catalog:, dir:, compress: false, force: false, parallel: false) ⇒ Exporter

Returns a new instance of Exporter.



5
6
7
8
9
10
11
# File 'lib/musicbox/exporter.rb', line 5

def initialize(catalog:, dir:, compress: false, force: false, parallel: false)
  @catalog = catalog
  @dir = Path.new(dir).expand_path
  @compress = compress
  @force = force
  @parallel = parallel
end

Instance Method Details

#compress_track(src_file, dst_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/musicbox/exporter.rb', line 45

def compress_track(src_file, dst_file)
  begin
    tags = Catalog::Tags.load(src_file)
    caf_file = dst_file.replace_extension('.caf')
    run_command('afconvert',
      src_file,
      caf_file,
      '--data', 0,
      '--file', 'caff',
      '--soundcheck-generate')
    run_command('afconvert',
      caf_file,
      '--data', 'aac',
      '--file', 'm4af',
      '--soundcheck-read',
      '--bitrate', 256000,
      '--quality', 127,
      '--strategy', 2,
      dst_file)
    tags.save(dst_file, force: true)
    dst_file.utime(src_file.atime, src_file.mtime)
  rescue => e
    dst_file.unlink if dst_file.exist?
    raise e
  ensure
    caf_file.unlink if caf_file.exist?
  end
end

#export_album(album) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/musicbox/exporter.rb', line 13

def export_album(album)
  raise Error, "Must specify destination directory" unless @dir
  name = '%s - %s (%s)' % [album.artist, album.title, album.year]
  export_dir = @dir / name
  export_dir.mkpath unless export_dir.exist?
  threads = []
  album.tracks.each do |track|
    src_file = track.path
    dst_file = export_dir / src_file.basename
    if @force || !dst_file.exist? || dst_file.mtime != src_file.mtime
      if @parallel
        threads << Thread.new do
          export_track(src_file, dst_file)
        end
      else
        export_track(src_file, dst_file)
      end
    end
  end
  threads.map(&:join)
end

#export_track(src_file, dst_file) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/musicbox/exporter.rb', line 35

def export_track(src_file, dst_file)
  if @compress
    warn "compressing #{src_file}"
    compress_track(src_file, dst_file)
  else
    warn "copying #{src_file}"
    src_file.cp(dst_file)
  end
end