1 /* $Id: test.c,v 1.82 2005/11/19 08:25:54 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
17 static int debug = HB_DEBUG_NONE;
18 static int update = 0;
19 static char * input = NULL;
20 static char * output = NULL;
21 static char * format = NULL;
22 static int titleindex = 1;
23 static int longest_title = 0;
24 static int subtitle_scan = 0;
25 static int subtitle_force = 0;
26 static char * native_language = NULL;
27 static int twoPass = 0;
28 static int deinterlace = 0;
29 static char * deinterlace_opt = 0;
30 static int deblock = 0;
31 static char * deblock_opt = 0;
32 static int denoise = 0;
33 static char * denoise_opt = 0;
34 static int detelecine = 0;
35 static char * detelecine_opt = 0;
36 static int grayscale = 0;
37 static int vcodec = HB_VCODEC_FFMPEG;
38 static int h264_13 = 0;
39 static int h264_30 = 0;
40 static char * audios = NULL;
41 static int audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
44 static int height = 0;
45 static int crop[4] = { -1,-1,-1,-1 };
49 static float vquality = -1.0;
50 static int vbitrate = 0;
52 static int abitrate = 0;
54 static int acodec = 0;
55 static int pixelratio = 0;
56 static int loosePixelratio = 0;
57 static int modulus = 0;
58 static int chapter_start = 0;
59 static int chapter_end = 0;
60 static int chapter_markers = 0;
61 static char * marker_file = NULL;
63 static char *x264opts = NULL;
64 static char *x264opts2 = NULL;
65 static int maxHeight = 0;
66 static int maxWidth = 0;
67 static int turbo_opts_enabled = 0;
68 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
69 static int largeFileSize = 0;
71 /* Exit cleanly on Ctrl-C */
72 static volatile int die = 0;
73 static void SigHandler( int );
76 static void ShowCommands();
77 static void ShowHelp();
78 static int ParseOptions( int argc, char ** argv );
79 static int CheckOptions( int argc, char ** argv );
80 static int HandleEvents( hb_handle_t * h );
82 /****************************************************************************
85 * When using the CLI just display using hb_log as we always did in the past
86 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
87 ****************************************************************************/
88 static void hb_cli_error_handler ( const char *errmsg )
90 fprintf( stderr, "ERROR: %s", errmsg );
93 int main( int argc, char ** argv )
99 /* Parse command line */
100 if( ParseOptions( argc, argv ) ||
101 CheckOptions( argc, argv ) )
106 /* Register our error handler */
107 hb_register_error_handler(&hb_cli_error_handler);
110 h = hb_init( debug, update );
113 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
114 hb_get_version( h ), hb_get_build( h ) );
116 /* Check for update */
119 if( ( build = hb_check_update( h, &version ) ) > -1 )
121 fprintf( stderr, "You are using an old version of "
122 "HandBrake.\nLatest is %s (build %d).\n", version,
127 fprintf( stderr, "Your version of HandBrake is up to "
135 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
136 hb_get_cpu_count( h ) > 1 ? "s" : "" );
139 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
140 cpu > 1 ? "s" : "" );
141 hb_set_cpu_count( h, cpu );
144 /* Exit ASAP on Ctrl-C */
145 signal( SIGINT, SigHandler );
147 /* Feed libhb with a DVD to scan */
148 fprintf( stderr, "Opening %s...\n", input );
152 * We need to scan for all the titles in order to find the longest
156 hb_scan( h, input, titleindex );
161 #if !defined(SYS_BEOS)
171 FD_SET( STDIN_FILENO, &fds );
172 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
179 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
181 if( buf[size] == '\n' )
188 if( size >= 256 || buf[size] == '\n' )
193 fprintf( stdout, "\nEncoding Quit by user command\n" );
197 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
219 if( input ) free( input );
220 if( output ) free( output );
221 if( format ) free( format );
222 if( audios ) free( audios );
223 if (native_language ) free (native_language );
224 if( x264opts ) free (x264opts );
225 if( x264opts2 ) free (x264opts2 );
227 fprintf( stderr, "HandBrake has exited.\n" );
232 static void ShowCommands()
234 fprintf( stdout, "\nCommands:\n" );
235 fprintf( stdout, " [h]elp Show this message\n" );
236 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
237 fprintf( stdout, " [p]ause Pause encoding\n" );
238 fprintf( stdout, " [r]esume Resume encoding\n" );
241 static void PrintTitleInfo( hb_title_t * title )
243 hb_chapter_t * chapter;
245 hb_subtitle_t * subtitle;
248 fprintf( stderr, "+ title %d:\n", title->index );
249 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
250 title->vts, title->ttn, title->cell_start, title->cell_end,
251 title->block_count );
252 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
253 title->hours, title->minutes, title->seconds );
254 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
255 title->width, title->height,
256 (float) title->aspect / HB_ASPECT_BASE,
257 (float) title->rate / title->rate_base );
258 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
259 title->crop[1], title->crop[2], title->crop[3] );
260 fprintf( stderr, " + chapters:\n" );
261 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
263 chapter = hb_list_item( title->list_chapter, i );
264 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
265 "%02d:%02d:%02d\n", chapter->index,
266 chapter->cell_start, chapter->cell_end,
267 chapter->block_count, chapter->hours, chapter->minutes,
270 fprintf( stderr, " + audio tracks:\n" );
271 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
273 audio = hb_list_item( title->list_audio, i );
274 if( ( audio->codec & HB_ACODEC_AC3 ) || ( audio->codec & HB_ACODEC_DCA) )
276 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
277 audio->lang, audio->rate, audio->bitrate );
281 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang );
284 fprintf( stderr, " + subtitle tracks:\n" );
285 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
287 subtitle = hb_list_item( title->list_subtitle, i );
288 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
293 static int HandleEvents( hb_handle_t * h )
296 hb_get_state( h, &s );
303 #define p s.param.scanning
304 case HB_STATE_SCANNING:
305 /* Show what title is currently being scanned */
306 fprintf( stderr, "Scanning title %d", p.title_cur );
308 fprintf( stderr, " of %d", p.title_count );
309 fprintf( stderr, "...\n" );
313 case HB_STATE_SCANDONE:
319 list = hb_get_titles( h );
321 if( !hb_list_count( list ) )
323 /* No valid title, stop right there */
324 fprintf( stderr, "No title found.\n" );
331 int longest_title_idx=0;
332 int longest_title_pos=-1;
333 int longest_title_time=0;
336 fprintf( stderr, "Searching for longest title...\n" );
338 for( i = 0; i < hb_list_count( list ); i++ )
340 title = hb_list_item( list, i );
341 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
342 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
343 i, title->index, title_time );
344 if( longest_title_time < title_time )
346 longest_title_time = title_time;
347 longest_title_pos = i;
348 longest_title_idx = title->index;
351 if( longest_title_pos == -1 )
353 fprintf( stderr, "No longest title found.\n" );
357 titleindex = longest_title_idx;
358 fprintf( stderr, "Found longest title, setting title to %d\n",
361 title = hb_list_item( list, longest_title_pos);
363 title = hb_list_item( list, 0 );
368 /* Scan-only mode, print infos and exit */
370 for( i = 0; i < hb_list_count( list ); i++ )
372 title = hb_list_item( list, i );
373 PrintTitleInfo( title );
379 /* Set job settings */
382 PrintTitleInfo( title );
384 if( chapter_start && chapter_end )
386 job->chapter_start = MAX( job->chapter_start,
388 job->chapter_end = MIN( job->chapter_end,
390 job->chapter_end = MAX( job->chapter_start,
394 if ( chapter_markers )
396 job->chapter_markers = chapter_markers;
398 if( marker_file != NULL )
400 hb_csv_file_t * file = hb_open_csv_file( marker_file );
401 hb_csv_cell_t * cell;
405 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
409 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
413 /* Parse the cells */
414 while( NULL != ( cell = hb_read_next_cell( file ) ) )
416 /* We have a chapter number */
417 if( cell->cell_col == 0 )
419 row = cell->cell_row;
420 chapter = atoi( cell->cell_text );
423 /* We have a chapter name */
424 if( cell->cell_col == 1 && row == cell->cell_row )
426 /* If we have a valid chapter, copy the string an terminate it */
427 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
429 hb_chapter_t * chapter_s;
431 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
432 strncpy(chapter_s->title, cell->cell_text, 1023);
433 chapter_s->title[1023] = '\0';
438 hb_dispose_cell( cell );
441 hb_close_csv_file( file );
446 if( crop[0] >= 0 && crop[1] >= 0 &&
447 crop[2] >= 0 && crop[3] >= 0 )
449 memcpy( job->crop, crop, 4 * sizeof( int ) );
452 job->deinterlace = deinterlace;
453 job->grayscale = grayscale;
456 job->pixel_ratio = 2;
459 job->modulus = modulus;
464 job->pixel_ratio = pixelratio;
466 /* Add selected filters */
467 job->filters = hb_list_init();
470 hb_filter_detelecine.settings = detelecine_opt;
471 hb_list_add( job->filters, &hb_filter_detelecine );
475 hb_filter_deinterlace.settings = deinterlace_opt;
476 hb_list_add( job->filters, &hb_filter_deinterlace );
480 hb_filter_deblock.settings = deblock_opt;
481 hb_list_add( job->filters, &hb_filter_deblock );
485 hb_log("Added denoise setting of '%s'", hb_filter_denoise.settings);
486 hb_filter_denoise.settings = denoise_opt;
487 hb_list_add( job->filters, &hb_filter_denoise );
490 if( width && height )
493 job->height = height;
498 hb_fix_aspect( job, HB_KEEP_WIDTH );
500 else if( height && !loosePixelratio)
502 job->height = height;
503 hb_fix_aspect( job, HB_KEEP_HEIGHT );
505 else if( !width && !height && !pixelratio && !loosePixelratio )
507 hb_fix_aspect( job, HB_KEEP_WIDTH );
509 else if (!width && loosePixelratio)
511 /* Default to full width when one isn't specified for loose anamorphic */
512 job->width = title->width - job->crop[2] - job->crop[3];
513 /* The height will be thrown away in hb.c but calculate it anyway */
514 hb_fix_aspect( job, HB_KEEP_WIDTH );
517 if( vquality >= 0.0 && vquality <= 1.0 )
519 job->vquality = vquality;
524 job->vquality = -1.0;
525 job->vbitrate = vbitrate;
529 job->vcodec = vcodec;
533 job->h264_level = 13;
537 job->h264_level = 30;
541 job->vrate = 27000000;
542 job->vrate_base = vrate;
551 if( strcasecmp( audios, "none" ) )
557 if( *tmp < '0' || *tmp > '9' )
559 /* Skip non numeric char */
563 job->audio_mixdowns[audio_count] = audio_mixdown;
564 job->audios[audio_count++] =
565 strtol( tmp, &tmp, 0 ) - 1;
567 job->audios[audio_count] = -1;
576 /* default to the first audio track if none has been specified */
578 job->audio_mixdowns[0] = audio_mixdown;
582 job->abitrate = abitrate;
586 job->acodec = acodec;
591 job->vbitrate = hb_calc_bitrate( job, size );
592 fprintf( stderr, "Calculated bitrate: %d kbps\n",
598 job->subtitle = sub - 1;
601 if( native_language )
603 job->native_language = strdup( native_language );
613 job->largeFileSize = 1;
616 job->file = strdup( output );
623 if( x264opts != NULL && *x264opts != '\0' )
625 fprintf( stderr, "Applying the following x264 options: %s\n",
627 job->x264opts = x264opts;
629 else /*avoids a bus error crash when options aren't specified*/
631 job->x264opts = NULL;
634 job->maxWidth = maxWidth;
636 job->maxHeight = maxHeight;
640 job->subtitle_force = subtitle_force;
648 * When subtitle scan is enabled do a fast pre-scan job
649 * which will determine which subtitles to enable, if any.
653 x264opts_tmp = job->x264opts;
655 job->x264opts = NULL;
657 job->indepth_scan = subtitle_scan;
658 fprintf( stderr, "Subtitle Scan Enabled - enabling "
659 "subtitles if found for foreign language segments\n");
660 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
661 *(job->select_subtitle) = NULL;
664 * Add the pre-scan job
668 job->x264opts = x264opts_tmp;
674 * If subtitle_scan is enabled then only turn it on
675 * for the first pass and then off again for the
678 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
680 job->select_subtitle = NULL;
684 job->indepth_scan = 0;
687 * If turbo options have been selected then append them
688 * to the x264opts now (size includes one ':' and the '\0')
690 if( turbo_opts_enabled )
692 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
695 tmp_x264opts = malloc(size * sizeof(char));
698 snprintf( tmp_x264opts, size, "%s:%s",
699 x264opts, turbo_opts );
703 * No x264opts to modify, but apply the turbo options
704 * anyway as they may be modifying defaults
706 snprintf( tmp_x264opts, size, "%s",
709 x264opts = tmp_x264opts;
711 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
714 job->x264opts = x264opts;
718 job->select_subtitle = subtitle_tmp;
722 * On the second pass we turn off subtitle scan so that we
723 * can actually encode using any subtitles that were auto
724 * selected in the first pass (using the whacky select-subtitle
725 * attribute of the job).
727 job->indepth_scan = 0;
729 job->x264opts = x264opts2;
736 * Turn on subtitle scan if requested, note that this option
737 * precludes encoding of any actual subtitles.
740 job->indepth_scan = 0;
748 #define p s.param.working
749 case HB_STATE_WORKING:
750 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
751 p.job_cur, p.job_count, 100.0 * p.progress );
754 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
755 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
756 p.hours, p.minutes, p.seconds );
762 #define p s.param.muxing
763 case HB_STATE_MUXING:
765 fprintf( stdout, "\rMuxing: %.2f %%", 100.0 * p.progress );
771 #define p s.param.workdone
772 case HB_STATE_WORKDONE:
773 /* Print error if any, then exit */
777 fprintf( stderr, "\nRip done!\n" );
779 case HB_ERROR_CANCELED:
780 fprintf( stderr, "\nRip canceled.\n" );
783 fprintf( stderr, "\nRip failed (error %x).\n",
793 /****************************************************************************
795 ****************************************************************************/
796 static volatile int64_t i_die_date = 0;
797 void SigHandler( int i_signal )
802 i_die_date = hb_get_date();
803 fprintf( stderr, "Signal %d received, terminating - do it "
804 "again in case it gets stuck\n", i_signal );
806 else if( i_die_date + 500 < hb_get_date() )
808 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
813 /****************************************************************************
815 ****************************************************************************/
816 static void ShowHelp()
821 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
823 "### General Handbrake Options------------------------------------------------\n\n"
824 " -h, --help Print help\n"
825 " -u, --update Check for updates and exit\n"
826 " -v, --verbose Be verbose\n"
827 " -C, --cpu Set CPU count (default: autodetected)\n"
830 "### Source Options-----------------------------------------------------------\n\n"
831 " -i, --input <string> Set input device\n"
832 " -t, --title <number> Select a title to encode (0 to scan only,\n"
834 " -L, --longest Select the longest title\n"
835 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
836 " 1 to 3, or \"3\" for chapter 3 only,\n"
837 " default: all chapters)\n"
840 "### Destination Options------------------------------------------------------\n\n"
841 " -o, --output <string> Set output file name\n"
842 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
843 " autodetected from file name)\n"
844 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
845 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
848 "### Picture Settings---------------------------------------------------------\n\n"
849 " -w, --width <number> Set picture width\n"
850 " -l, --height <number> Set picture height\n"
851 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
852 " -Y, --maxHeight <#> Set maximum height\n"
853 " -X, --maxWidth <#> Set maximum width\n"
854 " -s, --subtitle <number> Select subtitle (default: none)\n"
855 " -U, --subtitle-scan Scan for subtitles in an extra first pass, and choose\n"
856 " the one that's only used 10 percent of the time\n"
857 " or less. This should locate subtitles for short\n"
858 " foreign language segments. Best used in conjunction\n"
859 " with --subtitle-forced.\n"
860 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
861 " the subtitle has the forced flag set. May be used in\n"
862 " conjunction with --subtitle-scan to auto-select\n"
863 " a stream if it contains forced subtitles.\n"
864 " -N, --native-language Select subtitles with this language if it does not\n"
865 " <string> match the Audio language. Provide the language's\n"
866 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
867 " -m, --markers Add chapter markers (mp4 output format only)\n"
870 "### Video Options------------------------------------------------------------\n\n"
871 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
872 " x264,x264b13,x264b30 default: ffmpeg)\n"
873 " -q, --quality <float> Set video quality (0.0..1.0)\n"
874 " -Q, --crf Use with -q for CRF instead of CQP\n"
875 " -S, --size <MB> Set target size\n"
876 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
877 " -r, --rate Set video framerate (" );
878 for( i = 0; i < hb_video_rates_count; i++ )
880 fprintf( stderr, hb_video_rates[i].string );
881 if( i != hb_video_rates_count - 1 )
882 fprintf( stderr, "/" );
884 fprintf( stderr, ")\n"
886 " -2, --two-pass Use two-pass mode\n"
887 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
888 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
890 " <fast/slow/slower/slowest>\n"
891 " -7, --deblock Deblock video with pp7 filter\n"
892 " <QP:M> (default 0:2)\n"
893 " -8, --denoise Denoise video with hqdn3d filter\n"
894 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
896 " <weak/medium/strong>\n"
897 " -9, --detelecine Detelecine video with pullup filter\n"
898 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
899 " -g, --grayscale Grayscale encoding\n"
900 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
901 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
902 " <modulus> Takes as optional argument what number you want\n"
903 " the dimensions to divide cleanly by (default 16)\n"
909 "### Audio Options-----------------------------------------------------------\n\n"
910 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
911 " meaning passthrough, default: guessed)\n"
912 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
913 " -a, --audio <string> Select audio channel(s), separated by commas\n"
914 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
915 " tracks, default: first one, max: eight)\n"
916 " -6, --mixdown <string> Format for surround sound downmixing\n"
917 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
918 " -R, --arate Set audio samplerate (" );
919 for( i = 0; i < hb_audio_rates_count; i++ )
921 fprintf( stderr, hb_audio_rates[i].string );
922 if( i != hb_audio_rates_count - 1 )
923 fprintf( stderr, "/" );
925 fprintf( stderr, " kHz)\n"
932 "### Advanced H264 Options----------------------------------------------------\n\n"
933 " -x, --x264opts <string> Specify advanced x264 options in the\n"
934 " same style as mencoder:\n"
935 " option1=value1:option2=value2\n"
936 " -T, --turbo When using 2-pass use the turbo options\n"
937 " on the first pass to improve speed\n"
938 " (only works with x264, affects PSNR by about 0.05dB,\n"
939 " and increases first pass speed two to four times)\n");
942 /****************************************************************************
944 ****************************************************************************/
945 static int ParseOptions( int argc, char ** argv )
949 static struct option long_options[] =
951 { "help", no_argument, NULL, 'h' },
952 { "update", no_argument, NULL, 'u' },
953 { "verbose", no_argument, NULL, 'v' },
954 { "cpu", required_argument, NULL, 'C' },
956 { "format", required_argument, NULL, 'f' },
957 { "input", required_argument, NULL, 'i' },
958 { "output", required_argument, NULL, 'o' },
959 { "large-file", no_argument, NULL, '4' },
961 { "title", required_argument, NULL, 't' },
962 { "longest", no_argument, NULL, 'L' },
963 { "chapters", required_argument, NULL, 'c' },
964 { "markers", optional_argument, NULL, 'm' },
965 { "audio", required_argument, NULL, 'a' },
966 { "mixdown", required_argument, NULL, '6' },
967 { "subtitle", required_argument, NULL, 's' },
968 { "subtitle-scan", no_argument, NULL, 'U' },
969 { "subtitle-forced", no_argument, NULL, 'F' },
970 { "native-language", required_argument, NULL,'N' },
972 { "encoder", required_argument, NULL, 'e' },
973 { "aencoder", required_argument, NULL, 'E' },
974 { "two-pass", no_argument, NULL, '2' },
975 { "deinterlace", optional_argument, NULL, 'd' },
976 { "deblock", optional_argument, NULL, '7' },
977 { "denoise", optional_argument, NULL, '8' },
978 { "detelecine", optional_argument, NULL, '9' },
979 { "grayscale", no_argument, NULL, 'g' },
980 { "pixelratio", no_argument, NULL, 'p' },
981 { "loosePixelratio", optional_argument, NULL, 'P' },
982 { "width", required_argument, NULL, 'w' },
983 { "height", required_argument, NULL, 'l' },
984 { "crop", required_argument, NULL, 'n' },
986 { "vb", required_argument, NULL, 'b' },
987 { "quality", required_argument, NULL, 'q' },
988 { "size", required_argument, NULL, 'S' },
989 { "ab", required_argument, NULL, 'B' },
990 { "rate", required_argument, NULL, 'r' },
991 { "arate", required_argument, NULL, 'R' },
992 { "crf", no_argument, NULL, 'Q' },
993 { "x264opts", required_argument, NULL, 'x' },
994 { "turbo", no_argument, NULL, 'T' },
996 { "maxHeight", required_argument, NULL, 'Y' },
997 { "maxWidth", required_argument, NULL, 'X' },
1002 int option_index = 0;
1005 c = getopt_long( argc, argv,
1006 "hvuC:f:4i:o:t:Lc:ma:6:s:UFN:e:E:2d789gpP::w:l:n:b:q:S:B:r:R:Qx:TY:X:",
1007 long_options, &option_index );
1022 debug = HB_DEBUG_ALL;
1025 cpu = atoi( optarg );
1029 format = strdup( optarg );
1032 input = strdup( optarg );
1035 output = strdup( optarg );
1041 titleindex = atoi( optarg );
1049 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1051 chapter_start = start;
1054 else if( sscanf( optarg, "%d", &start ) == 1 )
1056 chapter_start = start;
1057 chapter_end = chapter_start;
1061 fprintf( stderr, "chapters: invalid syntax (%s)\n",
1068 if( optarg != NULL )
1070 marker_file = strdup( optarg );
1072 chapter_markers = 1;
1075 audios = strdup( optarg );
1078 if( !strcasecmp( optarg, "mono" ) )
1080 audio_mixdown = HB_AMIXDOWN_MONO;
1082 else if( !strcasecmp( optarg, "stereo" ) )
1084 audio_mixdown = HB_AMIXDOWN_STEREO;
1086 else if( !strcasecmp( optarg, "dpl1" ) )
1088 audio_mixdown = HB_AMIXDOWN_DOLBY;
1090 else if( !strcasecmp( optarg, "dpl2" ) )
1092 audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
1094 else if( !strcasecmp( optarg, "6ch" ) )
1096 audio_mixdown = HB_AMIXDOWN_6CH;
1100 sub = atoi( optarg );
1109 native_language = strdup( optarg );
1115 if( optarg != NULL )
1117 if (!( strcmp( optarg, "fast" ) ))
1119 deinterlace_opt = "-1";
1121 else if (!( strcmp( optarg, "slow" ) ))
1123 deinterlace_opt = "0";
1125 else if (!( strcmp( optarg, "slower" ) ))
1127 deinterlace_opt = "2:-1:1";
1129 else if (!( strcmp( optarg, "slowest" ) ))
1131 deinterlace_opt = "1:-1:1";
1135 deinterlace_opt = strdup( optarg );
1141 if( optarg != NULL )
1143 deblock_opt = strdup( optarg );
1148 if( optarg != NULL )
1150 if (!( strcmp( optarg, "weak" ) ))
1152 denoise_opt = "2:1:2:3";
1154 else if (!( strcmp( optarg, "medium" ) ))
1156 denoise_opt = "3:2:2:3";
1158 else if (!( strcmp( optarg, "strong" ) ))
1160 denoise_opt = "7:7:5:5";
1164 denoise_opt = strdup( optarg );
1170 if( optarg != NULL )
1172 detelecine_opt = strdup( optarg );
1183 loosePixelratio = 1;
1184 if( optarg != NULL )
1186 modulus = atoi( optarg );
1190 if( !strcasecmp( optarg, "ffmpeg" ) )
1192 vcodec = HB_VCODEC_FFMPEG;
1194 else if( !strcasecmp( optarg, "xvid" ) )
1196 vcodec = HB_VCODEC_XVID;
1198 else if( !strcasecmp( optarg, "x264" ) )
1200 vcodec = HB_VCODEC_X264;
1202 else if( !strcasecmp( optarg, "x264b13" ) )
1204 vcodec = HB_VCODEC_X264;
1207 else if( !strcasecmp( optarg, "x264b30" ) )
1209 vcodec = HB_VCODEC_X264;
1214 fprintf( stderr, "invalid codec (%s)\n", optarg );
1219 if( !strcasecmp( optarg, "ac3" ) )
1221 acodec = HB_ACODEC_AC3;
1223 else if( !strcasecmp( optarg, "lame" ) )
1225 acodec = HB_ACODEC_LAME;
1227 else if( !strcasecmp( optarg, "faac" ) )
1229 acodec = HB_ACODEC_FAAC;
1231 else if( !strcasecmp( optarg, "vorbis") )
1233 acodec = HB_ACODEC_VORBIS;
1237 width = atoi( optarg );
1240 height = atoi( optarg );
1245 char * tmp = optarg;
1246 for( i = 0; i < 4; i++ )
1250 crop[i] = strtol( tmp, &tmp, 0 );
1259 for( i = 0; i < hb_video_rates_count; i++ )
1261 if( !strcmp( optarg, hb_video_rates[i].string ) )
1263 vrate = hb_video_rates[i].rate;
1269 fprintf( stderr, "invalid framerate %s\n", optarg );
1277 for( i = 0; i < hb_audio_rates_count; i++ )
1279 if( !strcmp( optarg, hb_audio_rates[i].string ) )
1281 arate = hb_audio_rates[i].rate;
1287 fprintf( stderr, "invalid framerate %s\n", optarg );
1292 vbitrate = atoi( optarg );
1295 vquality = atof( optarg );
1298 size = atoi( optarg );
1301 abitrate = atoi( optarg );
1307 x264opts = strdup( optarg );
1308 x264opts2 = strdup( optarg );
1311 turbo_opts_enabled = 1;
1314 maxHeight = atoi( optarg );
1317 maxWidth = atoi (optarg );
1321 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1329 static int CheckOptions( int argc, char ** argv )
1336 if( input == NULL || *input == '\0' )
1338 fprintf( stderr, "Missing input device. Run %s --help for "
1339 "syntax.\n", argv[0] );
1344 if( titleindex > 0 )
1346 if( output == NULL || *output == '\0' )
1348 fprintf( stderr, "Missing output file name. Run %s --help "
1349 "for syntax.\n", argv[0] );
1355 char * p = strrchr( output, '.' );
1358 if( p && !strcasecmp( p, ".avi" ) )
1362 else if( p && ( !strcasecmp( p, ".mp4" ) ||
1363 !strcasecmp( p, ".m4v" ) ) )
1370 else if( p && ( !strcasecmp( p, ".ogm" ) ||
1371 !strcasecmp( p, ".ogg" ) ) )
1375 else if( p && !strcasecmp(p, ".mkv" ) )
1381 fprintf( stderr, "Output format couldn't be guessed "
1382 "from file name, using default.\n" );
1386 else if( !strcasecmp( format, "avi" ) )
1390 else if( !strcasecmp( format, "mp4" ) )
1397 else if( !strcasecmp( format, "ogm" ) ||
1398 !strcasecmp( format, "ogg" ) )
1402 else if( !strcasecmp( format, "mkv" ) )
1408 fprintf( stderr, "Invalid output format (%s). Possible "
1409 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
1415 if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
1417 acodec = HB_ACODEC_FAAC;
1419 else if( mux == HB_MUX_AVI )
1421 acodec = HB_ACODEC_LAME;
1423 else if( mux == HB_MUX_OGM )
1425 acodec = HB_ACODEC_VORBIS;
1427 else if( mux == HB_MUX_MKV )
1429 acodec = HB_ACODEC_AC3;