#!/usr/bin/env ruby require 'ftools' require 'progressbar' require 'find' system ('clear') def secs_to_days (secs) days = ((secs / 60) / (60 * 24)).floor hours = (((secs / 60) % (60 *24)) / 60).floor minutes = (secs / 60) % 60 seconds = secs % 60 "Conversion took #{days.to_s} days, #{hours.to_s} hours and #{minutes.to_s} minutes and #{seconds.to_s} seconds." end def elapsed_time start_time = Time.now yield end_time = Time.now elapsed_time = end_time.to_i - start_time.to_i end def user_input responses = Array.new recurse, delete = nil print "What type of file are we going to look for? (eg.. \".avi\" or \".wmv\") " filetype = gets.chomp! responses.push filetype print "What is the full path where we should begin searching? (use ./ for the current directory) " basedir = gets.chomp! responses.push basedir print "Should we search recursively? (y/n) " recurse = gets.chomp! if /y/i =~ recurse recurse = true else recurse = false end responses.push recurse print "Do you want to delete the original files after conversion? (y/n) " delete = gets.chomp! if /y/i =~ delete delete = true else delete = false end responses.push delete return responses end def find_files(criteria) puts "Beginning Filesystem Search for specified files..." puts "We will search for all files with an extension of #{criteria[0]}" puts "Beginning in the directory named #{criteria[1]} and will" if criteria[2] == true puts "Search Recursively" else puts "NOT Search Recursively" end file_stack = Array.new if criteria[2] == true Find.find(criteria[1]) do |filename| if File.extname(filename) == criteria[0] file_stack.push filename else next end end elsif criteria[2] == false puts Dir.glob("*#{criteria[0]}") file_stack = Dir.glob("*#{criteria[0]}") else puts "Oops, I broke!" end return file_stack end def convert_video(filename, criteria) pbar = ProgressBar.new("Conversion",100) pbar_step = 100 / filename.nitems filename.each do |video| myfile_extension = File.extname(video) myfile_basename = File.basename(video,myfile_extension) myfile = File.dirname(video)+"/"+myfile_basename system "ffmpeg -y -i #{video} -map 0.0 -map 0.1 -map 0.2 -map 0.3 -acodec libfaac -ab 64k -ac 2 -vcodec libx264 -r 29.97 -s 640x480 -vpre hq -crf 22 -threads 0 -f mp4 #{myfile}.mp4 -vcodec libx264 -r 29.97 -s 320x240 -vpre hq -crf 22 -newvideo -acodec libfaac -ab 64k -ac 2 -newaudio 2>> ffmpeg.log 1>> ffmpeg.error.log" system "rm -rf #{video}" if criteria[3] == true pbar.inc(pbar_step) end pbar.finish end criteria = user_input() file_stack = find_files(criteria) puts "Looks like we\'ve found #{file_stack.nitems}" if file_stack.nitems > 0 print "Press Enter to begin Transcoding or CTRL-C to cancel." gets convert = elapsed_time { convert_video(file_stack, criteria) } puts secs_to_days(convert) else puts "Sorry, I couldn't find any files to process... Exiting!" end