Last active
March 24, 2019 19:03
-
-
Save rlipscombe/6f1a376b40bf045befbd2a99ae5a4512 to your computer and use it in GitHub Desktop.
Transcode FLAC to MP3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'find' | |
VERBOSE = false | |
mp3_files = [] | |
Find.find('.') do |f| | |
if File.extname(f) == '.flac' | |
flac = f | |
mp3 = "../Music/#{File.dirname(flac)}/#{File.basename(flac, '.flac')}.mp3" | |
mp3_files.push(mp3) | |
file mp3 => flac do | |
puts "#{mp3}" | |
if VERBOSE | |
loglevel = 'info' | |
else | |
loglevel = 'panic' | |
end | |
FileUtils.mkdir_p("../Music/#{File.dirname(flac)}") | |
# Write to a temporary file | |
suffix = ('a'..'z').to_a.shuffle[0,8].join | |
tmp = "../Music/#{File.dirname(flac)}/.#{File.basename(flac, '.flac')}.mp3.#{suffix}~" | |
sh 'ffmpeg', | |
'-hide_banner', '-loglevel', loglevel, | |
'-y', | |
'-i', flac, | |
'-codec:a', 'libmp3lame', '-qscale:a', '2', | |
'-f', 'mp3', | |
tmp, verbose: VERBOSE | |
# Delete the MP3 file | |
File.delete(mp3) if File.exist?(mp3) | |
# Rename temp to MP3 | |
File.rename(tmp, mp3) | |
end | |
end | |
end | |
# Note: you can clean up the temp files with | |
# find ../Music/ -type f -name '.*~' -delete | |
multitask :transcode => mp3_files | |
art_files = [] | |
Find.find('.') do |f| | |
if File.extname(f) == '.jpg' | |
src = f | |
dst = "../Music/#{File.dirname(src)}/#{File.basename(src)}" | |
art_files.push(dst) | |
file dst => src do | |
FileUtils.copy(src, dst) | |
end | |
end | |
end | |
multitask :artwork => art_files | |
task :default => [:transcode, :artwork] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment