require 'mp3info' require 'mp4info' require 'rmagick' class MpegHelper def self.mp3(file, output, bitrate) Mp3Info.open(file) do |mp3| if mp3.bitrate == bitrate.to_i song = {} song['artist'] = mp3.tag.artist song['album'] = mp3.tag.album song['title'] = mp3.tag.title song['track_no'] = mp3.tag.tracknum song['audio_bitrate'] = mp3.bitrate song['audio_length'] = mp3.length song['audio_file_name'] = file song['audio_file_size'] = File.size(file) song['audio_content_type'] = "audio/mpeg" return song else `eyeD3 -i ./ #{file}` Dir.foreach("./") do |cover| if cover =~ /.*\.jpeg$/ @covername = cover elsif cover =~ /.*\.png$/ @covername = cover elsif cover =~ /.*\.gif$/ @covername = cover elsif cover =~ /.*\.jpg$/ @covername = cover end end cover = Magick::ImageList.new(@covername) cover.resize_to_fill!(500, 500) cover.write(@covername) `lame -b #{bitrate} #{file} #{output} --ti #{@covername}` FileUtils.remove_file @covername song = {} song['artist'] = mp3.tag.artist song['album'] = mp3.tag.album song['title'] = mp3.tag.title song['track_no'] = mp3.tag.tracknum song['audio_bitrate'] = bitrate song['audio_length'] = mp3.length song['audio_file_name'] = output song['audio_file_size'] = File.size(output) song['audio_content_type'] = "audio/mpeg" pictranscode = mp3.tag2.PIC FileUtils.remove_file file Mp3Info.open(output) do |mp3| mp3.tag.artist = song['artist'] mp3.tag.album = song['album'] mp3.tag.title = song['title'] mp3.tag.tracknum = song['track_no'] end return song end end end def self.mp4_to_mp3(file, output, bitrate) io = File.new(file) @mp4info = MP4Info.new(io) FileUtils.mv file, "preconverted.m4a" tempfilename = "tempcover#{Time.new.to_i}.jpg" File.open(tempfilename, "a") { |f| f.write(@mp4info.COVR) } cover = Magick::ImageList.new(tempfilename) cover.resize_to_fill!(500, 500) cover.write(tempfilename) `faad -q -o - preconverted.m4a | lame -b #{bitrate} - #{output} --ti #{tempfilename}` FileUtils.remove_file "preconverted.m4a" FileUtils.remove_file tempfilename Mp3Info.open(output) do |mp3| mp3.tag.artist = @mp4info.ART mp3.tag.album = @mp4info.ALB mp3.tag.title = @mp4info.NAM if @mp4info.TRKN[0] != nil mp3.tag.tracknum = 0 else mp3.tag.tracknum = @mp4info.TRKN[0] end mp3.tag2.PIC = @mp4info.COVR @song = {} @song['artist'] = mp3.tag.artist @song['album'] = mp3.tag.album @song['title'] = mp3.tag.title @song['track_no'] = mp3.tag.tracknum @song['audio_bitrate'] = bitrate @song['audio_length'] = mp3.length @song['audio_file_name'] = file @song['audio_file_size'] = File.size(output) @song['audio_content_type'] = "audio/mpeg" end return @song end def self.mp4_to_mp3_artwork_output(file, output, bitrate, covername, sizes) io = File.new(file) @mp4info = MP4Info.new(io) FileUtils.mv file, "preconverted.m4a" File.open(covername, "a") { |f| f.write(@mp4info.COVR) } cover = Magick::ImageList.new(covername) cover.resize_to_fill!(500, 500) cover.write(covername) `faad -q -o - preconverted.m4a | lame -b #{bitrate} - #{output} --ti #{covername}` FileUtils.remove_file "preconverted.m4a" Mp3Info.open(output) do |mp3| mp3.tag.artist = @mp4info.ART mp3.tag.album = @mp4info.ALB mp3.tag.title = @mp4info.NAM if @mp4info.TRKN[0] != nil mp3.tag.tracknum = 0 else mp3.tag.tracknum = @mp4info.TRKN[0] end mp3.tag2.PIC = @mp4info.COVR @song = {} @song['artist'] = mp3.tag.artist @song['album'] = mp3.tag.album @song['title'] = mp3.tag.title @song['track_no'] = mp3.tag.tracknum @song['audio_bitrate'] = bitrate @song['audio_length'] = mp3.length @song['audio_file_name'] = file @song['audio_file_size'] = File.size(output) @song['audio_content_type'] = "audio/mpeg" end count = 0 @song['covers'] = [] sizes.each do |width, height| cover = Magick::ImageList.new(covername) cover.resize_to_fill!(width, height) cover.write("#{width}x#{height}-#{covername}") @song['covers'] << "#{width}x#{height}-#{covername}" end FileUtils.remove_file covername return @song end end