OSDN Git Service

- change aspect from a scaled int to a double so we can handle the wider
[handbrake-jp/handbrake-jp-git.git] / scripts / manicure.rb
1 #! /usr/bin/ruby
2 # manincure.rb version 0.66
3
4 # This file is part of the HandBrake source code.
5 # Homepage: <http://handbrake.m0k.org/>.
6 # It may be used under the terms of the GNU General Public License.
7
8 # This script parses HandBrake's Mac presets into hashes, which can
9 # be displayed in various formats for use by the CLI and its wrappers.
10
11 # For handling command line arguments to the script
12 require 'optparse'
13 require 'ostruct'
14
15 # CLI options: (code based on http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html )
16 def readOptions
17   
18   # --[no-]cli-raw, -r gives raw CLI for wiki
19   # --cli-parse, -p gives CLI strings for wrappers
20   # --api, -a gives preset code for test.c
21   # --api-list, -A gives CLI strings for --preset-list display
22   # --[no-]header, -h turns off banner display
23   options = OpenStruct.new
24   options.cliraw = false
25   options.cliparse = false
26   options.api = false
27   options.apilist = false
28   options.header = false
29   
30   opts = OptionParser.new do |opts|
31     opts.banner = "Usage: manicure.rb [options]"
32     
33     opts.separator ""
34     opts.separator "Options:"
35     
36     opts.on("-r", "--cli-raw", "Gives example strings for the HB wiki") do |raw|
37       options.cliraw = raw
38       option_set = true
39     end
40     
41     opts.on("-p", "--cli-parse", "Gives presets as wrapper-parseable CLI", " option strings") do |par|
42       options.cliparse = par
43     end
44     
45     opts.on("-a", "--api", "Gives preset code for test.c") do |api|
46       options.api = api
47     end
48     
49     opts.on("-A", "--api-list", "Gives code for test.c's --preset-list", " options") do |alist|
50       options.apilist = alist
51     end
52     
53     opts.on("-H", "--Header", "Display a banner before each preset") do |head|
54       options.header = head
55     end
56     
57     opts.on_tail("-h", "--help", "Show this message") do
58         puts opts
59         exit
60     end
61   end.parse!
62   
63   return options
64   
65 end
66
67 # These arrays contain all the other presets and hashes that are going to be used.
68 # Yeah, they're global variables. In an object-oriented scripting language.
69 # Real smooth, huh?
70
71 # This class parses the user's presets .plist into an array of hashes
72 class Presets
73   
74   attr_reader :hashMasterList
75   
76   # Running initialization runs everything.
77   # Calling it will also call the parser
78   # and display output.
79   def initialize
80     
81     # Grab input from the user's presets .plist
82     rawPresets = readPresetPlist
83     
84     # Store all the presets in here
85     presetStew = []
86
87     # Each item in the array is one line from the .plist
88     presetStew = rawPresets.split("\n")
89     
90     # Now get rid of white space
91     presetStew = cleanStew(presetStew)
92     
93     # This stores the offsets between presets.
94     presetBreaks = findPresetBreaks(presetStew)
95
96     # Now it's time to use that info to store each
97     # preset individually, in the master list.
98     @presetMasterList = []
99     i = 0
100     while i <= presetBreaks.size    
101       if i == 0 #first preset
102         # Grab the stew, up to the 1st offset.
103         @presetMasterList[i] = presetStew.slice(0..presetBreaks[i].to_i)
104       elsif i < presetBreaks.size #middle presets
105         # Grab the stew from the last offset to the current..
106         @presetMasterList[i] = presetStew.slice(presetBreaks[i-1].to_i..presetBreaks[i].to_i)
107       else #final preset
108         # Grab the stew, starting at the last offset, all the way to the end.
109         @presetMasterList[i] = presetStew.slice(presetBreaks[i-1].to_i..presetStew.length)
110       end
111       i += 1
112     end
113     
114     # Parse the presets into hashes
115     @hashMasterList = []
116     
117     buildPresetHash
118     
119   end
120
121   def readPresetPlist # Grab the .plist and store it in presets
122     
123     # Grab the user's home path
124     homeLocation = `echo $HOME`.chomp
125     
126     # Use that to build a path to the presets .plist
127     inputFile = homeLocation+'/Library/Application\ Support/HandBrake/UserPresets.plist'
128     
129     # Builds a command that inputs the .plist, but not before stripping all the XML gobbledygook.
130     parseCommand = 'cat '+inputFile+' | sed -e \'s/<[a-z]*>//\' -e \'s/<\/[a-z]*>//\'  -e \'/<[?!]/d\' '
131     
132     puts "\n\n"
133     
134     # Run the command, return the raw presets
135     rawPresets = `#{parseCommand}`
136   end
137
138   def cleanStew(presetStew) #remove tabbed white space
139     presetStew.each do |oneline|
140       oneline.strip!
141     end
142   end
143
144   def findPresetBreaks(presetStew) #figure out where each preset starts and ends
145     i = 0
146     j = 0
147     presetBreaks =[]
148     presetStew.each do |presetLine|
149       if presetLine =~ /Audio1Bitrate/ # This is the first line of a new preset.
150         presetBreaks[j] = i-1         # So mark down how long the last one was.
151         j += 1
152       end
153     i += 1
154     end
155     return presetBreaks
156   end
157
158   def buildPresetHash #fill up @hashMasterList with hashes of all key/value pairs
159     j = 0
160     
161     # Iterate through all presets, treating each in turn as singleServing
162     @presetMasterList.each do |singleServing|
163       
164       # Initialize the hash for preset j (aka singleServing)
165       @hashMasterList[j] = Hash.new
166       
167       # Each key and value are on sequential lines.
168       # Iterating through by twos, use that to build a hash.
169       # Each key, on line i, paired with its value, on line i+1  
170       i = 1
171       while i < singleServing.length
172         @hashMasterList[j].store( singleServing[i],  singleServing[i+1] )
173         i += 2
174       end
175             
176       j += 1  
177     end   
178   end
179
180 end
181
182 # This class displays the presets to stdout in various formats.
183 class Display
184   
185   
186   def initialize(hashMasterList, options)
187   
188     @hashMasterList = hashMasterList
189     @options = options
190
191     # A width of 40 gives nice, compact output.
192     @columnWidth=40
193     
194     # Print to screen.
195     displayCommandStrings
196     
197   end
198   
199   def displayCommandStrings # prints everything to screen
200     
201     # Iterate through the hashes.    
202     @hashMasterList.each do |hash|
203     
204       # Check to make there are valid contents
205       if hash.key?("PresetName")
206         
207         if @options.header == true
208           # First throw up a header to make each preset distinct
209           displayHeader(hash)
210         end
211         
212         if @options.cliraw == true
213           # Show the preset's full CLI string equivalent
214           generateCLIString(hash)
215         end
216         
217         if @options.cliparse == true
218           generateCLIParse(hash)
219         end
220         
221         if @options.api == true
222           # Show the preset as code for test/test.c, HandBrakeCLI
223           generateAPIcalls(hash)
224         end
225         
226         if @options.apilist == true
227           # Show the preset as print statements, for CLI wrappers to parse.
228           generateAPIList(hash) 
229         end
230       end
231     end    
232   end
233   
234   def displayHeader(hash) # A distinct banner to separate each preset
235     
236     # Print a line of asterisks
237     puts "*" * @columnWidth
238     
239     # Print the name, centered
240     puts '* '+hash["PresetName"].to_s.center(@columnWidth-4)+' *'
241     
242     # Print a line of dashes
243     puts '~' * @columnWidth
244     
245     # Print the description, centered and word-wrapped
246     puts hash["PresetDescription"].to_s.center(@columnWidth).gsub(/\n/," ").scan(/\S.{0,#{@columnWidth-2}}\S(?=\s|$)|\S+/)
247     
248     # Print another line of dashes
249     puts '~' * @columnWidth
250     
251     # Print the formats the preset uses
252     puts "#{hash["FileCodecs"]}".center(@columnWidth)
253     
254     # Note if the preset isn't built-in
255     if hash["Type"].to_i == 1
256       puts "Custom Preset".center(@columnWidth)
257     end
258
259     # Note if the preset is marked as default.
260     if hash["Default"].to_i == 1
261       puts "This is your default preset.".center(@columnWidth)
262     end
263     
264     # End with a line of tildes.  
265     puts "~" * @columnWidth
266     
267   end
268   
269   def generateCLIString(hash) # Makes a full CLI equivalent of a preset
270     commandString = ""
271     commandString << './HandBrakeCLI -i DVD -o ~/Movies/movie.'
272     
273     #Filename suffix
274     case hash["FileFormat"]
275     when /MP4/
276       commandString << "mp4 "
277     when /AVI/
278       commandString << "avi "
279     when /OGM/
280       commandString << "ogm "
281     when /MKV/
282       commandString << "mkv "
283     end
284     
285     #Video encoder
286     if hash["VideoEncoder"] != "MPEG-4 (FFmpeg)"
287       commandString << " -e "
288       case hash["VideoEncoder"]
289       when /x264/
290         commandString << "x264"
291       when /XviD/
292         commandString << "xvid"
293       end
294     end
295
296     #VideoRateControl
297     case hash["VideoQualityType"].to_i
298     when 0
299       commandString << " -S " << hash["VideoTargetSize"]
300     when 1
301       commandString << " -b " << hash["VideoAvgBitrate"]
302     when 2
303       commandString << " -q " << hash["VideoQualitySlider"]
304     end
305
306     #FPS
307     if hash["VideoFramerate"] != "Same as source"
308       if hash["VideoFramerate"] == "23.976 (NTSC Film)"
309         commandString << " -r " << "23.976"
310       elsif hash["VideoFramerate"] == "29.97 (NTSC Video)"
311         commandString << " -r " << "29.97"
312       else
313         commandString << " -r " << hash["VideoFramerate"]
314       end
315     end
316     
317     #Audio tracks
318     commandString << " -a "
319     commandString << hash["Audio1Track"]
320     if hash["Audio2Track"]
321       commandString << "," << hash["Audio2Track"]
322     end
323     if hash["Audio3Track"]
324       commandString << "," << hash["Audio3Track"]
325     end
326     if hash["Audio4Track"]
327       commandString << "," << hash["Audio4Track"]
328     end
329     
330     #Audio encoders
331     commandString << " -E "
332     case hash["Audio1Encoder"]
333     when /AC3 /
334       commandString << "ac3"
335     when /AAC/
336       commandString << "faac"
337     when /Vorbis/
338       commandString << "vorbis"
339     when /MP3/
340       commandString << "lame"
341     end
342     case hash["Audio2Encoder"]
343     when /AC3 /
344       commandString << ",ac3"
345     when /AAC/
346       commandString << ",faac"
347     when /Vorbis/
348       commandString << ",vorbis"
349     when /MP3/
350       commandString << ",lame"
351     end
352     case hash["Audio3Encoder"]
353     when /AC3 /
354       commandString << ",ac3"
355     when /AAC/
356       commandString << ",faac"
357     when /Vorbis/
358       commandString << ",vorbis"
359     when /MP3/
360       commandString << ",lame"
361     end
362     case hash["Audio4Encoder"]
363     when /AC3 /
364       commandString << ",ac3"
365     when /AAC/
366       commandString << ",faac"
367     when /Vorbis/
368       commandString << ",vorbis"
369     when /MP3/
370       commandString << ",lame"
371     end
372     
373     #Audio bit rate
374     commandString << " -B "
375     commandString << hash["Audio1Bitrate"]
376     if hash["Audio2Bitrate"]
377       if hash["Audio2Encoder"] != "AC3 Passthru"
378         commandString << "," << hash["Audio2Bitrate"]
379       else
380         commandString << "," << "auto"
381       end
382     end
383     if hash["Audio3Bitrate"]
384       if hash["Audio3Encoder"] != "AC3 Passthru"
385         commandString << "," << hash["Audio3Bitrate"]
386       else
387         commandString << "," << "auto"
388       end
389     end
390     if hash["Audio4Bitrate"]
391       if hash["Audio4Encoder"] != "AC3 Passthru"
392         commandString << "," << hash["Audio4Bitrate"]
393       else
394         commandString << "," << "auto"
395       end
396     end
397
398     #Audio sample rate
399     commandString << " -R "
400     commandString << hash["Audio1Samplerate"]
401     if hash["Audio2Samplerate"]
402       commandString << "," << hash["Audio2Samplerate"]
403     end
404     if hash["Audio3Samplerate"]
405       commandString << "," << hash["Audio3Samplerate"]
406     end
407     if hash["Audio4Samplerate"]
408       commandString << "," << hash["Audio4Samplerate"]
409     end
410     
411     #Audio Mixdown
412     commandString << " -6 "
413     case hash["Audio1Mixdown"]
414     when /Mono/
415       commandString << "mono"
416     when /Stereo/
417       commandString << "stereo"
418     when /Dolby Surround/
419       commandString << "dpl1"
420     when /Dolby Pro Logic II/
421       commandString << "dpl2"
422     when /discrete/
423       commandString << "6ch"
424     when /Passthru/
425       commandString << "auto"
426     end
427     
428     if hash["Audio2Mixdown"]
429       case hash["Audio2Mixdown"]
430       when /Mono/
431         commandString << ",mono"
432       when /Stereo/
433         commandString << ",stereo"
434       when /Dolby Surround/
435         commandString << ",dpl1"
436       when /Dolby Pro Logic II/
437         commandString << ",dpl2"
438       when /discrete/
439         commandString << ",6ch"
440       when /Passthru/
441         commandString << ",auto"
442       end
443     end
444     
445     if hash["Audio3Mixdown"]
446       case hash["Audio3Mixdown"]
447       when /Mono/
448         commandString << ",mono"
449       when /Stereo/
450         commandString << ",stereo"
451       when /Dolby Surround/
452         commandString << ",dpl1"
453       when /Dolby Pro Logic II/
454         commandString << ",dpl2"
455       when /discrete/
456         commandString << ",6ch"
457       when /Passthru/
458         commandString << ",auto"
459       end
460     end
461     
462     if hash["Audio4Mixdown"]
463       case hash["Audio4Mixdown"]
464       when /Mono/
465         commandString << ",mono"
466       when /Stereo/
467         commandString << ",stereo"
468       when /Dolby Surround/
469         commandString << ",dpl1"
470       when /Dolby Pro Logic II/
471         commandString << ",dpl2"
472       when /discrete/
473         commandString << ",6ch"
474       when /Passthru/
475         commandString << ",auto"
476       end
477     end
478     
479     #Container
480     commandString << " -f "
481     case hash["FileFormat"]
482     when /MP4/
483       commandString << "mp4"
484     when /AVI/
485       commandString << "avi"
486     when /OGM/
487       commandString << "ogm"
488     when /MKV/
489       commandString << "mkv"
490     end
491     
492     #iPod MP4 atom
493     if hash["Mp4iPodCompatible"].to_i == 1
494       commandString << " -I"
495     end
496     
497     # 64-bit files
498     if hash["Mp4LargeFile"].to_i == 1
499       commandString << " -4"
500     end
501     
502     #Cropping
503     if !hash["PictureAutoCrop"].to_i
504       commandString << " --crop "
505       commandString << hash["PictureTopCrop"]
506       commandString << ":"
507       commandString << hash["PictureBottomCrop"]
508       commandString << ":"
509       commandString << hash["PictureLeftCrop"]
510       commandString << ":"
511       commandString << hash["PictureRightCrop"]
512     end
513     
514     #Dimensions
515     if hash["PictureWidth"].to_i != 0
516       commandString << " -w "
517       commandString << hash["PictureWidth"]
518     end
519     if hash["PictureHeight"].to_i != 0
520       commandString << " -l "
521       commandString << hash["PictureHeight"]
522     end
523     
524     #Subtitles
525     if hash["Subtitles"] != "None"
526       commandString << " -s "
527       commandString << hash["Subtitles"]
528     end
529
530     #Video Filters
531     if hash["UsesPictureFilters"].to_i == 1
532       
533       case hash["PictureDeinterlace"].to_i
534       when 1
535         commandString << " --deinterlace=\"fast\""
536       when 2
537         commandString << " --deinterlace=\slow\""
538       when 3
539         commandString << " --deinterlace=\"slower\""
540       when 4
541         commandString << " --deinterlace=\"slowest\""
542       end
543       
544       case hash["PictureDenoise"].to_i
545       when 1
546         commandString << " --denoise=\"weak\""
547       when 2
548         commandString << " --denoise=\"medium\""
549       when 3
550         commandString << " --denoise=\"strong\""
551       end
552       
553       if hash["PictureDetelecine"].to_i == 1 then commandString << " --detelecine" end
554       if hash["PictureDeblock"].to_i == 1 then commandString << " --deblock" end
555       if hash["VFR"].to_i == 1 then commandString << " --vfr" end
556     end
557
558     #Booleans
559     if hash["ChapterMarkers"].to_i == 1 then commandString << " -m" end
560     if hash["PicturePAR"].to_i == 1 then commandString << " -p" end
561     if hash["VideoGrayScale"].to_i == 1 then commandString << " -g" end
562     if hash["VideoTwoPass"].to_i == 1 then commandString << " -2" end
563     if hash["VideoTurboTwoPass"].to_i == 1 then commandString << " -T" end
564
565     #x264 Options
566     if hash["x264Option"] != ""
567       commandString << " -x "
568       commandString << hash["x264Option"]
569     end
570     
571     # That's it, print to screen now
572     puts commandString
573     
574     #puts "*" * @columnWidth
575
576     puts  "\n"
577   end
578
579   def generateCLIParse(hash) # Makes a CLI equivalent of all user presets, for wrappers to parse
580     commandString = ""
581     commandString << '+ ' << hash["PresetName"] << ":"
582         
583     #Video encoder
584     if hash["VideoEncoder"] != "MPEG-4 (FFmpeg)"
585       commandString << " -e "
586       case hash["VideoEncoder"]
587       when /x264/
588         commandString << "x264"
589       when /XviD/
590         commandString << "xvid"
591       end
592     end
593
594     #VideoRateControl
595     case hash["VideoQualityType"].to_i
596     when 0
597       commandString << " -S " << hash["VideoTargetSize"]
598     when 1
599       commandString << " -b " << hash["VideoAvgBitrate"]
600     when 2
601       commandString << " -q " << hash["VideoQualitySlider"]
602     end
603
604     #FPS
605     if hash["VideoFramerate"] != "Same as source"
606       if hash["VideoFramerate"] == "23.976 (NTSC Film)"
607         commandString << " -r " << "23.976"
608       elsif hash["VideoFramerate"] == "29.97 (NTSC Video)"
609         commandString << " -r " << "29.97"
610       else
611         commandString << " -r " << hash["VideoFramerate"]
612       end
613     end
614     
615     #Audio tracks
616     commandString << " -a "
617     commandString << hash["Audio1Track"]
618     if hash["Audio2Track"]
619       commandString << "," << hash["Audio2Track"]
620     end
621     if hash["Audio3Track"]
622       commandString << "," << hash["Audio3Track"]
623     end
624     if hash["Audio4Track"]
625       commandString << "," << hash["Audio4Track"]
626     end
627     
628     #Audio encoders
629     commandString << " -E "
630     case hash["Audio1Encoder"]
631     when /AC3/
632       commandString << "ac3"
633     when /AAC/
634       commandString << "faac"
635     when /Vorbis/
636       commandString << "vorbis"
637     when /MP3/
638       commandString << "lame"
639     end
640     case hash["Audio2Encoder"]
641     when /AC3 /
642       commandString << ",ac3"
643     when /AAC/
644       commandString << ",faac"
645     when /Vorbis/
646       commandString << ",vorbis"
647     when /MP3/
648       commandString << ",lame"
649     end
650     case hash["Audio3Encoder"]
651     when /AC3 /
652       commandString << ",ac3"
653     when /AAC/
654       commandString << ",faac"
655     when /Vorbis/
656       commandString << ",vorbis"
657     when /MP3/
658       commandString << ",lame"
659     end
660     case hash["Audio4Encoder"]
661     when /AC3 /
662       commandString << ",ac3"
663     when /AAC/
664       commandString << ",faac"
665     when /Vorbis/
666       commandString << ",vorbis"
667     when /MP3/
668       commandString << ",lame"
669     end
670
671     #Audio bit rate
672     commandString << " -B "
673     commandString << hash["Audio1Bitrate"]
674     if hash["Audio2Bitrate"]
675       if hash["Audio2Encoder"] != "AC3 Passthru"
676         commandString << "," << hash["Audio2Bitrate"]
677       else
678         commandString << "," << "auto"
679       end
680     end
681     if hash["Audio3Bitrate"]
682       if hash["Audio3Encoder"] != "AC3 Passthru"
683         commandString << "," << hash["Audio3Bitrate"]
684       else
685         commandString << "," << "auto"
686       end
687     end
688     if hash["Audio4Bitrate"]
689       if hash["Audio4Encoder"] != "AC3 Passthru"
690         commandString << "," << hash["Audio4Bitrate"]
691       else
692         commandString << "," << "auto"
693       end
694     end
695
696     #Audio sample rate
697     commandString << " -R "
698     commandString << hash["Audio1Samplerate"]
699     if hash["Audio2Samplerate"]
700       commandString << "," << hash["Audio2Samplerate"]
701     end
702     if hash["Audio3Samplerate"]
703       commandString << "," << hash["Audio3Samplerate"]
704     end
705     if hash["Audio4Samplerate"]
706       commandString << "," << hash["Audio4Samplerate"]
707     end
708     
709     #Audio Mixdown
710     commandString << " -6 "
711     case hash["Audio1Mixdown"]
712     when /Mono/
713       commandString << "mono"
714     when /Stereo/
715       commandString << "stereo"
716     when /Dolby Surround/
717       commandString << "dpl1"
718     when /Dolby Pro Logic II/
719       commandString << "dpl2"
720     when /discrete/
721       commandString << "6ch"
722     when /Passthru/
723       commandString << "auto"
724     end
725     
726     if hash["Audio2Mixdown"]
727       case hash["Audio2Mixdown"]
728       when /Mono/
729         commandString << ",mono"
730       when /Stereo/
731         commandString << ",stereo"
732       when /Dolby Surround/
733         commandString << ",dpl1"
734       when /Dolby Pro Logic II/
735         commandString << ",dpl2"
736       when /discrete/
737         commandString << ",6ch"
738       when /Passthru/
739         commandString << ",auto"
740       end
741     end
742     
743     if hash["Audio3Mixdown"]
744       case hash["Audio3Mixdown"]
745       when /Mono/
746         commandString << ",mono"
747       when /Stereo/
748         commandString << ",stereo"
749       when /Dolby Surround/
750         commandString << ",dpl1"
751       when /Dolby Pro Logic II/
752         commandString << ",dpl2"
753       when /discrete/
754         commandString << ",6ch"
755       when /Passthru/
756         commandString << ",auto"
757       end
758     end
759     
760     if hash["Audio4Mixdown"]
761       case hash["Audio4Mixdown"]
762       when /Mono/
763         commandString << ",mono"
764       when /Stereo/
765         commandString << ",stereo"
766       when /Dolby Surround/
767         commandString << ",dpl1"
768       when /Dolby Pro Logic II/
769         commandString << ",dpl2"
770       when /discrete/
771         commandString << ",6ch"
772       when /Passthru/
773         commandString << ",auto"
774       end
775     end
776
777     
778     #Container
779     commandString << " -f "
780     case hash["FileFormat"]
781     when /MP4/
782       commandString << "mp4"
783     when /AVI/
784       commandString << "avi"
785     when /OGM/
786       commandString << "ogm"
787     when /MKV/
788       commandString << "mkv"
789     end
790     
791     #iPod MP4 atom
792     if hash["Mp4iPodCompatible"].to_i == 1
793       commandString << " -I"
794     end
795     
796     # 64-bit files
797     if hash["Mp4LargeFile"].to_i == 1
798       commandString << " -4"
799     end
800     
801     #Cropping
802     if !hash["PictureAutoCrop"].to_i
803       commandString << " --crop "
804       commandString << hash["PictureTopCrop"]
805       commandString << ":"
806       commandString << hash["PictureBottomCrop"]
807       commandString << ":"
808       commandString << hash["PictureLeftCrop"]
809       commandString << ":"
810       commandString << hash["PictureRightCrop"]
811     end
812     
813     #Dimensions
814     if hash["PictureWidth"].to_i != 0
815       commandString << " -w "
816       commandString << hash["PictureWidth"]
817     end
818     if hash["PictureHeight"].to_i != 0
819       commandString << " -l "
820       commandString << hash["PictureHeight"]
821     end
822     
823     #Subtitles
824     if hash["Subtitles"] != "None"
825       commandString << " -s "
826       commandString << hash["Subtitles"]
827     end
828     
829     #Video Filters
830     if hash["UsesPictureFilters"].to_i == 1
831       
832       case hash["PictureDeinterlace"].to_i
833       when 1
834         commandString << " --deinterlace=\"fast\""
835       when 2
836         commandString << " --deinterlace=\slow\""
837       when 3
838         commandString << " --deinterlace=\"slower\""
839       when 4
840         commandString << " --deinterlace=\"slowest\""
841       end
842       
843       case hash["PictureDenoise"].to_i
844       when 1
845         commandString << " --denoise=\"weak\""
846       when 2
847         commandString << " --denoise=\"medium\""
848       when 3
849         commandString << " --denoise=\"strong\""
850       end
851       
852       if hash["PictureDetelecine"].to_i == 1 then commandString << " --detelecine" end
853       if hash["PictureDeblock"].to_i == 1 then commandString << " --deblock" end
854       if hash["VFR"].to_i == 1 then commandString << " --vfr" end
855     end
856
857     #Booleans
858     if hash["ChapterMarkers"].to_i == 1 then commandString << " -m" end
859     if hash["PicturePAR"].to_i == 1 then commandString << " -p" end
860     if hash["VideoGrayScale"].to_i == 1 then commandString << " -g" end
861     if hash["VideoTwoPass"].to_i == 1 then commandString << " -2" end
862     if hash["VideoTurboTwoPass"].to_i == 1 then commandString << " -T" end
863
864     #x264 Options
865     if hash["x264Option"] != ""
866       commandString << " -x "
867       commandString << hash["x264Option"]
868     end
869     
870     # That's it, print to screen now
871     puts commandString
872     
873     #puts "*" * @columnWidth
874
875     puts  "\n"
876   end
877
878   def generateAPIcalls(hash) # Makes a C version of the preset ready for coding into the CLI
879     
880     commandString = "if (!strcmp(preset_name, \"" << hash["PresetName"] << "\"))\n{\n    "
881     
882     #Filename suffix
883     case hash["FileFormat"]
884     when /MP4/
885       commandString << "mux = " << "HB_MUX_MP4;\n    "
886     when /AVI/
887       commandString << "mux = " << "HB_MUX_AVI;\n    "
888     when /OGM/
889       commandString << "mux = " << "HB_MUX_OGM;\n    "
890     when /MKV/
891       commandString << "mux = " << "HB_MUX_MKV;\n    "
892     end
893     
894     #iPod MP4 atom
895     if hash["Mp4iPodCompatible"].to_i == 1
896       commandString << "job->ipod_atom = 1;\n   "
897     end
898     
899     # 64-bit files
900     if hash["Mp4LargeFile"].to_i == 1
901       commandString << "job->largeFileSize = 1;\n"
902     end
903     
904     #Video encoder
905     if hash["VideoEncoder"] != "MPEG-4 (FFmpeg)"
906       commandString << "vcodec = "
907       case hash["VideoEncoder"]
908       when /x264/
909         commandString << "HB_VCODEC_X264;\n    "
910       when /XviD/
911         commandString << "HB_VCODEC_XVID;\n    "        
912       end
913     end
914
915     #VideoRateControl
916     case hash["VideoQualityType"].to_i
917     when 0
918       commandString << "size = " << hash["VideoTargetSize"] << ";\n    "
919     when 1
920       commandString << "job->vbitrate = " << hash["VideoAvgBitrate"] << ";\n    "
921     when 2
922       commandString << "job->vquality = " << hash["VideoQualitySlider"] << ";\n    "
923       commandString << "job->crf = 1;\n    "
924     end
925
926     #FPS
927     if hash["VideoFramerate"] != "Same as source"
928       if hash["VideoFramerate"] == "23.976 (NTSC Film)"
929         commandString << "job->vrate_base = " << "1126125;\n    "
930       elsif hash["VideoFramerate"] == "29.97 (NTSC Video)"
931         commandString << "job->vrate_base = " << "900900;\n    "
932       # Gotta add the rest of the framerates for completion's sake.
933       end
934       commandString << "job->cfr = 1;\n    "
935     end
936     
937     #Audio tracks
938     commandString << "atracks = \""
939     commandString << hash["Audio1Track"]
940     if hash["Audio2Track"]
941       commandString << "," << hash["Audio2Track"]
942     end
943     if hash["Audio3Track"]
944       commandString << "," << hash["Audio3Track"]
945     end
946     if hash["Audio4Track"]
947       commandString << "," << hash["Audio4Track"]
948     end
949     commandString << "\";\n    "
950     
951     # Audio bitrate
952     commandString << "abitrates = \""
953     if hash["Audio1Encoder"] != "AC3 Passthru"
954       commandString << hash["Audio1Bitrate"]
955     else
956       commandString << "auto"
957     end
958     if hash["Audio2Bitrate"]
959       if hash["Audio2Encoder"] != "AC3 Passthru"
960         commandString << "," << hash["Audio2Bitrate"]
961       else
962         commandString << "," << "auto"
963       end
964     end
965     if hash["Audio3Bitrate"]
966       if hash["Audio3Encoder"] != "AC3 Passthru"
967         commandString << "," << hash["Audio3Bitrate"]
968       else
969         commandString << "," << "auto"
970       end
971     end
972     if hash["Audio4Bitrate"]
973       if hash["Audio4Encoder"] != "AC3 Passthru"
974         commandString << "," << hash["Audio4Bitrate"]
975       else
976         commandString << "," << "auto"
977       end
978     end
979     commandString << "\";\n   "
980         
981     #Audio samplerate
982     commandString << "arates = \""
983     commandString << hash["Audio1Samplerate"]
984     if hash["Audio2Samplerate"]
985       commandString << "," << hash["Audio2Samplerate"]
986     end
987     if hash["Audio3Samplerate"]
988       commandString << "," << hash["Audio3Samplerate"]
989     end
990     if hash["Audio4Samplerate"]
991       commandString << "," << hash["Audio4Samplerate"]
992     end
993     commandString << "\";\n    "
994       
995     #Audio encoder
996     commandString << "acodecs = \""
997     case hash["Audio1Encoder"]
998     when /AC3/
999       commandString << "ac3"
1000     when /AAC/
1001       commandString << "faac"
1002     when /Vorbis/
1003       commandString << "vorbis"
1004     when /MP3/
1005       commandString << "lame"
1006     end
1007     case hash["Audio2Encoder"]
1008     when /AC3 /
1009       commandString << ",ac3"
1010     when /AAC/
1011       commandString << ",faac"
1012     when /Vorbis/
1013       commandString << ",vorbis"
1014     when /MP3/
1015       commandString << ",lame"
1016     end
1017     case hash["Audio3Encoder"]
1018     when /AC3 /
1019       commandString << ",ac3"
1020     when /AAC/
1021       commandString << ",faac"
1022     when /Vorbis/
1023       commandString << ",vorbis"
1024     when /MP3/
1025       commandString << ",lame"
1026     end
1027     case hash["Audio4Encoder"]
1028     when /AC3 /
1029       commandString << ",ac3"
1030     when /AAC/
1031       commandString << ",faac"
1032     when /Vorbis/
1033       commandString << ",vorbis"
1034     when /MP3/
1035       commandString << ",lame"
1036     end
1037     commandString << "\";\n    "
1038     
1039     #Audio mixdowns
1040     commandString << "mixdowns = \""
1041     case hash["Audio1Mixdown"]
1042     when /Mono/
1043       commandString << "mono"
1044     when /Stereo/
1045       commandString << "stereo"
1046     when /Dolby Surround/
1047       commandString << "dpl1"
1048     when /Dolby Pro Logic II/
1049       commandString << "dpl2"
1050     when /discrete/
1051       commandString << "6ch"
1052     when /Passthru/
1053       commandString << "auto"
1054     end
1055     if hash["Audio2Mixdown"]
1056       case hash["Audio2Mixdown"]
1057       when /Mono/
1058         commandString << ",mono"
1059       when /Stereo/
1060         commandString << ",stereo"
1061       when /Dolby Surround/
1062         commandString << ",dpl1"
1063       when /Dolby Pro Logic II/
1064         commandString << ",dpl2"
1065       when /discrete/
1066         commandString << ",6ch"
1067       when /Passthru/
1068         commandString << ",auto"
1069       end
1070     end
1071     if hash["Audio3Mixdown"]
1072       case hash["Audio3Mixdown"]
1073       when /Mono/
1074         commandString << ",mono"
1075       when /Stereo/
1076         commandString << ",stereo"
1077       when /Dolby Surround/
1078         commandString << ",dpl1"
1079       when /Dolby Pro Logic II/
1080         commandString << ",dpl2"
1081       when /discrete/
1082         commandString << ",6ch"
1083       when /Passthru/
1084         commandString << ",auto"
1085       end
1086     end
1087     if hash["Audio4Mixdown"]
1088       case hash["Audio4Mixdown"]
1089       when /Mono/
1090         commandString << ",mono"
1091       when /Stereo/
1092         commandString << ",stereo"
1093       when /Dolby Surround/
1094         commandString << ",dpl1"
1095       when /Dolby Pro Logic II/
1096         commandString << ",dpl2"
1097       when /discrete/
1098         commandString << ",6ch"
1099       when /Passthru/
1100         commandString << ",auto"
1101       end
1102     end
1103     commandString << "\";\n    "
1104     
1105     #Cropping
1106     if !hash["PictureAutoCrop"].to_i
1107       commandString << "job->crop[0] = " << hash["PictureTopCrop"] << ";\n    "
1108       commandString << "job->crop[1] = " << hash["PictureBottomCrop"] << ";\n    "
1109       commandString << "job->crop[2] = " << hash["PictureLeftCrop"] << ";\n    "
1110       commandString << "job->crop[4] - " << hash["PictureRightCrop"] << ";\n    "
1111     end
1112     
1113     #Dimensions
1114     if hash["PictureWidth"].to_i != 0
1115       commandString << "job->width = "
1116       commandString << hash["PictureWidth"] << ";\n    "
1117     end
1118     if hash["PictureHeight"].to_i != 0
1119       commandString << "job->height = "
1120       commandString << hash["PictureHeight"] << ";\n    "
1121     end
1122     
1123     #Subtitles
1124     if hash["Subtitles"] != "None"
1125       commandString << "job->subtitle = "
1126       commandString << ( hash["Subtitles"].to_i - 1).to_s << ";\n    "
1127     end
1128     
1129     #x264 Options
1130     if hash["x264Option"] != ""
1131       commandString << "x264opts = strdup(\""
1132       commandString << hash["x264Option"] << "\");\n    "
1133     end
1134     
1135     #Video Filters
1136     if hash["UsesPictureFilters"].to_i == 1
1137       
1138       case hash["PictureDeinterlace"].to_i
1139       when 1
1140         commandString << "deinterlace = 1;\n    "
1141         commandString << "deinterlace_opt = \"-1\";\n    "
1142       when 2
1143         commandString << "deinterlace = 1;\n    "
1144         commandString << "deinterlace_opt = \"2\";\n    "
1145       when 3
1146         commandString << "deinterlace = 1;\n    "
1147         commandString << "deinterlace_opt = \"0\";\n    "
1148       when 4
1149         commandString << "deinterlace = 1;\n    "
1150         commandString << "deinterlace_opt = \"1:-1:1\";\n    "
1151       end
1152       
1153       case hash["PictureDenoise"].to_i
1154       when 1
1155         commandString << "denoise = 1;\n    "
1156         commandString << "denoise_opt = \"2:1:2:3\";\n    "
1157       when 2
1158         commandString << "denoise = 1;\n    "
1159         commandString << "denoise_opt = \"3:2:2:3\";\n    "
1160       when 3
1161         commandString << "denoise = 1;\n    "
1162         commandString << "denoise_opt = \"7:7:5:5\";\n    "
1163       end
1164       
1165       if hash["PictureDetelecine"].to_i == 1 then commandString << "detelecine = 1;\n    " end
1166       if hash["PictureDeblock"].to_i == 1 then commandString << "deblock = 1;\n    " end
1167       if hash["VFR"].to_i == 1 then commandString << "vfr = 1;\n    " end
1168     end
1169     
1170     #Booleans
1171     if hash["ChapterMarkers"].to_i == 1 then commandString << "job->chapter_markers = 1;\n    " end
1172     if hash["PicturePAR"].to_i == 1 then commandString << "pixelratio = 1;\n    " end
1173     if hash["VideoGrayScale"].to_i == 1 then commandString << "job->grayscale = 1;\n    " end
1174     if hash["VideoTwoPass"].to_i == 1 then commandString << "twoPass = 1;\n    " end
1175     if hash["VideoTurboTwoPass"].to_i == 1 then commandString << "turbo_opts_enabled = 1;\n" end
1176     
1177     commandString << "}"
1178     
1179     # That's it, print to screen now
1180     puts commandString
1181     #puts "*" * @columnWidth
1182     puts  "\n"
1183   end
1184
1185   def generateAPIList(hash) # Makes a list of the CLI options a built-in CLI preset uses, for wrappers to parse
1186     commandString = ""
1187     commandString << "    printf(\"\\n+ " << hash["PresetName"] << ": "
1188         
1189     #Video encoder
1190     if hash["VideoEncoder"] != "MPEG-4 (FFmpeg)"
1191       commandString << " -e "
1192       case hash["VideoEncoder"]
1193       when /x264/
1194         commandString << "x264 "
1195       when /XviD/
1196         commandString << "xvid "
1197       end
1198     end
1199
1200     #VideoRateControl
1201     case hash["VideoQualityType"].to_i
1202     when 0
1203       commandString << " -S " << hash["VideoTargetSize"]
1204     when 1
1205       commandString << " -b " << hash["VideoAvgBitrate"]
1206     when 2
1207       commandString << " -q " << hash["VideoQualitySlider"]
1208     end
1209
1210     #FPS
1211     if hash["VideoFramerate"] != "Same as source"
1212       if hash["VideoFramerate"] == "23.976 (NTSC Film)"
1213         commandString << " -r " << "23.976"
1214       elsif hash["VideoFramerate"] == "29.97 (NTSC Video)"
1215         commandString << " -r " << "29.97"
1216       else
1217         commandString << " -r " << hash["VideoFramerate"]
1218       end
1219     end
1220     
1221     #Audio tracks
1222     commandString << " -a "
1223     commandString << hash["Audio1Track"]
1224     if hash["Audio2Track"]
1225       commandString << "," << hash["Audio2Track"]
1226     end
1227     if hash["Audio3Track"]
1228       commandString << "," << hash["Audio3Track"]
1229     end
1230     if hash["Audio4Track"]
1231       commandString << "," << hash["Audio4Track"]
1232     end
1233     
1234     #Audio encoders
1235     commandString << " -E "
1236     case hash["Audio1Encoder"]
1237     when /AC3/
1238       commandString << "ac3"
1239     when /AAC/
1240       commandString << "faac"
1241     when /Vorbis/
1242       commandString << "vorbis"
1243     when /MP3/
1244       commandString << "lame"
1245     end
1246     case hash["Audio2Encoder"]
1247     when /AC3 /
1248       commandString << ",ac3"
1249     when /AAC/
1250       commandString << ",faac"
1251     when /Vorbis/
1252       commandString << ",vorbis"
1253     when /MP3/
1254       commandString << ",lame"
1255     end
1256     case hash["Audio3Encoder"]
1257     when /AC3 /
1258       commandString << ",ac3"
1259     when /AAC/
1260       commandString << ",faac"
1261     when /Vorbis/
1262       commandString << ",vorbis"
1263     when /MP3/
1264       commandString << ",lame"
1265     end
1266     case hash["Audio4Encoder"]
1267     when /AC3 /
1268       commandString << ",ac3"
1269     when /AAC/
1270       commandString << ",faac"
1271     when /Vorbis/
1272       commandString << ",vorbis"
1273     when /MP3/
1274       commandString << ",lame"
1275     end
1276
1277     #Audio bit rate
1278     commandString << " -B "
1279     commandString << hash["Audio1Bitrate"]
1280     if hash["Audio2Bitrate"]
1281       if hash["Audio2Encoder"] != "AC3 Passthru"
1282         commandString << "," << hash["Audio2Bitrate"]
1283       else
1284         commandString << "," << "auto"
1285       end
1286     end
1287     if hash["Audio3Bitrate"]
1288       if hash["Audio3Encoder"] != "AC3 Passthru"
1289         commandString << "," << hash["Audio3Bitrate"]
1290       else
1291         commandString << "," << "auto"
1292       end
1293     end
1294     if hash["Audio4Bitrate"]
1295       if hash["Audio4Encoder"] != "AC3 Passthru"
1296         commandString << "," << hash["Audio4Bitrate"]
1297       else
1298         commandString << "," << "auto"
1299       end
1300     end
1301
1302     #Audio sample rate
1303     commandString << " -R "
1304     commandString << hash["Audio1Samplerate"]
1305     if hash["Audio2Samplerate"]
1306       commandString << "," << hash["Audio2Samplerate"]
1307     end
1308     if hash["Audio3Samplerate"]
1309       commandString << "," << hash["Audio3Samplerate"]
1310     end
1311     if hash["Audio4Samplerate"]
1312       commandString << "," << hash["Audio4Samplerate"]
1313     end
1314     
1315     #Audio Mixdown
1316     commandString << " -6 "
1317     case hash["Audio1Mixdown"]
1318     when /Mono/
1319       commandString << "mono"
1320     when /Stereo/
1321       commandString << "stereo"
1322     when /Dolby Surround/
1323       commandString << "dpl1"
1324     when /Dolby Pro Logic II/
1325       commandString << "dpl2"
1326     when /discrete/
1327       commandString << "6ch"
1328     when /Passthru/
1329       commandString << "auto"
1330     end
1331     
1332     if hash["Audio2Mixdown"]
1333       case hash["Audio2Mixdown"]
1334       when /Mono/
1335         commandString << ",mono"
1336       when /Stereo/
1337         commandString << ",stereo"
1338       when /Dolby Surround/
1339         commandString << ",dpl1"
1340       when /Dolby Pro Logic II/
1341         commandString << ",dpl2"
1342       when /discrete/
1343         commandString << ",6ch"
1344       when /Passthru/
1345         commandString << ",auto"
1346       end
1347     end
1348     
1349     if hash["Audio3Mixdown"]
1350       case hash["Audio3Mixdown"]
1351       when /Mono/
1352         commandString << ",mono"
1353       when /Stereo/
1354         commandString << ",stereo"
1355       when /Dolby Surround/
1356         commandString << ",dpl1"
1357       when /Dolby Pro Logic II/
1358         commandString << ",dpl2"
1359       when /discrete/
1360         commandString << ",6ch"
1361       when /Passthru/
1362         commandString << ",auto"
1363       end
1364     end
1365     
1366     if hash["Audio4Mixdown"]
1367       case hash["Audio4Mixdown"]
1368       when /Mono/
1369         commandString << ",mono"
1370       when /Stereo/
1371         commandString << ",stereo"
1372       when /Dolby Surround/
1373         commandString << ",dpl1"
1374       when /Dolby Pro Logic II/
1375         commandString << ",dpl2"
1376       when /discrete/
1377         commandString << ",6ch"
1378       when /Passthru/
1379         commandString << ",auto"
1380       end
1381     end
1382     
1383     #Container
1384     commandString << " -f "
1385     case hash["FileFormat"]
1386     when /MP4/
1387       commandString << "mp4"
1388     when /AVI/
1389       commandString << "avi"
1390     when /OGM/
1391       commandString << "ogm"
1392     when /MKV/
1393       commandString << "mkv"
1394     end
1395     
1396     #iPod MP4 atom
1397     if hash["Mp4iPodCompatible"].to_i == 1
1398       commandString << " -I"
1399     end
1400     
1401     # 64-bit files
1402     if hash["Mp4LargeFile"].to_i == 1
1403       commandString << " -4"
1404     end
1405     
1406     #Cropping
1407     if !hash["PictureAutoCrop"].to_i
1408       commandString << " --crop "
1409       commandString << hash["PictureTopCrop"]
1410       commandString << ":"
1411       commandString << hash["PictureBottomCrop"]
1412       commandString << ":"
1413       commandString << hash["PictureLeftCrop"]
1414       commandString << ":"
1415       commandString << hash["PictureRightCrop"]
1416     end
1417     
1418     #Dimensions
1419     if hash["PictureWidth"].to_i != 0
1420       commandString << " -w "
1421       commandString << hash["PictureWidth"]
1422     end
1423     if hash["PictureHeight"].to_i != 0
1424       commandString << " -l "
1425       commandString << hash["PictureHeight"]
1426     end
1427     
1428     #Subtitles
1429     if hash["Subtitles"] != "None"
1430       commandString << " -s "
1431       commandString << hash["Subtitles"]
1432     end
1433     
1434     #Video Filters
1435     if hash["UsesPictureFilters"].to_i == 1
1436       
1437       case hash["PictureDeinterlace"].to_i
1438       when 1
1439         commandString << " --deinterlace=\\\"fast\\\""
1440       when 2
1441         commandString << " --deinterlace=\\\slow\\\""
1442       when 3
1443         commandString << " --deinterlace=\\\"slower\\\""
1444       when 4
1445         commandString << " --deinterlace=\\\"slowest\\\""
1446       end
1447       
1448       case hash["PictureDenoise"].to_i
1449       when 1
1450         commandString << " --denoise=\\\"weak\\\""
1451       when 2
1452         commandString << " --denoise=\\\"medium\\\""
1453       when 3
1454         commandString << " --denoise=\\\"strong\\\""
1455       end
1456       
1457       if hash["PictureDetelecine"].to_i == 1 then commandString << " --detelecine" end
1458       if hash["PictureDeblock"].to_i == 1 then commandString << " --deblock" end
1459       if hash["VFR"].to_i == 1 then commandString << " --vfr" end
1460     end
1461     
1462     #Booleans
1463     if hash["ChapterMarkers"].to_i == 1 then commandString << " -m" end
1464     if hash["PicturePAR"].to_i == 1 then commandString << " -p" end
1465     if hash["VideoGrayScale"].to_i == 1 then commandString << " -g" end
1466     if hash["VideoTwoPass"].to_i == 1 then commandString << " -2" end
1467     if hash["VideoTurboTwoPass"].to_i == 1 then commandString << " -T" end
1468     
1469       #x264 Options
1470       if hash["x264Option"] != ""
1471         commandString << " -x "
1472         commandString << hash["x264Option"]
1473       end
1474     
1475     commandString << "\\n\");"
1476     
1477     # That's it, print to screen now
1478     puts commandString
1479     puts  "\n"
1480   end
1481   
1482 end
1483
1484 # First grab the specified CLI options
1485 options = readOptions
1486
1487 # Only run if one of the useful CLI flags have been passed
1488 if options.cliraw == true || options.cliparse == true || options.api == true || options.apilist == true
1489   # This line is the ignition -- generates hashes of
1490   # presets and then displays them to the screen
1491   # with the options the user selects on the CLI. 
1492   Display.new( Presets.new.hashMasterList, options )
1493 else
1494   # Direct the user to the help
1495   puts "\n\tUsage: manicure.rb [options]"
1496   puts "\tSee help with -h or --help"
1497 end