Class: MusicTrack
- Inherits:
-
Object
- Object
- MusicTrack
- Defined in:
- lib/playlist_transfer.rb
Overview
Class MusicTrack represents one audiofile (trackfile) in some “Music folder” (basedir). trackfile and basedir are Pathnames
Instance Method Summary collapse
-
#encode_flac(outputfile) ⇒ Object
Method encode_flac transcodes FLAC file to MP3 file placed in output (which is passed as parameter).
-
#filetransfer(outfile, justcopy = nil, skoda = nil, relative_path = nil, output = nil) ⇒ Object
This method transfers track file to destination (outfile parameter is Pathname).
-
#get_tags ⇒ Object
Method get_tags returns an array with tags extracted from original FLAC file.
-
#initialize(trackfile, basedir) ⇒ MusicTrack
constructor
Class MusicTrack represents one audiofile (trackfile) in some “Music folder” (basedir).
-
#is_flac? ⇒ Boolean
Returns true if track type is FLAC.
-
#just_copy(output) ⇒ Object
Method to transfer file and it’s structure without removing special characters or transcoding files to MP3 - just alias for transfer with proper parameters.
-
#transfer(output, compatible = nil, justcopy = nil, skoda = nil) ⇒ Object
Method to transfer file and it’s directory structure to destination (output) You can specify you want to create directory srtucture in destination in “compatible” way.
-
#transfer_compatible(output, justcopy = nil) ⇒ Object
Method to transfer file and it’s structure in “compatible” way (file and it’s directory structure with removed special characters) - just alias for transfer with proper parameters.
Constructor Details
#initialize(trackfile, basedir) ⇒ MusicTrack
Class MusicTrack represents one audiofile (trackfile) in some “Music folder” (basedir).
9 10 11 12 13 |
# File 'lib/playlist_transfer.rb', line 9 def initialize(trackfile,basedir) abort "File #{trackfile.} is not located in #{basedir.}" if !trackfile..is_child?(basedir.) @path = trackfile. @basedir = basedir. end |
Instance Method Details
#encode_flac(outputfile) ⇒ Object
Method encode_flac transcodes FLAC file to MP3 file placed in output (which is passed as parameter). Parameter output should be Pathname
39 40 41 42 43 44 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 73 74 |
# File 'lib/playlist_transfer.rb', line 39 def encode_flac(outputfile) wavtemp = Tempfile.new('wavfile') info = self. track = info['tracknumber'].gsub(/\D/,'') system( 'flac', '-s', '-d', '-f', '-o', wavtemp.path, @path.to_s ) system( 'lame', '--silent', '--replaygain-accurate', '--preset', 'cbr', '320', '-m', 's', '-q', '0', '--add-id3v2', '--tt', info['title'], '--ta', info['artist'], '--tl', info['album'], '--ty', info['date'], '--tn', track, '--tg', info['genre'] || 'Rock', wavtemp.path, outputfile.to_s ) # If encoding process is interrupted (by pressinc Ctrl + C), remove probably incomplete file in destination. rescue Interrupt FileUtils.rm(outputfile) if outputfile.file? abort "Interrupted when encoding #{outputfile}. Incomplete file removed from destination." end |
#filetransfer(outfile, justcopy = nil, skoda = nil, relative_path = nil, output = nil) ⇒ Object
This method transfers track file to destination (outfile parameter is Pathname). (When it is FLAC, it converts it to MP3 by default, other files will be just copied). You can specify that you do not want to convert FLAC to MP3 by adding second parameter (justcopy) with value “true”. It also creates directory structure (relatively from basedir to track itself) in destination.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/playlist_transfer.rb', line 88 def filetransfer(outfile, justcopy = nil, skoda = nil, relative_path = nil, output = nil) abort "File #{@path.to_s} does not exist." if !@path.file? outfile = outfile.sub(/\.flac$/i, '.mp3') if !justcopy # If file in destination already exists in destination, skip it, no need to tranfer it. if !outfile.file? puts "Transfering #{outfile.to_s}" outfile.dirname.mkpath if self.is_flac? && !justcopy self.encode_flac(outfile) else FileUtils.cp(@path.realpath,outfile) end # Quick and dirty fix for stuid SKODA audio system, to be able to shuffle all music on storage (Not just in folder like it usualy does) if skoda playlist_line = "/" + relative_path.to_s open("#{output}/ALL_MUSIC.M3U", 'a') { |f| f.puts playlist_line } end else puts "File #{outfile.to_s} already exists. Skipping..." end end |
#get_tags ⇒ Object
Method get_tags returns an array with tags extracted from original FLAC file. Array has fields named as attributes from original file. Each field contains value of that attribute
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/playlist_transfer.rb', line 18 def tmpfile = Tempfile.new('flacdata') system( 'metaflac', "--export-tags-to=#{tmpfile.path}", '--no-utf8-convert', @path.to_s ) data = tmpfile.open = { 'title' => '', 'artist' => '', 'album' => '', 'date' => '', 'tracknumber' => '0' } while line = data.gets m = line.match(/^(\w+)=(.*)$/) if m && m[2] [m[1].downcase] = m[2] end end return end |
#is_flac? ⇒ Boolean
Returns true if track type is FLAC. Identified by file extension.
77 78 79 80 81 82 83 |
# File 'lib/playlist_transfer.rb', line 77 def is_flac? if @path.extname.downcase == ".flac" return true else return false end end |
#just_copy(output) ⇒ Object
Method to transfer file and it’s structure without removing special characters or transcoding files to MP3 - just alias for transfer with proper parameters.
132 133 134 |
# File 'lib/playlist_transfer.rb', line 132 def just_copy(output) self.transfer(output, nil, true) end |
#transfer(output, compatible = nil, justcopy = nil, skoda = nil) ⇒ Object
Method to transfer file and it’s directory structure to destination (output) You can specify you want to create directory srtucture in destination in “compatible” way. This means no spaces and special characters in filenames. If you do not want any conversion from FLAC to MP3, just set justcopy to true.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/playlist_transfer.rb', line 115 def transfer(output, compatible = nil, justcopy = nil, skoda = nil) abort "Can't write to #{output.to_s} or it is not directory." unless output..directory? && output..writable? # Next 3 lines just creates complete destination path for transfered track (combining desired output directory, current track location, and basedir). It also removes special characters if necessary. relative_path = @path.relative_path_from(@basedir) relative_path = relative_path.no_special_chars if compatible output_file = output. + relative_path self.filetransfer(output_file, justcopy, skoda, relative_path, output.) end |
#transfer_compatible(output, justcopy = nil) ⇒ Object
Method to transfer file and it’s structure in “compatible” way (file and it’s directory structure with removed special characters) - just alias for transfer with proper parameters.
127 128 129 |
# File 'lib/playlist_transfer.rb', line 127 def transfer_compatible(output, justcopy = nil) self.transfer(output, true, justcopy) end |