Skip to content

Instantly share code, notes, and snippets.

@pedrovanzella
Created June 2, 2010 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrovanzella/423161 to your computer and use it in GitHub Desktop.
Save pedrovanzella/423161 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#This is for SABnzbd+ only
#Let's hope it works and I don't look like a complete moron.
#Uses gem mediainfo from http://github.com/greatseth/mediainfo
#Now using metaflac from the flac package to parse metadata and keep tags.
#Next step: remove mediainfo dependecy.
#Output parsing shamelessly copied from http://kpumuk.info/ruby-on-rails/encoding-media-files-in-ruby-using-ffmpeg-mencoder-with-progress-tracking/
#loops through dir, finds all all FLACs and converts and moves, moves the other audio files, ignores the rest.
#ARGV[0] contains the full path to the downloaded nzb.
ITUNES_FOLDER = "/mnt/Media/iTunes/Automatically Add to iTunes/"
require 'rubygems'
require 'mediainfo'
require 'find' #To list files
class MediaFormatException < StandardError
end
def find_all_flacs(dir)
files = Array.new
Find.find(dir.chomp) do |path|
if FileTest.directory?(path)
if File.basename(path)[0] == ""
Find.prune # Don't look any further into this directory.
else
next
end
else
puts "Found file: " + path
files << path
end
end
files
end
def execute_ffmpeg(command)
progress = nil
IO.popen(command) do |pipe|
pipe.each("r") do |line|
if line =~ /Duration: (d{2}):(d{2}):(d{2}).(d{1})/
duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 10 + $4.to_i
end
if line =~ /time=(d+).(d+)/
if not duration.nil? and duration != 0
p = ($1.to_i * 10 + $2.to_i) * 100 / duration
else
p = 0
end
p = 100 if p > 100
if progress != p
progress = p
print "PROGRESS: #{progress}n"
$defout.flush
end
end
end
end
raise MediaFormatException if $?.exitstatus != 0
end
error = 0
files = find_all_flacs(ARGV[0])
files.each do |f|
info = Mediainfo.new(f)
puts "Scanning: " + info.filename
unless info.audio?
puts "Found audio for: " + info.filename
#Only touch audio files
unless info.format == "FLAC"
puts "FLAC file: " + info.filename
#Do conversion magic
cmd = "ffmpeg -i " + "\"" + f + "\"" + " -acodec alac " + "\"" + ITUNES_FOLDER + info.filename.sub(/(\.flac)$/i, '.m4a') + "\"" + \
" -metadata title=\"$(metaflac --show-tag=TITLE " + "\"" + f + "\"" + " | sed \'s/title=//g\')\"" + \
" -metadata author=\"$(metaflac --show-tag=ARTIST " + "\"" + f + "\"" + " | sed \'s/artist=//g\')\" " + \
" -metadata album=\"$(metaflac --show-tag=ALBUM " + "\"" + f + "\"" + " | sed \'s/album=//g\')\" " + \
" -metadata year=\"$(metaflac --show-tag=DATE " + "\"" + f + "\"" + " | sed \'s/date=//g\')\" " + \
" -metadata track=\"$(metaflac --show-tag=TRACKNUMBER " + "\"" + f + "\"" + " | sed \'s/tracknumber=//g\')\" " + \
" -metadata genre=\"$(metaflac --show-tag=GENRE " + "\"" + f + "\"" + " | sed \'s/genre=//g\')\" "
puts "I will now run [ " + cmd + " ]"
begin
execute_ffmpeg(cmd)
rescue
puts "FAILED: Conversion of " + f
error = 1
end
end
end
end
error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment