Created
February 3, 2015 08:15
-
-
Save palkan/31a70fd45b2b43024370 to your computer and use it in GitHub Desktop.
Convert all audio files within directory to MP3 (320) with FFMPEG
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
#!/usr/bin/env ruby | |
CONVERT_COMMAND = "ffmpeg -i ':input' -vn -ar 44100 -ac 2 -ab 320 -f mp3 ':output'" | |
def is_audio?(filename) | |
filename =~ /\.(wav|flac|aac)$/ | |
end | |
def extract_name(filename) | |
File.basename(filename, File.extname(filename))+".mp3" | |
end | |
def convert(filename, dir) | |
p "Conveting #{File.basename(filename)}" | |
inpath = File.expand_path(filename) | |
outpath = File.join(dir, extract_name(filename)) | |
command = CONVERT_COMMAND.gsub(/(:\w+)/, {":input" => inpath, ":output" => outpath}) | |
if system(command) | |
p "Success" | |
else | |
p "Failed(" | |
end | |
end | |
dir = ARGV[0] | |
raise "Dir not found!" unless File.directory?(dir) | |
Dir.chdir(dir) | |
target_dir = ARGV[1] || dir | |
if target_dir != dir | |
require 'fileutils' | |
FileUtils.mkdir_p(target_dir) | |
if target_dir !~ /^\// | |
target_dir = File.join(File.expand_path(dir), target_dir) | |
end | |
end | |
Dir["*"].each do |file| | |
convert(file, target_dir) if is_audio?(file) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment