rendered paste body#! /usr/bin/ruby# manincure.rb version 0.5$presetMasterList = []$hashMasterList = []class PresetClass @@columnWidth=40 def initialize rawPresets = readPresetPlist presetStew = [] presetStew = rawPresets.split("\n") presetStew = cleanStew(presetStew) presetBreaks = findPresetBreaks(presetStew) i = 0 while i <= presetBreaks.size if i == 0 #first preset $presetMasterList[i] = presetStew.slice(0..presetBreaks[i].to_i) elsif i < presetBreaks.size #middle presets $presetMasterList[i] = presetStew.slice(presetBreaks[i-1].to_i..presetBreaks[i].to_i) else #final preset $presetMasterList[i] = presetStew.slice(presetBreaks[i-1].to_i..presetStew.length) end i += 1 end buildPresetHash displayCommandStrings end def readPresetPlist # Grab the .plist and store it in presets homeLocation = `echo $HOME`.chomp inputFile = homeLocation+'/Library/Application\ Support/HandBrake/UserPresets.plist' parseCommand = 'cat '+inputFile+' | sed -e \'s/<[a-z]*>//\' -e \'s/<\/[a-z]*>//\' -e \'/<[?!]/d\' ' puts "\n\n" rawPresets = `#{parseCommand}` end def cleanStew(presetStew) #remove tabbed white space presetStew.each do |oneline| oneline.strip! end end def findPresetBreaks(presetStew) #figure out where each preset starts and ends i = 0 j = 0 presetBreaks =[] presetStew.each do |presetLine| if presetLine =~ /AudioBitRate/ presetBreaks[j] = i-1 j += 1 end i += 1 end return presetBreaks end def buildPresetHash #fill up $hashMasterList with hashes of all key/value pairs j = 0 $presetMasterList.each do |singleServing| #iterate presets tempHash = Hash.new i = 1 while i < singleServing.length tempHash[singleServing[i]] = singleServing[i+1] i += 2 end $hashMasterList[j]=tempHash j += 1 end end def displayCommandStrings $hashMasterList.each do |hash| if hash.key?("PresetName") displayHeader(hash) generateCLIString(hash) generateAPIcalls(hash) generateCLIPresetList(hash) end end end def displayHeader(hash) puts "*" * @@columnWidth puts '* '+hash["PresetName"].to_s.center(@@columnWidth-4)+' *' puts '~' * @@columnWidth puts hash["PresetDescription"].to_s.center(@@columnWidth).gsub(/\n/," ").scan(/\S.{0,#{@@columnWidth-2}}\S(?=\s|$)|\S+/) puts '~' * @@columnWidth puts "#{hash["FileCodecs"]}".center(@@columnWidth) if hash["Type"].to_i == 1 puts "Custom Preset".center(@@columnWidth) end if hash["Default"].to_i == 1 puts "This is your default preset.".center(@@columnWidth) end puts "~" * @@columnWidth # puts "\n" end def generateCLIString(hash) commandString = "" commandString << './HandBrakeCLI -i DVD -o ~/Movies/movie.' #Filename suffix case hash["FileFormat"] when /MP4/ commandString << "mp4 " when /AVI/ commandString << "avi " when /OGM/ commandString << "ogm " when /MKV/ commandString << "mkv " end #Video encoder if hash["VideoEncoder"] != "FFmpeg" commandString << " -e " if hash["VideoEncoder"] == "x264 (h.264 Main)" commandString << "x264" elsif hash["VideoEncoder"] == "x264 (h.264 iPod)" commandString << "x264b30" else commandString << hash["VideoEncoder"].to_s.downcase end end #VideoRateControl case hash["VideoQualityType"].to_i when 0 commandString << " -S " << hash["VideoTargetSize"] when 1 commandString << " -b " << hash["VideoAvgBitrate"] when 2 commandString << " -q " << hash["VideoQualitySlider"] end #FPS if hash["VideoFramerate"] != "Same as source" if hash["VideoFramerate"] == "23.976 (NTSC Film)" commandString << " -r " << "23.976" elsif hash["VideoFramerate"] == "29.97 (NTSC Video)" commandString << " -r " << "29.97" else commandString << " -r " << hash["VideoFramerate"] end end #Audio bitrate commandString << " -B " << hash["AudioBitRate"] #Audio samplerate commandString << " -R " << hash["AudioSampleRate"] #Audio encoder commandString << " -E " case hash["FileCodecs"] when /AAC/ commandString << "faac" when /AC-3/ commandString << "ac3" when /Vorbis/ commandString << "vorbis" when /MP3/ commandString << "lame" end #Container commandString << " -f " case hash["FileFormat"] when /MP4/ commandString << "mp4" when /AVI/ commandString << "avi" when /OGM/ commandString << "ogm" when /MKV/ commandString << "mkv" end #Cropping if !hash["PictureAutoCrop"].to_i commandString << " --crop " commandString << hash["PictureTopCrop"] commandString << ":" commandString << hash["PictureBottomCrop"] commandString << ":" commandString << hash["PictureLeftCrop"] commandString << ":" commandString << hash["PictureRightCrop"] end #Dimensions if hash["PictureWidth"].to_i != 0 commandString << " -w " commandString << hash["PictureWidth"] end if hash["PictureHeight"].to_i != 0 commandString << " -l " commandString << hash["PictureHeight"] end #Subtitles if hash["Subtitles"] != "None" commandString << " -s " commandString << hash["Subtitles"] end #x264 Options if hash["x264Option"] != "" commandString << " -x " commandString << hash["x264Option"] end #Booleans if hash["ChapterMarkers"].to_i == 1 then commandString << " -m" end if hash["PictureDeinterlace"].to_i == 1 then commandString << " -d" end if hash["PicturePAR"].to_i == 1 then commandString << " -p" end if hash["VideoGrayScale"].to_i == 1 then commandString << " -g" end if hash["VideoTwoPass"].to_i == 1 then commandString << " -2" end if hash["VideoTurboTwoPass"].to_i == 1 then commandString << " -T" end # That's it, print to screen now puts commandString puts "*" * @@columnWidth puts "\n" end def generateAPIcalls(hash) commandString = "if (!strcmp(presetname, \"" << hash["PresetName"] << "\"))\n{\n" #Filename suffix case hash["FileFormat"] when /MP4/ commandString << "job->mux = " << "HB_MUX_MP4;\n" when /AVI/ commandString << "job->mux = " << "HB_MUX_AVI;\n" when /OGM/ commandString << "job->mux = " << "HB_MUX_OGM;\n" when /MKV/ commandString << "job->mux = " << "HB_MUX_MKV;\n" end #Video encoder if hash["VideoEncoder"] != "FFmpeg" commandString << "vcodec = " if hash["VideoEncoder"] == "x264 (h.264 Main)" commandString << "HB_VCODEC_X264;\n" elsif hash["VideoEncoder"] == "x264 (h.264 iPod)" commandString << "HB_VCODEC_X264;\njob->h264_level = 30;\n" elsif hash["VideoEncoder"].to_s.downcase == "xvid" commandString << "HB_VCODEC_XVID;\n" end end #VideoRateControl case hash["VideoQualityType"].to_i when 0 commandString << "size = " << hash["VideoTargetSize"] << ";\n" when 1 commandString << "job->vbitrate = " << hash["VideoAvgBitrate"] << ";\n" when 2 commandString << "job->vquality = " << hash["VideoQualitySlider"] << ";\n" end #FPS if hash["VideoFramerate"] != "Same as source" if hash["VideoFramerate"] == "23.976 (NTSC Film)" commandString << "job->vrate_base = " << "1126125;\n" elsif hash["VideoFramerate"] == "29.97 (NTSC Video)" commandString << "job->vrate_base = " << "900900;\n" # Gotta add the rest of the framerates for completion's sake. end end #Audio bitrate commandString << "job->abitrate = " << hash["AudioBitRate"] << ";\n" #Audio samplerate commandString << "job->arate = " case hash["AudioSampleRate"] when /48/ commandString << "48000" when /44.1/ commandString << "44100" when /32/ commandString << "32000" when /24/ commandString << "24000" when /22.05/ commandString << "22050" end commandString << ";\n" #Audio encoder commandString << "acodec = " case hash["FileCodecs"] when /AAC/ commandString << "HB_ACODEC_FAAC;\n" when /AC-3/ commandString << "HB_ACODEC_AC3;\n" when /Vorbis/ commandString << "HB_ACODEC_VORBIS;\n" when /MP3/ commandString << "HB_ACODEC_LAME;\n" end #Cropping if !hash["PictureAutoCrop"].to_i commandString << "job->crop[0] = " << hash["PictureTopCrop"] << ";\n" commandString << "job->crop[1] = " << hash["PictureBottomCrop"] << ";\n" commandString << "job->crop[2] = " << hash["PictureLeftCrop"] << ";\n" commandString << "job->crop[4] - " << hash["PictureRightCrop"] << ";\n" end #Dimensions if hash["PictureWidth"].to_i != 0 commandString << "job->width = " commandString << hash["PictureWidth"] << ";\n" end if hash["PictureHeight"].to_i != 0 commandString << "job->height = " commandString << hash["PictureHeight"] << ";\n" end #Subtitles if hash["Subtitles"] != "None" commandString << "job->subtitle = " commandString << ( hash["Subtitles"].to_i - 1).to_s << ";\n" end #x264 Options if hash["x264Option"] != "" commandString << ">x264opts = strdup(\"" commandString << hash["x264Option"] << "\");\n" end #Booleans if hash["ChapterMarkers"].to_i == 1 then commandString << "job->chapter_markers = 1;\n" end if hash["PictureDeinterlace"].to_i == 1 then commandString << "job->deinterlace = 1;\n" end if hash["PicturePAR"].to_i == 1 then commandString << "job->pixel_ratio = 1;\n" end if hash["VideoGrayScale"].to_i == 1 then commandString << "job->greyscale = 1;\n" end if hash["VideoTwoPass"].to_i == 1 then commandString << "twoPass = 1;\n" end if hash["VideoTurboTwoPass"].to_i == 1 then commandString << "turbo_opts_enabled = 1;\n" end commandString << "}" # That's it, print to screen now puts commandString #puts "*" * @@columnWidth puts "\n" end def generateCLIPresetList(hash) commandString = "" commandString << "printf(\"\\n+ " << hash["PresetName"] << ": " #Video encoder if hash["VideoEncoder"] != "FFmpeg" commandString << " -e " if hash["VideoEncoder"] == "x264 (h.264 Main)" commandString << "x264" elsif hash["VideoEncoder"] == "x264 (h.264 iPod)" commandString << "x264b30" else commandString << hash["VideoEncoder"].to_s.downcase end end #VideoRateControl case hash["VideoQualityType"].to_i when 0 commandString << " -S " << hash["VideoTargetSize"] when 1 commandString << " -b " << hash["VideoAvgBitrate"] when 2 commandString << " -q " << hash["VideoQualitySlider"] end #FPS if hash["VideoFramerate"] != "Same as source" if hash["VideoFramerate"] == "23.976 (NTSC Film)" commandString << " -r " << "23.976" elsif hash["VideoFramerate"] == "29.97 (NTSC Video)" commandString << " -r " << "29.97" else commandString << " -r " << hash["VideoFramerate"] end end #Audio bitrate commandString << " -B " << hash["AudioBitRate"] #Audio samplerate commandString << " -R " << hash["AudioSampleRate"] #Audio encoder commandString << " -E " case hash["FileCodecs"] when /AAC/ commandString << "faac" when /AC-3/ commandString << "ac3" when /Vorbis/ commandString << "vorbis" when /MP3/ commandString << "lame" end #Container commandString << " -f " case hash["FileFormat"] when /MP4/ commandString << "mp4" when /AVI/ commandString << "avi" when /OGM/ commandString << "ogm" when /MKV/ commandString << "mkv" end #Cropping if !hash["PictureAutoCrop"].to_i commandString << " --crop " commandString << hash["PictureTopCrop"] commandString << ":" commandString << hash["PictureBottomCrop"] commandString << ":" commandString << hash["PictureLeftCrop"] commandString << ":" commandString << hash["PictureRightCrop"] end #Dimensions if hash["PictureWidth"].to_i != 0 commandString << " -w " commandString << hash["PictureWidth"] end if hash["PictureHeight"].to_i != 0 commandString << " -l " commandString << hash["PictureHeight"] end #Subtitles if hash["Subtitles"] != "None" commandString << " -s " commandString << hash["Subtitles"] end #Booleans if hash["ChapterMarkers"].to_i == 1 then commandString << " -m" end if hash["PictureDeinterlace"].to_i == 1 then commandString << " -d" end if hash["PicturePAR"].to_i == 1 then commandString << " -p" end if hash["VideoGrayScale"].to_i == 1 then commandString << " -g" end if hash["VideoTwoPass"].to_i == 1 then commandString << " -2" end if hash["VideoTurboTwoPass"].to_i == 1 then commandString << " -T" end #x264 Options if hash["x264Option"] != "" commandString << " -x " commandString << hash["x264Option"] end commandString << "\\n\");" # That's it, print to screen now puts commandString puts "\n" endendPresetClass.new