Created
October 10, 2010 11:30
-
-
Save ayosec/619172 to your computer and use it in GitHub Desktop.
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
# Author: Ayose Cazorla | |
# Public Domain | |
# | |
# Normalize a lot of videos using ffmpeg and normalize-audio (both available as Debian packages) | |
# | |
# HRC on tha house... | |
AMPLITUDE="-12dBFS" | |
def say(msg) | |
$stdout.print "\r#{msg}\033[K" | |
$stdout.flush | |
end | |
def run(*args) | |
Process.waitpid(fork do | |
STDIN.reopen("/dev/null", "r") | |
STDERR.reopen("/dev/null", "w") | |
STDOUT.reopen("/dev/null", "w") | |
exec(*args) | |
end) | |
$?.success? | |
end | |
NumProcessors = File.read("/proc/cpuinfo").grep(/^processor\s+:\s+\d+\s*$/).size | |
MainThread = Thread.current | |
StreamFiles = {} | |
VideoFiles = ARGV.grep(/^(?!-)/) | |
Dir.mkdir("streams") unless File.directory?("streams") | |
Dir.mkdir("normalized") unless File.directory?("normalized") | |
def extract_streams(filename) | |
basename = File.basename(filename) | |
dest_audio = "streams/#{basename}.wav" | |
dest_video = "streams/noaudio-#{basename}" | |
if run("ffmpeg", "-y", "-i", filename, "-vn", "-sameq", "-acodec", "pcm_s16le", "-ac", "2", "-ar", "44100", dest_audio) and | |
run("ffmpeg", "-y", "-i", filename, "-an", "-vcodec", "copy", dest_video) | |
StreamFiles[filename] = { :audio => dest_audio, :video => dest_video } | |
end | |
end | |
VideoFiles.each_with_index do |filename, index| | |
say "Extracting #{index + 1}/#{VideoFiles.size}..." | |
Thread.new { extract_streams filename } | |
sleep 0.5 while Thread.list.size > NumProcessors | |
end | |
sleep 0.5 while Thread.list.size > 1 | |
say "Normalizing..." | |
puts | |
system "normalize-audio -a #{AMPLITUDE} streams/*.wav" | |
puts "Creating new videos..." | |
n = 0 | |
StreamFiles.each_pair do |video_name, stream| | |
n += 1 | |
Thread.new do | |
say "[#{n} / #{StreamFiles.size}] Writing normalized" | |
run "ffmpeg", "-y", "-i", stream[:video],"-i", stream[:audio], "-vcodec", "copy", "-acodec", "libmp3lame", "normalized/#{File.basename video_name}" | |
File.unlink stream[:video] | |
File.unlink stream[:audio] | |
end | |
sleep 0.5 while Thread.list.size > NumProcessors | |
end | |
sleep 0.5 while Thread.list.size > 1 | |
say "Done" | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment