Class: StreamTrackRipper
- Inherits:
-
Object
- Object
- StreamTrackRipper
- Defined in:
- lib/stream2tracks/stream_track_ripper.rb
Constant Summary collapse
- Version =
['0','0','3']
- TO_BYTES =
{'GB'=>1000000000,'MB'=>1000000,'KB'=>1000}
Instance Attribute Summary collapse
-
#trackfiles ⇒ Object
Returns the value of attribute trackfiles.
-
#tracks ⇒ Object
Returns the value of attribute tracks.
Instance Method Summary collapse
- #convert ⇒ Object
- #get ⇒ Object
-
#initialize(stream, options) ⇒ StreamTrackRipper
constructor
A new instance of StreamTrackRipper.
- #rename ⇒ Object
- #tag ⇒ Object
Constructor Details
#initialize(stream, options) ⇒ StreamTrackRipper
Returns a new instance of StreamTrackRipper.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 13 def initialize stream, @stream=stream @options= @options.env||={} @log=Logger.new @options.log=='-' ? $stderr : @options.log if @options.log @tracks=@stream.parse @trackfiles=[] @log.info 'Processing stream: %s' % @stream.key if @log @log.info 'Found %i tracks' % @tracks.count if @log album=@stream.[:album] @log.info 'Found stream title: %s' % album if @log and album @curdir=Dir.pwd @workdir=File.join(ENV['HOME'],'.cache','stream2tracks',@stream.key) mkdir_p @workdir if block_given? # TODO: support (re)processing selected tracks from any stage get convert tag rename yield end # TODO: report on any failures end |
Instance Attribute Details
#trackfiles ⇒ Object
Returns the value of attribute trackfiles.
11 12 13 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 11 def trackfiles @trackfiles end |
#tracks ⇒ Object
Returns the value of attribute tracks.
11 12 13 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 11 def tracks @tracks end |
Instance Method Details
#convert ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 77 def convert @trackfiles.each do |trackfile| =trackfile. input_filename=trackfile.filename output_filename=File.join @workdir,'%02d.%s' % [[:track],@options.format] cmd=case @options.format when 'ogg' 'ffmpeg -i "%s" -acodec libvorbis "%s"' % [input_filename,output_filename] else # e.g. mp3, flac 'ffmpeg -i "%s" "%s"' % [input_filename,output_filename] end @log.info 'Spawning: %s' % cmd if @log WatchedProcessGroup.new([WatchedProcess.new(cmd,@options.env)]).watch(@options) rm input_filename trackfile.filename=output_filename trackfile.format=@options.format end end |
#get ⇒ Object
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 75 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 39 def get processes=WatchedProcessGroup.new =@options.dup unless .quiet .progress=proc do |buffer| current,total=if buffer.scan(/(\d+\.\d+) (.B) \/ (\d+\.\d+) (.B)/m).last cur,cur_unit,tot,tot_unit=$1,$2,$3,$4 [(cur.to_f*TO_BYTES[cur_unit.upcase]).to_i, (tot.to_f*TO_BYTES[tot_unit.upcase]).to_i] end [current,total] end .validate=proc do |buffer| raise 'libmms connection error' if buffer.include? 'libmms error: libmms connection error' buffer.include? 'Download complete!' end end @tracks.each do |track| =track. # TODO: support formats which may not be possible to determine # from the file extension of the entry in the stream (using # magic number from file itself). format=File.extname(File.basename(track.uri)).sub(/^\./,'') @log.info 'Track tags: %s' % .inspect if @log output_filename=File.join @workdir,'%02d.%s' % [[:track],format] if File.exists? output_filename @log.info 'Discarding old partial output: %s' % output_filename if @options.log rm output_filename end cmd='mimms "%s" "%s"' % [track.uri,output_filename] @log.info 'Spawning: %s' % cmd if @log processes << WatchedProcess.new(cmd,@options.env) @trackfiles << TrackFile.new(,output_filename,format) processes[-1,1].watch unless @options.multi end processes.watch if @options.multi end |
#rename ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 111 def rename @trackfiles.each do |trackfile| =trackfile. input_filename=trackfile.filename output_filename='%02d-%s-%s.%s' % [[:track],[:album] ? [:album] : [:artist],[:title], trackfile.format] # Strip characters that could cause problems for some target filesystems output_filename.gsub!(/[:\/\\]/,' ') # And reduce any blanks to underscores as a shell typing aid output_filename.gsub!(/ +/,'_') @log.info 'Renaming: %s to: %s' % [input_filename,output_filename] if @log mv input_filename,output_filename trackfile.filename=output_filename end end |
#tag ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/stream2tracks/stream_track_ripper.rb', line 98 def tag @trackfiles.each do |trackfile| @log.info 'Tagging: %s' % trackfile.filename if @log =trackfile. file=TagLib::File.new trackfile.filename file.album = [:album] if [:album] file.title = [:title] file.artist = [:artist] file.track = [:track] file.save end end |