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.fr/>.
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 hb_list_t * audios = NULL;
41 static hb_audio_config_t * audio = NULL;
42 static int num_audio_tracks = 0;
43 static char * mixdowns = NULL;
44 static char * dynamic_range_compression = NULL;
45 static char * arates = NULL;
46 static char * abitrates = NULL;
47 static char * acodecs = NULL;
48 static int default_acodec = HB_ACODEC_FAAC;
49 static int default_arate = 44100;
50 static int default_abitrate = 128;
53 static int height = 0;
54 static int crop[4] = { -1,-1,-1,-1 };
57 static float vquality = -1.0;
58 static int vbitrate = 0;
61 static int pixelratio = 0;
62 static int loosePixelratio = 0;
63 static int modulus = 0;
64 static int chapter_start = 0;
65 static int chapter_end = 0;
66 static int chapter_markers = 0;
67 static char * marker_file = NULL;
69 static char *x264opts = NULL;
70 static char *x264opts2 = NULL;
71 static int maxHeight = 0;
72 static int maxWidth = 0;
73 static int turbo_opts_enabled = 0;
74 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
75 static int largeFileSize = 0;
76 static int preset = 0;
77 static char * preset_name = 0;
79 static int mp4_optimize = 0;
80 static int ipod_atom = 0;
82 /* Exit cleanly on Ctrl-C */
83 static volatile int die = 0;
84 static void SigHandler( int );
87 static void ShowCommands();
88 static void ShowHelp();
89 static void ShowPresets();
91 static int ParseOptions( int argc, char ** argv );
92 static int CheckOptions( int argc, char ** argv );
93 static int HandleEvents( hb_handle_t * h );
95 static int get_acodec_for_string( char *codec );
96 static int is_sample_rate_valid(int rate);
98 /* Only print the "Muxing..." message once */
99 static int show_mux_warning = 1;
101 /****************************************************************************
104 * When using the CLI just display using hb_log as we always did in the past
105 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
106 ****************************************************************************/
107 static void hb_cli_error_handler ( const char *errmsg )
109 fprintf( stderr, "ERROR: %s", errmsg );
112 int main( int argc, char ** argv )
118 audios = hb_list_init();
120 /* Parse command line */
121 if( ParseOptions( argc, argv ) ||
122 CheckOptions( argc, argv ) )
127 /* Register our error handler */
128 hb_register_error_handler(&hb_cli_error_handler);
131 h = hb_init( debug, update );
134 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
135 hb_get_version( h ), hb_get_build( h ) );
137 /* Check for update */
140 if( ( build = hb_check_update( h, &version ) ) > -1 )
142 fprintf( stderr, "You are using an old version of "
143 "HandBrake.\nLatest is %s (build %d).\n", version,
148 fprintf( stderr, "Your version of HandBrake is up to "
156 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
157 hb_get_cpu_count( h ) > 1 ? "s" : "" );
160 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
161 cpu > 1 ? "s" : "" );
162 hb_set_cpu_count( h, cpu );
165 /* Exit ASAP on Ctrl-C */
166 signal( SIGINT, SigHandler );
168 /* Feed libhb with a DVD to scan */
169 fprintf( stderr, "Opening %s...\n", input );
173 * We need to scan for all the titles in order to find the longest
177 hb_scan( h, input, titleindex );
182 #if !defined(SYS_BEOS)
192 FD_SET( STDIN_FILENO, &fds );
193 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
200 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
202 if( buf[size] == '\n' )
209 if( size >= 256 || buf[size] == '\n' )
214 fprintf( stdout, "\nEncoding Quit by user command\n" );
218 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
240 if( input ) free( input );
241 if( output ) free( output );
242 if( format ) free( format );
245 while( ( audio = hb_list_item( audios, 0 ) ) )
247 hb_list_rem( audios, audio );
250 hb_list_close( &audios );
252 if( mixdowns ) free( mixdowns );
253 if( dynamic_range_compression ) free( dynamic_range_compression );
254 if( arates ) free( arates );
255 if( abitrates ) free( abitrates );
256 if( acodecs ) free( acodecs );
257 if (native_language ) free (native_language );
258 if( x264opts ) free (x264opts );
259 if( x264opts2 ) free (x264opts2 );
260 if (preset_name) free (preset_name);
262 fprintf( stderr, "HandBrake has exited.\n" );
267 static void ShowCommands()
269 fprintf( stdout, "\nCommands:\n" );
270 fprintf( stdout, " [h]elp Show this message\n" );
271 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
272 fprintf( stdout, " [p]ause Pause encoding\n" );
273 fprintf( stdout, " [r]esume Resume encoding\n" );
276 static void PrintTitleInfo( hb_title_t * title )
278 hb_chapter_t * chapter;
279 hb_audio_config_t * audio;
280 hb_subtitle_t * subtitle;
283 fprintf( stderr, "+ title %d:\n", title->index );
284 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
285 title->vts, title->ttn, title->cell_start, title->cell_end,
286 title->block_count );
287 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
288 title->hours, title->minutes, title->seconds );
289 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
290 title->width, title->height,
291 (float) title->aspect / HB_ASPECT_BASE,
292 (float) title->rate / title->rate_base );
293 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
294 title->crop[1], title->crop[2], title->crop[3] );
295 fprintf( stderr, " + chapters:\n" );
296 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
298 chapter = hb_list_item( title->list_chapter, i );
299 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
300 "%02d:%02d:%02d\n", chapter->index,
301 chapter->cell_start, chapter->cell_end,
302 chapter->block_count, chapter->hours, chapter->minutes,
305 fprintf( stderr, " + audio tracks:\n" );
306 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
308 audio = hb_list_audio_config_item( title->list_audio, i );
309 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
311 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
312 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
316 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
319 fprintf( stderr, " + subtitle tracks:\n" );
320 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
322 subtitle = hb_list_item( title->list_subtitle, i );
323 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
327 if(title->detected_interlacing)
329 /* Interlacing was found in half or more of the preview frames */
330 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
335 static int HandleEvents( hb_handle_t * h )
338 hb_get_state( h, &s );
345 #define p s.param.scanning
346 case HB_STATE_SCANNING:
347 /* Show what title is currently being scanned */
348 fprintf( stderr, "Scanning title %d", p.title_cur );
350 fprintf( stderr, " of %d", p.title_count );
351 fprintf( stderr, "...\n" );
355 case HB_STATE_SCANDONE:
362 /* Audio argument string parsing variables */
366 int mixdown = HB_AMIXDOWN_DOLBYPLII;
368 /* Audio argument string parsing variables */
370 list = hb_get_titles( h );
372 if( !hb_list_count( list ) )
374 /* No valid title, stop right there */
375 fprintf( stderr, "No title found.\n" );
382 int longest_title_idx=0;
383 int longest_title_pos=-1;
384 int longest_title_time=0;
387 fprintf( stderr, "Searching for longest title...\n" );
389 for( i = 0; i < hb_list_count( list ); i++ )
391 title = hb_list_item( list, i );
392 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
393 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
394 i, title->index, title_time );
395 if( longest_title_time < title_time )
397 longest_title_time = title_time;
398 longest_title_pos = i;
399 longest_title_idx = title->index;
402 if( longest_title_pos == -1 )
404 fprintf( stderr, "No longest title found.\n" );
408 titleindex = longest_title_idx;
409 fprintf( stderr, "Found longest title, setting title to %d\n",
412 title = hb_list_item( list, longest_title_pos);
414 title = hb_list_item( list, 0 );
419 /* Scan-only mode, print infos and exit */
421 for( i = 0; i < hb_list_count( list ); i++ )
423 title = hb_list_item( list, i );
424 PrintTitleInfo( title );
430 /* Set job settings */
433 PrintTitleInfo( title );
435 if( chapter_start && chapter_end )
437 job->chapter_start = MAX( job->chapter_start,
439 job->chapter_end = MIN( job->chapter_end,
441 job->chapter_end = MAX( job->chapter_start,
447 fprintf( stderr, "+ Using preset: %s", preset_name);
449 if (!strcmp(preset_name, "Animation"))
452 vcodec = HB_VCODEC_X264;
453 job->vbitrate = 1000;
454 default_abitrate = 160;
455 default_arate = 48000;
456 default_acodec = HB_ACODEC_FAAC;
457 x264opts = strdup("ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2");
459 deinterlace_opt = "0";
460 job->chapter_markers = 1;
463 turbo_opts_enabled = 1;
466 if (!strcmp(preset_name, "AppleTV"))
470 acodecs = strdup("faac,ac3");
474 job->largeFileSize = 1;
475 vcodec = HB_VCODEC_X264;
476 job->vbitrate = 2500;
477 default_abitrate = 160;
478 default_arate = 48000;
479 default_acodec = HB_ACODEC_FAAC;
480 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
481 job->chapter_markers = 1;
485 if (!strcmp(preset_name, "Bedlam"))
488 vcodec = HB_VCODEC_X264;
489 job->vbitrate = 1800;
490 default_acodec = HB_ACODEC_AC3;
491 x264opts = strdup("ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1");
492 job->chapter_markers = 1;
495 turbo_opts_enabled = 1;
498 if (!strcmp(preset_name, "Blind"))
502 default_abitrate = 128;
503 default_arate = 48000;
504 default_acodec = HB_ACODEC_FAAC;
506 job->chapter_markers = 1;
509 if (!strcmp(preset_name, "Broke"))
512 vcodec = HB_VCODEC_X264;
514 default_abitrate = 128;
515 default_arate = 48000;
516 default_acodec = HB_ACODEC_FAAC;
518 x264opts = strdup("ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip");
519 job->chapter_markers = 1;
521 turbo_opts_enabled = 1;
524 if (!strcmp(preset_name, "Classic"))
527 job->vbitrate = 1000;
528 default_abitrate = 160;
529 default_arate = 48000;
530 default_acodec = HB_ACODEC_FAAC;
533 if (!strcmp(preset_name, "Constant Quality Rate"))
536 vcodec = HB_VCODEC_X264;
537 job->vquality = 0.64709997177124023;
539 default_acodec = HB_ACODEC_AC3;
540 x264opts = strdup("ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh");
541 job->chapter_markers = 1;
545 if (!strcmp(preset_name, "Deux Six Quatre"))
548 vcodec = HB_VCODEC_X264;
549 job->vbitrate = 1600;
550 default_acodec = HB_ACODEC_AC3;
551 x264opts = strdup("ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip");
552 job->chapter_markers = 1;
555 turbo_opts_enabled = 1;
558 if (!strcmp(preset_name, "Film"))
561 vcodec = HB_VCODEC_X264;
562 job->vbitrate = 1800;
563 default_acodec = HB_ACODEC_AC3;
564 x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip");
565 job->chapter_markers = 1;
568 turbo_opts_enabled = 1;
571 if (!strcmp(preset_name, "iPhone / iPod Touch"))
575 vcodec = HB_VCODEC_X264;
577 default_abitrate = 128;
578 default_arate = 48000;
579 default_acodec = HB_ACODEC_FAAC;
581 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
582 job->chapter_markers = 1;
585 if (!strcmp(preset_name, "iPod High-Rez"))
589 vcodec = HB_VCODEC_X264;
590 job->vbitrate = 1500;
591 default_abitrate = 160;
592 default_arate = 48000;
593 default_acodec = HB_ACODEC_FAAC;
595 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
596 job->chapter_markers = 1;
599 if (!strcmp(preset_name, "iPod Low-Rez"))
603 vcodec = HB_VCODEC_X264;
605 default_abitrate = 160;
606 default_arate = 48000;
607 default_acodec = HB_ACODEC_FAAC;
609 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
610 job->chapter_markers = 1;
613 if (!strcmp(preset_name, "Normal"))
616 vcodec = HB_VCODEC_X264;
617 job->vbitrate = 1500;
618 default_abitrate = 160;
619 default_arate = 48000;
620 default_acodec = HB_ACODEC_FAAC;
621 x264opts = strdup("ref=2:bframes=2:subme=5:me=umh");
622 job->chapter_markers = 1;
625 turbo_opts_enabled = 1;
628 if (!strcmp(preset_name, "PS3"))
631 vcodec = HB_VCODEC_X264;
632 job->vbitrate = 2500;
633 default_abitrate = 160;
634 default_arate = 48000;
635 default_acodec = HB_ACODEC_FAAC;
636 x264opts = strdup("level=41:subme=5:me=umh");
640 if (!strcmp(preset_name, "PSP"))
643 job->vbitrate = 1024;
644 default_abitrate = 128;
645 default_arate = 48000;
646 default_acodec = HB_ACODEC_FAAC;
649 job->chapter_markers = 1;
652 if (!strcmp(preset_name, "QuickTime"))
655 vcodec = HB_VCODEC_X264;
656 job->vbitrate = 2000;
657 default_abitrate = 160;
658 default_arate = 48000;
659 default_acodec = HB_ACODEC_FAAC;
660 x264opts = strdup("ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip");
661 job->chapter_markers = 1;
664 turbo_opts_enabled = 1;
667 if (!strcmp(preset_name, "Television"))
670 vcodec = HB_VCODEC_X264;
671 job->vbitrate = 1300;
672 default_abitrate = 160;
673 default_arate = 48000;
674 default_acodec = HB_ACODEC_FAAC;
675 x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip");
677 deinterlace_opt = "0";
679 denoise_opt = "2:1:2:3";
680 job->chapter_markers = 1;
682 turbo_opts_enabled = 1;
685 if (!strcmp(preset_name, "Xbox 360"))
688 vcodec = HB_VCODEC_X264;
689 job->vbitrate = 2000;
690 default_abitrate = 160;
691 default_arate = 48000;
692 default_acodec = HB_ACODEC_FAAC;
693 x264opts = strdup("level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1");
698 if ( chapter_markers )
700 job->chapter_markers = chapter_markers;
702 if( marker_file != NULL )
704 hb_csv_file_t * file = hb_open_csv_file( marker_file );
705 hb_csv_cell_t * cell;
709 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
713 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
717 /* Parse the cells */
718 while( NULL != ( cell = hb_read_next_cell( file ) ) )
720 /* We have a chapter number */
721 if( cell->cell_col == 0 )
723 row = cell->cell_row;
724 chapter = atoi( cell->cell_text );
727 /* We have a chapter name */
728 if( cell->cell_col == 1 && row == cell->cell_row )
730 /* If we have a valid chapter, copy the string an terminate it */
731 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
733 hb_chapter_t * chapter_s;
735 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
736 strncpy(chapter_s->title, cell->cell_text, 1023);
737 chapter_s->title[1023] = '\0';
742 hb_dispose_cell( cell );
745 hb_close_csv_file( file );
752 int number_of_chapters = hb_list_count(job->title->list_chapter);
755 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
757 hb_chapter_t * chapter_s;
758 chapter_s = hb_list_item( job->title->list_chapter, chapter);
759 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
760 chapter_s->title[1023] = '\0';
765 if( crop[0] >= 0 && crop[1] >= 0 &&
766 crop[2] >= 0 && crop[3] >= 0 )
768 memcpy( job->crop, crop, 4 * sizeof( int ) );
771 job->deinterlace = deinterlace;
772 job->grayscale = grayscale;
775 job->pixel_ratio = 2;
778 job->modulus = modulus;
783 job->pixel_ratio = pixelratio;
792 /* Add selected filters */
793 job->filters = hb_list_init();
796 hb_filter_detelecine.settings = detelecine_opt;
797 hb_list_add( job->filters, &hb_filter_detelecine );
801 hb_filter_deinterlace.settings = deinterlace_opt;
802 hb_list_add( job->filters, &hb_filter_deinterlace );
806 hb_filter_deblock.settings = deblock_opt;
807 hb_list_add( job->filters, &hb_filter_deblock );
811 hb_filter_denoise.settings = denoise_opt;
812 hb_list_add( job->filters, &hb_filter_denoise );
815 if( width && height )
818 job->height = height;
823 hb_fix_aspect( job, HB_KEEP_WIDTH );
825 else if( height && !loosePixelratio)
827 job->height = height;
828 hb_fix_aspect( job, HB_KEEP_HEIGHT );
830 else if( !width && !height && !pixelratio && !loosePixelratio )
832 hb_fix_aspect( job, HB_KEEP_WIDTH );
834 else if (!width && loosePixelratio)
836 /* Default to full width when one isn't specified for loose anamorphic */
837 job->width = title->width - job->crop[2] - job->crop[3];
838 /* The height will be thrown away in hb.c but calculate it anyway */
839 hb_fix_aspect( job, HB_KEEP_WIDTH );
842 if( vquality >= 0.0 && vquality <= 1.0 )
844 job->vquality = vquality;
849 job->vquality = -1.0;
850 job->vbitrate = vbitrate;
854 job->vcodec = vcodec;
858 job->h264_level = 13;
862 job->h264_level = 30;
866 job->vrate = 27000000;
867 job->vrate_base = vrate;
870 /* Parse audio tracks */
871 if( hb_list_count(audios) == 0 )
873 /* Create a new audio track with default settings */
874 audio = calloc(1, sizeof(*audio));
875 hb_audio_config_init(audio);
876 /* Add it to our audios */
877 hb_list_add(audios, audio);
880 num_audio_tracks = hb_list_count(audios);
881 for (i = 0; i < num_audio_tracks; i++)
883 audio = hb_list_item(audios, 0);
884 if( (audio == NULL) || (audio->in.track == -1) ||
885 (audio->out.track == -1) || (audio->out.codec == 0) )
891 hb_audio_add( job, audio );
893 hb_list_rem(audios, audio);
903 char * token = strtok(acodecs, ",");
906 while ( token != NULL )
908 if ((acodec = get_acodec_for_string(token)) == -1)
910 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
911 acodec = default_acodec;
913 if( i < num_audio_tracks )
915 audio = hb_list_audio_config_item(job->list_audio, i);
916 audio->out.codec = acodec;
918 if( i >= num_audio_tracks )
920 int last_track = hb_list_count(job->list_audio);
921 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
923 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, last_track - 1 );
924 audio = calloc(1, sizeof(*audio));
925 hb_audio_config_init(audio);
926 audio->in.track = last_audio->in.track;
927 audio->out.track = num_audio_tracks++;
928 audio->out.codec = acodec;
929 hb_audio_add(job, audio);
932 token = strtok(NULL, ",");
936 if( i < num_audio_tracks )
938 /* We have fewer inputs than audio tracks, use the default codec for
939 * this container for the remaining tracks. Unless we only have one input
940 * then use that codec instead.
943 acodec = default_acodec;
944 for ( ; i < num_audio_tracks; i++)
946 audio = hb_list_audio_config_item(job->list_audio, i);
947 audio->out.codec = acodec;
956 char * token = strtok(arates, ",");
959 while ( token != NULL )
962 audio = hb_list_audio_config_item(job->list_audio, i);
963 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
965 /* Would probably be better to have this handled in libhb. */
966 audio->out.samplerate = audio->in.samplerate;
970 if (!is_sample_rate_valid(arate))
972 fprintf(stderr, "Invalid sample rate %d, using default\n", arate);
973 arate = default_arate;
975 audio->out.samplerate = arate;
977 if( (++i) >= num_audio_tracks )
978 break; /* We have more inputs than audio tracks, oops */
979 token = strtok(NULL, ",");
982 if (i < num_audio_tracks)
984 /* We have fewer inputs than audio tracks, use default sample rate.
985 * Unless we only have one input, then use that for all tracks.
988 arate = default_arate;
989 for ( ; i < num_audio_tracks; i++)
991 audio = hb_list_audio_config_item(job->list_audio, i);
992 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
994 audio->out.samplerate = audio->in.samplerate;
997 audio->out.samplerate = arate;
1006 char * token = strtok(abitrates, ",");
1009 while ( token != NULL )
1011 abitrate = atoi(token);
1012 audio = hb_list_audio_config_item(job->list_audio, i);
1013 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1015 audio->out.bitrate = audio->in.bitrate; /* See note above about arate. */
1019 audio->out.bitrate = abitrate;
1021 if( (++i) >= num_audio_tracks )
1022 break; /* We have more inputs than audio tracks, oops */
1023 token = strtok(NULL, ",");
1026 if (i < num_audio_tracks)
1028 /* We have fewer inputs than audio tracks, use the default bitrate
1029 * for the remaining tracks. Unless we only have one input, then use
1030 * that for all tracks.
1033 abitrate = default_abitrate;
1034 for (; i < num_audio_tracks; i++)
1036 audio = hb_list_audio_config_item(job->list_audio, i);
1037 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1039 audio->out.bitrate = audio->in.bitrate;
1042 audio->out.bitrate = abitrate;
1049 if ( dynamic_range_compression )
1051 char * token = strtok(dynamic_range_compression, ",");
1053 token = dynamic_range_compression;
1054 while ( token != NULL )
1056 d_r_c = atof(token);
1057 audio = hb_list_audio_config_item(job->list_audio, i);
1058 audio->out.dynamic_range_compression = d_r_c;
1059 if( (++i) >= num_audio_tracks )
1060 break; /* We have more inputs than audio tracks, oops */
1061 token = strtok(NULL, ",");
1064 if (i < num_audio_tracks)
1066 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1067 * tracks. Unless we only have one input, then use the same DRC for all
1072 for (; i < num_audio_tracks; i++)
1074 audio = hb_list_audio_config_item(job->list_audio, i);
1075 audio->out.dynamic_range_compression = d_r_c;
1084 char * token = strtok(mixdowns, ",");
1087 while ( token != NULL )
1089 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1090 audio = hb_list_audio_config_item(job->list_audio, i);
1091 audio->out.mixdown = mixdown;
1092 if( (++i) >= num_audio_tracks )
1093 break; /* We have more inputs than audio tracks, oops */
1094 token = strtok(NULL, ",");
1097 if (i < num_audio_tracks)
1099 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1100 * we only have one input, then use that.
1103 mixdown = HB_AMIXDOWN_DOLBYPLII;
1104 for (; i < num_audio_tracks; i++)
1106 audio = hb_list_audio_config_item(job->list_audio, i);
1107 audio->out.mixdown = mixdown;
1114 job->vbitrate = hb_calc_bitrate( job, size );
1115 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1121 job->subtitle = sub - 1;
1124 if( native_language )
1126 job->native_language = strdup( native_language );
1134 if ( largeFileSize )
1136 job->largeFileSize = 1;
1140 job->mp4_optimize = 1;
1147 job->file = strdup( output );
1154 if( x264opts != NULL && *x264opts != '\0' )
1156 job->x264opts = x264opts;
1158 else /*avoids a bus error crash when options aren't specified*/
1160 job->x264opts = NULL;
1163 job->maxWidth = maxWidth;
1165 job->maxHeight = maxHeight;
1167 if( subtitle_force )
1169 job->subtitle_force = subtitle_force;
1177 * When subtitle scan is enabled do a fast pre-scan job
1178 * which will determine which subtitles to enable, if any.
1182 x264opts_tmp = job->x264opts;
1184 job->x264opts = NULL;
1186 job->indepth_scan = subtitle_scan;
1187 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1188 "subtitles if found for foreign language segments\n");
1189 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1190 *(job->select_subtitle) = NULL;
1193 * Add the pre-scan job
1197 job->x264opts = x264opts_tmp;
1203 * If subtitle_scan is enabled then only turn it on
1204 * for the first pass and then off again for the
1207 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1209 job->select_subtitle = NULL;
1213 job->indepth_scan = 0;
1217 x264opts2 = strdup(x264opts);
1221 * If turbo options have been selected then append them
1222 * to the x264opts now (size includes one ':' and the '\0')
1224 if( turbo_opts_enabled )
1226 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1229 tmp_x264opts = malloc(size * sizeof(char));
1232 snprintf( tmp_x264opts, size, "%s:%s",
1233 x264opts, turbo_opts );
1237 * No x264opts to modify, but apply the turbo options
1238 * anyway as they may be modifying defaults
1240 snprintf( tmp_x264opts, size, "%s",
1243 x264opts = tmp_x264opts;
1245 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1248 job->x264opts = x264opts;
1252 job->select_subtitle = subtitle_tmp;
1256 * On the second pass we turn off subtitle scan so that we
1257 * can actually encode using any subtitles that were auto
1258 * selected in the first pass (using the whacky select-subtitle
1259 * attribute of the job).
1261 job->indepth_scan = 0;
1263 job->x264opts = x264opts2;
1270 * Turn on subtitle scan if requested, note that this option
1271 * precludes encoding of any actual subtitles.
1274 job->indepth_scan = 0;
1282 #define p s.param.working
1283 case HB_STATE_WORKING:
1284 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1285 p.job_cur, p.job_count, 100.0 * p.progress );
1286 if( p.seconds > -1 )
1288 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1289 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1290 p.hours, p.minutes, p.seconds );
1296 #define p s.param.muxing
1297 case HB_STATE_MUXING:
1299 if (show_mux_warning)
1301 fprintf( stdout, "\rMuxing: this may take awhile..." );
1303 show_mux_warning = 0;
1309 #define p s.param.workdone
1310 case HB_STATE_WORKDONE:
1311 /* Print error if any, then exit */
1315 fprintf( stderr, "\nRip done!\n" );
1317 case HB_ERROR_CANCELED:
1318 fprintf( stderr, "\nRip canceled.\n" );
1321 fprintf( stderr, "\nRip failed (error %x).\n",
1331 /****************************************************************************
1333 ****************************************************************************/
1334 static volatile int64_t i_die_date = 0;
1335 void SigHandler( int i_signal )
1340 i_die_date = hb_get_date();
1341 fprintf( stderr, "Signal %d received, terminating - do it "
1342 "again in case it gets stuck\n", i_signal );
1344 else if( i_die_date + 500 < hb_get_date() )
1346 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1351 /****************************************************************************
1353 ****************************************************************************/
1354 static void ShowHelp()
1359 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1361 "### General Handbrake Options------------------------------------------------\n\n"
1362 " -h, --help Print help\n"
1363 " -u, --update Check for updates and exit\n"
1364 " -v, --verbose Be verbose\n"
1365 " -C, --cpu Set CPU count (default: autodetected)\n"
1366 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1367 " if the preset name has spaces, surround it with\n"
1368 " double quotation marks\n"
1369 " -z, --preset-list See a list of available built-in presets\n"
1372 "### Source Options-----------------------------------------------------------\n\n"
1373 " -i, --input <string> Set input device\n"
1374 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1376 " -L, --longest Select the longest title\n"
1377 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1378 " 1 to 3, or \"3\" for chapter 3 only,\n"
1379 " default: all chapters)\n"
1382 "### Destination Options------------------------------------------------------\n\n"
1383 " -o, --output <string> Set output file name\n"
1384 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1385 " autodetected from file name)\n"
1386 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1387 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1388 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1389 " -I, --ipod-atom Mark mp4 files so iPods will accept them\n"
1392 "### Picture Settings---------------------------------------------------------\n\n"
1393 " -w, --width <number> Set picture width\n"
1394 " -l, --height <number> Set picture height\n"
1395 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1396 " -Y, --maxHeight <#> Set maximum height\n"
1397 " -X, --maxWidth <#> Set maximum width\n"
1398 " -s, --subtitle <number> Select subtitle (default: none)\n"
1399 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1400 " the one that's only used 10 percent of the time\n"
1401 " or less. This should locate subtitles for short\n"
1402 " foreign language segments. Best used in conjunction\n"
1403 " with --subtitle-forced.\n"
1404 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1405 " the subtitle has the forced flag set. May be used in\n"
1406 " conjunction with --subtitle-scan to auto-select\n"
1407 " a stream if it contains forced subtitles.\n"
1408 " -N, --native-language Select subtitles with this language if it does not\n"
1409 " <string> match the Audio language. Provide the language's\n"
1410 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1411 " -m, --markers Add chapter markers (mp4 output format only)\n"
1414 "### Video Options------------------------------------------------------------\n\n"
1415 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1416 " x264,theora default: ffmpeg)\n"
1417 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1418 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1419 " -S, --size <MB> Set target size\n"
1420 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1421 " -r, --rate Set video framerate (" );
1422 for( i = 0; i < hb_video_rates_count; i++ )
1424 fprintf( stderr, hb_video_rates[i].string );
1425 if( i != hb_video_rates_count - 1 )
1426 fprintf( stderr, "/" );
1428 fprintf( stderr, ")\n"
1430 " -2, --two-pass Use two-pass mode\n"
1431 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1432 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1434 " <fast/slow/slower>\n"
1435 " -7, --deblock Deblock video with pp7 filter\n"
1436 " <QP:M> (default 0:2)\n"
1437 " -8, --denoise Denoise video with hqdn3d filter\n"
1438 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1440 " <weak/medium/strong>\n"
1441 " -9, --detelecine Detelecine video with pullup filter\n"
1442 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1443 " -g, --grayscale Grayscale encoding\n"
1444 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1445 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1446 " <modulus> Takes as optional argument what number you want\n"
1447 " the dimensions to divide cleanly by (default 16)\n"
1453 "### Audio Options-----------------------------------------------------------\n\n"
1454 " -a, --audio <string> Select audio channel(s), separated by commas\n"
1455 " More than one output track can be used for one\n"
1457 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1458 " tracks, default: first one)\n"
1459 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1460 " ac3 meaning passthrough\n"
1461 " Seperated by commas for more than one audio track.\n"
1462 " (default: guessed)\n"
1463 " -B, --ab <kb/s> Set audio bitrate(s) (default: 128)\n"
1464 " Seperated by commas for more than one audio track.\n"
1465 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1466 " Seperated by commas for more than one audio track.\n"
1467 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1468 " -R, --arate Set audio samplerate(s) (" );
1469 for( i = 0; i < hb_audio_rates_count; i++ )
1471 fprintf( stderr, hb_audio_rates[i].string );
1472 if( i != hb_audio_rates_count - 1 )
1473 fprintf( stderr, "/" );
1475 fprintf( stderr, " kHz)\n"
1476 " Seperated by commas for more than one audio track.\n"
1477 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1478 " making soft sounds louder. Range is 1.0 to 4.0\n"
1479 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1480 " Seperated by commas for more than one audio track.\n"
1486 "### Advanced Options---------------------------------------------------------\n\n"
1487 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1488 " same style as mencoder:\n"
1489 " option1=value1:option2=value2\n"
1490 " -T, --turbo When using 2-pass use the turbo options\n"
1491 " on the first pass to improve speed\n"
1492 " (only works with x264, affects PSNR by about 0.05dB,\n"
1493 " and increases first pass speed two to four times)\n"
1494 " -V, --vfr Perform variable framerate detelecine on NTSC video\n"
1498 /****************************************************************************
1500 ****************************************************************************/
1501 static void ShowPresets()
1503 printf("\n+ Animation: -e x264 -b 1000 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" -m -p -2 -T -x ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2\n");
1505 printf("\n+ AppleTV: -e x264 -b 2500 -B 160 -R 48 -E faac,ac3 -f mp4 -4 -m -p -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0\n");
1507 printf("\n+ Bedlam: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1\n");
1509 printf("\n+ Blind: -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1511 printf("\n+ Broke: -e x264 -S 695 -B 128 -R 48 -E faac -f mp4 -w 640 -m -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1513 printf("\n+ Classic: -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1515 printf("\n+ Constant Quality Rate: -e x264 -q 0.64709997177124023 -E ac3 -f mkv -m -p -x ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh\n");
1517 printf("\n+ Deux Six Quatre: -e x264 -b 1600 -E ac3 -f mkv -m -p -2 -T -x ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1519 printf("\n+ Film: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip\n");
1521 printf("\n+ iPhone / iPod Touch: -e x264 -b 960 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1\n");
1523 printf("\n+ iPod High-Rez: -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -I -w 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1525 printf("\n+ iPod Low-Rez: -e x264 -b 700 -B 160 -R 48 -E faac -f mp4 -I -w 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1527 printf("\n+ Normal: -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=2:bframes=2:subme=5:me=umh\n");
1529 printf("\n+ PS3: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1531 printf("\n+ PSP: -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1533 printf("\n+ QuickTime: -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip\n");
1535 printf("\n+ Television: -e x264 -b 1300 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" --denoise=\"weak\" -m -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip\n");
1537 printf("\n+ Xbox 360: -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -p -x level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1\n");
1540 /****************************************************************************
1542 ****************************************************************************/
1543 static int ParseOptions( int argc, char ** argv )
1547 static struct option long_options[] =
1549 { "help", no_argument, NULL, 'h' },
1550 { "update", no_argument, NULL, 'u' },
1551 { "verbose", no_argument, NULL, 'v' },
1552 { "cpu", required_argument, NULL, 'C' },
1554 { "format", required_argument, NULL, 'f' },
1555 { "input", required_argument, NULL, 'i' },
1556 { "output", required_argument, NULL, 'o' },
1557 { "large-file", no_argument, NULL, '4' },
1558 { "optimize", no_argument, NULL, 'O' },
1559 { "ipod-atom", no_argument, NULL, 'I' },
1561 { "title", required_argument, NULL, 't' },
1562 { "longest", no_argument, NULL, 'L' },
1563 { "chapters", required_argument, NULL, 'c' },
1564 { "markers", optional_argument, NULL, 'm' },
1565 { "audio", required_argument, NULL, 'a' },
1566 { "mixdown", required_argument, NULL, '6' },
1567 { "drc", required_argument, NULL, 'D' },
1568 { "subtitle", required_argument, NULL, 's' },
1569 { "subtitle-scan", no_argument, NULL, 'U' },
1570 { "subtitle-forced", no_argument, NULL, 'F' },
1571 { "native-language", required_argument, NULL,'N' },
1573 { "encoder", required_argument, NULL, 'e' },
1574 { "aencoder", required_argument, NULL, 'E' },
1575 { "two-pass", no_argument, NULL, '2' },
1576 { "deinterlace", optional_argument, NULL, 'd' },
1577 { "deblock", optional_argument, NULL, '7' },
1578 { "denoise", optional_argument, NULL, '8' },
1579 { "detelecine", optional_argument, NULL, '9' },
1580 { "grayscale", no_argument, NULL, 'g' },
1581 { "pixelratio", no_argument, NULL, 'p' },
1582 { "loosePixelratio", optional_argument, NULL, 'P' },
1583 { "width", required_argument, NULL, 'w' },
1584 { "height", required_argument, NULL, 'l' },
1585 { "crop", required_argument, NULL, 'n' },
1587 { "vb", required_argument, NULL, 'b' },
1588 { "quality", required_argument, NULL, 'q' },
1589 { "size", required_argument, NULL, 'S' },
1590 { "ab", required_argument, NULL, 'B' },
1591 { "rate", required_argument, NULL, 'r' },
1592 { "arate", required_argument, NULL, 'R' },
1593 { "cqp", no_argument, NULL, 'Q' },
1594 { "x264opts", required_argument, NULL, 'x' },
1595 { "turbo", no_argument, NULL, 'T' },
1596 { "maxHeight", required_argument, NULL, 'Y' },
1597 { "maxWidth", required_argument, NULL, 'X' },
1598 { "preset", required_argument, NULL, 'Z' },
1599 { "preset-list", no_argument, NULL, 'z' },
1600 { "vfr", no_argument, NULL, 'V' },
1605 int option_index = 0;
1608 c = getopt_long( argc, argv,
1609 "hvuC:f:4i:Io:t:Lc:m::a:6:s:UFN:e:E:2dD:789gpOP::w:l:n:b:q:S:B:r:R:Qx:TY:X:VZ:z",
1610 long_options, &option_index );
1625 debug = HB_DEBUG_ALL;
1628 cpu = atoi( optarg );
1633 preset_name = strdup(optarg);
1640 format = strdup( optarg );
1643 input = strdup( optarg );
1646 output = strdup( optarg );
1659 titleindex = atoi( optarg );
1667 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1669 chapter_start = start;
1672 else if( sscanf( optarg, "%d", &start ) == 1 )
1674 chapter_start = start;
1675 chapter_end = chapter_start;
1679 fprintf( stderr, "chapters: invalid syntax (%s)\n",
1686 if( optarg != NULL )
1688 marker_file = strdup( optarg );
1690 chapter_markers = 1;
1694 char * token = strtok(optarg, ",");
1697 int track_start, track_end;
1698 while( token != NULL )
1700 audio = calloc(1, sizeof(*audio));
1701 hb_audio_config_init(audio);
1702 if (strlen(token) >= 3)
1704 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1707 for (i = track_start - 1; i < track_end; i++)
1709 if (i != track_start - 1)
1711 audio = calloc(1, sizeof(*audio));
1712 hb_audio_config_init(audio);
1714 audio->in.track = i;
1715 audio->out.track = num_audio_tracks++;
1716 hb_list_add(audios, audio);
1719 else if( !strcasecmp(token, "none" ) )
1721 audio->in.track = audio->out.track = -1;
1722 audio->out.codec = 0;
1723 hb_list_add(audios, audio);
1728 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1735 audio->in.track = atoi(token) - 1;
1736 audio->out.track = num_audio_tracks++;
1737 hb_list_add(audios, audio);
1739 token = strtok(NULL, ",");
1744 if( optarg != NULL )
1746 mixdowns = strdup( optarg );
1750 if( optarg != NULL )
1752 dynamic_range_compression = strdup( optarg );
1756 sub = atoi( optarg );
1765 native_language = strdup( optarg );
1771 if( optarg != NULL )
1773 if (!( strcmp( optarg, "fast" ) ))
1775 deinterlace_opt = "-1";
1777 else if (!( strcmp( optarg, "slow" ) ))
1779 deinterlace_opt = "2";
1781 else if (!( strcmp( optarg, "slower" ) ))
1783 deinterlace_opt = "0";
1787 deinterlace_opt = strdup( optarg );
1793 if( optarg != NULL )
1795 deblock_opt = strdup( optarg );
1800 if( optarg != NULL )
1802 if (!( strcmp( optarg, "weak" ) ))
1804 denoise_opt = "2:1:2:3";
1806 else if (!( strcmp( optarg, "medium" ) ))
1808 denoise_opt = "3:2:2:3";
1810 else if (!( strcmp( optarg, "strong" ) ))
1812 denoise_opt = "7:7:5:5";
1816 denoise_opt = strdup( optarg );
1822 if( optarg != NULL )
1824 detelecine_opt = strdup( optarg );
1835 loosePixelratio = 1;
1836 if( optarg != NULL )
1838 modulus = atoi( optarg );
1842 if( !strcasecmp( optarg, "ffmpeg" ) )
1844 vcodec = HB_VCODEC_FFMPEG;
1846 else if( !strcasecmp( optarg, "xvid" ) )
1848 vcodec = HB_VCODEC_XVID;
1850 else if( !strcasecmp( optarg, "x264" ) )
1852 vcodec = HB_VCODEC_X264;
1854 else if( !strcasecmp( optarg, "x264b13" ) )
1856 vcodec = HB_VCODEC_X264;
1859 else if( !strcasecmp( optarg, "x264b30" ) )
1861 vcodec = HB_VCODEC_X264;
1864 else if( !strcasecmp( optarg, "theora" ) )
1866 vcodec = HB_VCODEC_THEORA;
1870 fprintf( stderr, "invalid codec (%s)\n", optarg );
1875 if( optarg != NULL )
1877 acodecs = strdup( optarg );
1881 width = atoi( optarg );
1884 height = atoi( optarg );
1889 char * tmp = optarg;
1890 for( i = 0; i < 4; i++ )
1894 crop[i] = strtol( tmp, &tmp, 0 );
1903 for( i = 0; i < hb_video_rates_count; i++ )
1905 if( !strcmp( optarg, hb_video_rates[i].string ) )
1907 vrate = hb_video_rates[i].rate;
1913 fprintf( stderr, "invalid framerate %s\n", optarg );
1918 if( optarg != NULL )
1920 arates = strdup( optarg );
1924 vbitrate = atoi( optarg );
1927 vquality = atof( optarg );
1930 size = atoi( optarg );
1933 if( optarg != NULL )
1935 abitrates = strdup( optarg );
1942 x264opts = strdup( optarg );
1945 turbo_opts_enabled = 1;
1948 maxHeight = atoi( optarg );
1951 maxWidth = atoi (optarg );
1958 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
1966 static int CheckOptions( int argc, char ** argv )
1973 if( input == NULL || *input == '\0' )
1975 fprintf( stderr, "Missing input device. Run %s --help for "
1976 "syntax.\n", argv[0] );
1981 if( titleindex > 0 )
1983 if( output == NULL || *output == '\0' )
1985 fprintf( stderr, "Missing output file name. Run %s --help "
1986 "for syntax.\n", argv[0] );
1992 char * p = strrchr( output, '.' );
1995 if( p && !strcasecmp( p, ".avi" ) )
1998 default_acodec = HB_ACODEC_LAME;
2000 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2001 !strcasecmp( p, ".m4v" ) ) )
2007 default_acodec = HB_ACODEC_FAAC;
2009 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2010 !strcasecmp( p, ".ogg" ) ) )
2013 default_acodec = HB_ACODEC_VORBIS;
2015 else if( p && !strcasecmp(p, ".mkv" ) )
2018 default_acodec = HB_ACODEC_AC3;
2022 fprintf( stderr, "Output format couldn't be guessed "
2023 "from file name, using default.\n" );
2027 else if( !strcasecmp( format, "avi" ) )
2030 default_acodec = HB_ACODEC_LAME;
2032 else if( !strcasecmp( format, "mp4" ) ||
2033 !strcasecmp( format, "m4v" ) )
2039 default_acodec = HB_ACODEC_FAAC;
2041 else if( !strcasecmp( format, "ogm" ) ||
2042 !strcasecmp( format, "ogg" ) )
2045 default_acodec = HB_ACODEC_VORBIS;
2047 else if( !strcasecmp( format, "mkv" ) )
2050 default_acodec = HB_ACODEC_AC3;
2054 fprintf( stderr, "Invalid output format (%s). Possible "
2055 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2063 static int get_acodec_for_string( char *codec )
2065 if( !strcasecmp( codec, "ac3" ) )
2067 return HB_ACODEC_AC3;
2069 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2071 return HB_ACODEC_DCA;
2073 else if( !strcasecmp( codec, "lame" ) )
2075 return HB_ACODEC_LAME;
2077 else if( !strcasecmp( codec, "faac" ) )
2079 return HB_ACODEC_FAAC;
2081 else if( !strcasecmp( codec, "vorbis") )
2083 return HB_ACODEC_VORBIS;
2091 static int is_sample_rate_valid(int rate)
2094 for( i = 0; i < hb_audio_rates_count; i++ )
2096 if (rate == hb_audio_rates[i].rate)