#!/usr/bin/env ruby # coding: utf-8 # Hyper Morning Server Project class File def self.duration(file) duration = nil if(file =~ /\.flac/) IO.popen("metaflac --show-channel --show-bps --show-sample-rate --show-total-samples #{file} 2>&1") {|io| infos = io.read.split(/\s+/) infos[0].to_i == 2 and \ infos[1].to_i == 16 and \ infos[2].to_i == 44100 and \ duration = infos[3].to_i / infos[2].to_f } elsif(file =~ /\.mp3/) IO.popen("ffprobe #{file} 2>&1") {|io| io.each {|info| if(info =~ /^\s*Duration: (\d{2}):(\d{2}):(\d{2})\.(\d{2})/) duration = $1.to_i * 3600 + $2.to_i * 60 + $3.to_i + $4.to_i / 100.0 break end } } end duration end end #--------------------------------------------------------------- # # class Playlist # class Playlist < Array def initialize @total_duration = 0 end def <<(file) if(d = File.duration(file)) super({ :FILE => file, :DURATION => d, }) @total_duration += d end end def unshift(file) if(d = File.duration(file)) super({ :FILE => file, :DURATION => d }) @total_duration += d end end def reduce(max_duration) max_try = 20; while(@total_duration > max_duration and (max_try -= 1) > -1) @total_duration -= self.delete_at(rand(self.size))[:DURATION] end end def padding(max_duration, file) max_try = 20; while(@total_duration < max_duration and (max_try -= 1) > -1) self.unshift(file) end end def out self.each {|e| puts(e[:FILE]) } end def check self.each {|e| p e } p @total_duration end end #=============================================================== # # main # ARGV.size == 0 and abort(< b[:FILE] } #playlist.shuffle! #--------------------------------------------------------------- # # reduce total duration # playlist.reduce(40 * 60) #--------------------------------------------------------------- # # add last number # playlist << [ '/mnt/mediatomb/NINTENDO_52_SPLATUNE_DISC2/track04.cdda.flac', # 火、木 '/mnt/mediatomb/NINTENDO_51_SPLATUNE_DISC1/track08.cdda.flac', # 月、水、金 ][Time.now.wday % 2] #--------------------------------------------------------------- # # padding playlist # playlist.padding(50 * 60, '/mnt/mediatomb/silence1min.flac') playlist << '/mnt/mediatomb/silence1min.flac' #--------------------------------------------------------------- # # check playlist # #playlist.check #--------------------------------------------------------------- # # output playlist # playlist.out __END__