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. */
14 #if defined( __MINGW32__ )
18 #if defined( PTW32_STATIC_LIB )
25 #if defined( __APPLE_CC__ )
26 #import <CoreServices/CoreServices.h>
27 #include <IOKit/IOKitLib.h>
28 #include <IOKit/storage/IOMedia.h>
29 #include <IOKit/storage/IODVDMedia.h>
33 static int debug = HB_DEBUG_ALL;
34 static int update = 0;
35 static int dvdnav = 1;
36 static char * input = NULL;
37 static char * output = NULL;
38 static char * format = NULL;
39 static int titleindex = 1;
40 static int titlescan = 0;
41 static int main_feature = 0;
42 static char * native_language = NULL;
43 static int native_dub = 0;
44 static int twoPass = 0;
45 static int deinterlace = 0;
46 static char * deinterlace_opt = 0;
47 static int deblock = 0;
48 static char * deblock_opt = 0;
49 static int denoise = 0;
50 static char * denoise_opt = 0;
51 static int detelecine = 0;
52 static char * detelecine_opt = 0;
53 static int decomb = 0;
54 static char * decomb_opt = 0;
55 static int rotate = 0;
56 static char * rotate_opt = 0;
57 static int grayscale = 0;
58 static int vcodec = HB_VCODEC_FFMPEG;
59 static int h264_13 = 0;
60 static int h264_30 = 0;
61 static hb_list_t * audios = NULL;
62 static hb_audio_config_t * audio = NULL;
63 static int num_audio_tracks = 0;
64 static char * mixdowns = NULL;
65 static char * dynamic_range_compression = NULL;
66 static char * atracks = NULL;
67 static char * arates = NULL;
68 static char * abitrates = NULL;
69 static char * acodecs = NULL;
70 static char * anames = NULL;
71 static int default_acodec = HB_ACODEC_FAAC;
72 static int default_abitrate = 160;
73 static int audio_explicit = 0;
74 static char ** subtracks = NULL;
75 static char ** subforce = NULL;
76 static char * subburn = NULL;
77 static char * subdefault = NULL;
78 static char ** srtfile = NULL;
79 static char ** srtcodeset = NULL;
80 static char ** srtoffset = NULL;
81 static char ** srtlang = NULL;
82 static int srtdefault = -1;
83 static int subtitle_scan = 0;
85 static int height = 0;
86 static int crop[4] = { -1,-1,-1,-1 };
89 static float vquality = -1.0;
90 static int vbitrate = 0;
93 static int anamorphic_mode = 0;
94 static int modulus = 0;
95 static int par_height = 0;
96 static int par_width = 0;
97 static int display_width = 0;
98 static int keep_display_aspect = 0;
99 static int itu_par = 0;
100 static int angle = 0;
101 static int chapter_start = 0;
102 static int chapter_end = 0;
103 static int chapter_markers = 0;
104 static char * marker_file = NULL;
105 static char *x264opts = NULL;
106 static char *x264opts2 = NULL;
107 static int maxHeight = 0;
108 static int maxWidth = 0;
109 static int turbo_opts_enabled = 0;
110 static char * turbo_opts = "ref=1:subme=2:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
111 static int largeFileSize = 0;
112 static int preset = 0;
113 static char * preset_name = 0;
115 static int mp4_optimize = 0;
116 static int ipod_atom = 0;
117 static int color_matrix = 0;
118 static int preview_count = 10;
119 static int store_previews = 0;
120 static int start_at_preview = 0;
121 static int64_t start_at_pts = 0;
122 static int start_at_frame = 0;
123 static char * start_at_string = NULL;
124 static char * start_at_token = NULL;
125 static int64_t stop_at_pts = 0;
126 static int stop_at_frame = 0;
127 static char * stop_at_string = NULL;
128 static char * stop_at_token = NULL;
130 /* Exit cleanly on Ctrl-C */
131 static volatile int die = 0;
132 static void SigHandler( int );
135 static void ShowCommands();
136 static void ShowHelp();
137 static void ShowPresets();
139 static int ParseOptions( int argc, char ** argv );
140 static int CheckOptions( int argc, char ** argv );
141 static int HandleEvents( hb_handle_t * h );
143 static int get_acodec_for_string( char *codec );
144 static int is_sample_rate_valid(int rate);
147 static char* bsd_name_for_path(char *path);
148 static int device_is_dvd(char *device);
149 static io_service_t get_iokit_service( char *device );
150 static int is_dvd_service( io_service_t service );
151 static int is_whole_media_service( io_service_t service );
154 /* Only print the "Muxing..." message once */
155 static int show_mux_warning = 1;
157 /****************************************************************************
160 * When using the CLI just display using hb_log as we always did in the past
161 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
162 ****************************************************************************/
163 static void hb_cli_error_handler ( const char *errmsg )
165 fprintf( stderr, "ERROR: %s\n", errmsg );
168 int main( int argc, char ** argv )
174 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
175 * force it to unbuffered otherwise informative output is not easily parsed.
177 #if defined( _WIN32 ) || defined( __MINGW32__ )
178 setvbuf( stdout, NULL, _IONBF, 0 );
179 setvbuf( stderr, NULL, _IONBF, 0 );
182 audios = hb_list_init();
184 /* Parse command line */
185 if( ParseOptions( argc, argv ) ||
186 CheckOptions( argc, argv ) )
191 /* Register our error handler */
192 hb_register_error_handler(&hb_cli_error_handler);
195 h = hb_init( debug, update );
196 hb_dvd_set_dvdnav( dvdnav );
199 fprintf( stderr, "%s - %s - %s\n",
200 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
202 /* Check for update */
205 if( ( build = hb_check_update( h, &version ) ) > -1 )
207 fprintf( stderr, "You are using an old version of "
208 "HandBrake.\nLatest is %s (build %d).\n", version,
213 fprintf( stderr, "Your version of HandBrake is up to "
222 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
223 hb_get_cpu_count( h ) > 1 ? "s" : "" );
226 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
227 cpu > 1 ? "s" : "" );
228 hb_set_cpu_count( h, cpu );
231 /* Exit ASAP on Ctrl-C */
232 signal( SIGINT, SigHandler );
234 /* Feed libhb with a DVD to scan */
235 fprintf( stderr, "Opening %s...\n", input );
239 * We need to scan for all the titles in order to find the main feature
244 hb_scan( h, input, titleindex, preview_count, store_previews );
249 #if defined( __MINGW32__ )
253 case 0x03: /* ctrl-c */
255 fprintf( stdout, "\nEncoding Quit by user command\n" );
259 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
271 #elif !defined(SYS_BEOS)
281 FD_SET( STDIN_FILENO, &fds );
282 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
289 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
291 if( buf[size] == '\n' )
298 if( size >= 256 || buf[size] == '\n' )
303 fprintf( stdout, "\nEncoding Quit by user command\n" );
307 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
330 if( input ) free( input );
331 if( output ) free( output );
332 if( format ) free( format );
335 while( ( audio = hb_list_item( audios, 0 ) ) )
337 hb_list_rem( audios, audio );
338 if( audio->out.name )
340 free( audio->out.name );
344 hb_list_close( &audios );
346 if( mixdowns ) free( mixdowns );
347 if( dynamic_range_compression ) free( dynamic_range_compression );
348 if( atracks ) free( atracks );
349 if( arates ) free( arates );
350 if( abitrates ) free( abitrates );
351 if( acodecs ) free( acodecs );
352 if( anames ) free( anames );
353 if (native_language ) free (native_language );
354 if( x264opts ) free (x264opts );
355 if( x264opts2 ) free (x264opts2 );
356 if (preset_name) free (preset_name);
357 if( stop_at_string ) free( stop_at_string );
358 if( start_at_string ) free( start_at_string );
360 fprintf( stderr, "HandBrake has exited.\n" );
365 static void ShowCommands()
367 fprintf( stdout, "\nCommands:\n" );
368 fprintf( stdout, " [h]elp Show this message\n" );
369 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
370 fprintf( stdout, " [p]ause Pause encoding\n" );
371 fprintf( stdout, " [r]esume Resume encoding\n" );
374 static void PrintTitleInfo( hb_title_t * title )
376 hb_chapter_t * chapter;
377 hb_subtitle_t * subtitle;
380 fprintf( stderr, "+ title %d:\n", title->index );
381 if ( title->index == title->job->feature )
383 fprintf( stderr, " + Main Feature\n" );
385 if ( title->type == HB_STREAM_TYPE )
387 fprintf( stderr, " + stream: %s\n", title->path );
389 else if ( title->type == HB_DVD_TYPE )
391 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
392 title->vts, title->ttn, title->cell_start, title->cell_end,
393 title->block_count );
396 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
397 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
398 title->hours, title->minutes, title->seconds );
399 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
400 title->width, title->height,
401 title->pixel_aspect_width,
402 title->pixel_aspect_height,
403 (float) title->aspect,
404 (float) title->rate / title->rate_base );
405 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
406 title->crop[1], title->crop[2], title->crop[3] );
407 fprintf( stderr, " + chapters:\n" );
408 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
410 chapter = hb_list_item( title->list_chapter, i );
411 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
412 "%02d:%02d:%02d\n", chapter->index,
413 chapter->cell_start, chapter->cell_end,
414 chapter->block_count, chapter->hours, chapter->minutes,
417 fprintf( stderr, " + audio tracks:\n" );
418 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
420 audio = hb_list_audio_config_item( title->list_audio, i );
421 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
423 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
425 audio->lang.description,
426 audio->lang.iso639_2,
427 audio->in.samplerate,
432 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
434 audio->lang.description,
435 audio->lang.iso639_2 );
438 fprintf( stderr, " + subtitle tracks:\n" );
439 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
441 subtitle = hb_list_item( title->list_subtitle, i );
442 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
443 i + 1, subtitle->lang,
445 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
448 if(title->detected_interlacing)
450 /* Interlacing was found in half or more of the preview frames */
451 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
456 static int test_sub_list( char ** list, char * needle, int pos )
460 if ( list == NULL || needle == NULL )
463 if ( list[0] == NULL && pos == 1 )
466 for ( i = 0; list[i] != NULL; i++ )
468 if ( strcasecmp( list[i], needle ) == 0 )
474 static int HandleEvents( hb_handle_t * h )
477 int tmp_num_audio_tracks;
479 hb_get_state( h, &s );
486 #define p s.param.scanning
487 case HB_STATE_SCANNING:
488 /* Show what title is currently being scanned */
489 fprintf( stderr, "Scanning title %d", p.title_cur );
490 if( !titleindex || titlescan )
491 fprintf( stderr, " of %d", p.title_count );
492 fprintf( stderr, "...\n" );
496 case HB_STATE_SCANDONE:
504 /* Audio argument string parsing variables */
508 int mixdown = HB_AMIXDOWN_DOLBYPLII;
510 /* Audio argument string parsing variables */
512 list = hb_get_titles( h );
514 if( !hb_list_count( list ) )
516 /* No valid title, stop right there */
517 fprintf( stderr, "No title found.\n" );
524 int main_feature_idx=0;
525 int main_feature_pos=-1;
526 int main_feature_time=0;
529 fprintf( stderr, "Searching for main feature title...\n" );
531 for( i = 0; i < hb_list_count( list ); i++ )
533 title = hb_list_item( list, i );
534 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
535 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
536 i, title->index, title_time );
537 if( main_feature_time < title_time )
539 main_feature_time = title_time;
540 main_feature_pos = i;
541 main_feature_idx = title->index;
543 if( title->job->feature == title->index )
545 main_feature_time = title_time;
546 main_feature_pos = i;
547 main_feature_idx = title->index;
551 if( main_feature_pos == -1 )
553 fprintf( stderr, "No main feature title found.\n" );
557 titleindex = main_feature_idx;
558 fprintf( stderr, "Found main feature title, setting title to %d\n",
561 title = hb_list_item( list, main_feature_pos);
563 title = hb_list_item( list, 0 );
566 if( !titleindex || titlescan )
568 /* Scan-only mode, print infos and exit */
570 for( i = 0; i < hb_list_count( list ); i++ )
572 title = hb_list_item( list, i );
573 PrintTitleInfo( title );
579 /* Set job settings */
582 PrintTitleInfo( title );
584 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame && !start_at_pts && !start_at_frame )
586 job->chapter_start = MAX( job->chapter_start,
588 job->chapter_end = MIN( job->chapter_end,
590 job->chapter_end = MAX( job->chapter_start,
601 fprintf( stderr, "+ Using preset: %s", preset_name);
603 if (!strcmp(preset_name, "Universal"))
609 vcodec = HB_VCODEC_X264;
610 job->vquality = 20.0;
613 atracks = strdup("1,1");
617 acodecs = strdup("faac,ac3");
621 abitrates = strdup("160,160");
625 mixdowns = strdup("dpl2,auto");
629 arates = strdup("48,Auto");
631 if( !dynamic_range_compression )
633 dynamic_range_compression = strdup("0.0,0.0");
638 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6");
640 if( !anamorphic_mode )
644 job->chapter_markers = 1;
648 if (!strcmp(preset_name, "iPod"))
655 vcodec = HB_VCODEC_X264;
659 atracks = strdup("1");
663 acodecs = strdup("faac");
667 abitrates = strdup("160");
671 mixdowns = strdup("dpl2");
675 arates = strdup("48");
677 if( !dynamic_range_compression )
679 dynamic_range_compression = strdup("0.0");
684 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0");
686 job->chapter_markers = 1;
690 if (!strcmp(preset_name, "iPhone & iPod Touch"))
696 vcodec = HB_VCODEC_X264;
697 job->vquality = 20.0;
700 atracks = strdup("1");
704 acodecs = strdup("faac");
708 abitrates = strdup("128");
712 mixdowns = strdup("dpl2");
716 arates = strdup("48");
718 if( !dynamic_range_compression )
720 dynamic_range_compression = strdup("0.0");
725 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0");
727 job->chapter_markers = 1;
731 if (!strcmp(preset_name, "iPad"))
737 job->largeFileSize = 1;
738 vcodec = HB_VCODEC_X264;
739 job->vquality = 20.0;
740 job->vrate_base = 900900;
744 atracks = strdup("1");
748 acodecs = strdup("faac");
752 abitrates = strdup("160");
756 mixdowns = strdup("dpl2");
760 arates = strdup("48");
762 if( !dynamic_range_compression )
764 dynamic_range_compression = strdup("0.0");
767 if( !anamorphic_mode )
771 job->chapter_markers = 1;
775 if (!strcmp(preset_name, "AppleTV"))
781 job->largeFileSize = 1;
782 vcodec = HB_VCODEC_X264;
783 job->vquality = 20.0;
786 atracks = strdup("1,1");
790 acodecs = strdup("faac,ac3");
794 abitrates = strdup("160,160");
798 mixdowns = strdup("dpl2,auto");
802 arates = strdup("48,Auto");
804 if( !dynamic_range_compression )
806 dynamic_range_compression = strdup("0.0,0.0");
811 x264opts = strdup("cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500");
813 if( !anamorphic_mode )
817 job->chapter_markers = 1;
821 if (!strcmp(preset_name, "Normal"))
827 vcodec = HB_VCODEC_X264;
828 job->vquality = 20.0;
831 atracks = strdup("1");
835 acodecs = strdup("faac");
839 abitrates = strdup("160");
843 mixdowns = strdup("dpl2");
847 arates = strdup("48");
849 if( !dynamic_range_compression )
851 dynamic_range_compression = strdup("0.0");
855 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
857 if( !anamorphic_mode )
861 job->chapter_markers = 1;
865 if (!strcmp(preset_name, "High Profile"))
871 vcodec = HB_VCODEC_X264;
872 job->vquality = 20.0;
875 atracks = strdup("1,1");
879 acodecs = strdup("faac,ac3");
883 abitrates = strdup("160,160");
887 mixdowns = strdup("dpl2,auto");
891 arates = strdup("48,Auto");
893 if( !dynamic_range_compression )
895 dynamic_range_compression = strdup("0.0,0.0");
899 x264opts = strdup("b-adapt=2:rc-lookahead=50");
903 if( !anamorphic_mode )
907 job->chapter_markers = 1;
911 if (!strcmp(preset_name, "Classic"))
917 job->vbitrate = 1000;
920 atracks = strdup("1");
924 acodecs = strdup("faac");
928 abitrates = strdup("160");
932 mixdowns = strdup("dpl2");
936 arates = strdup("48");
938 if( !dynamic_range_compression )
940 dynamic_range_compression = strdup("0.0");
945 if (!strcmp(preset_name, "AppleTV Legacy"))
951 job->largeFileSize = 1;
952 vcodec = HB_VCODEC_X264;
953 job->vbitrate = 2500;
956 atracks = strdup("1,1");
960 acodecs = strdup("faac,ac3");
964 abitrates = strdup("160,160");
968 mixdowns = strdup("dpl2,auto");
972 arates = strdup("48,Auto");
974 if( !dynamic_range_compression )
976 dynamic_range_compression = strdup("0.0,0.0");
980 x264opts = strdup("ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
982 if( !anamorphic_mode )
986 job->chapter_markers = 1;
990 if (!strcmp(preset_name, "iPhone Legacy"))
997 vcodec = HB_VCODEC_X264;
1001 atracks = strdup("1");
1005 acodecs = strdup("faac");
1009 abitrates = strdup("128");
1013 mixdowns = strdup("dpl2");
1017 arates = strdup("48");
1019 if( !dynamic_range_compression )
1021 dynamic_range_compression = strdup("0.0");
1026 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:bframes=0:subme=6:8x8dct=0:trellis=0");
1028 job->chapter_markers = 1;
1032 if (!strcmp(preset_name, "iPod Legacy"))
1039 vcodec = HB_VCODEC_X264;
1040 job->vbitrate = 1500;
1043 atracks = strdup("1");
1047 acodecs = strdup("faac");
1051 abitrates = strdup("160");
1055 mixdowns = strdup("dpl2");
1059 arates = strdup("48");
1061 if( !dynamic_range_compression )
1063 dynamic_range_compression = strdup("0.0");
1068 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0");
1070 job->chapter_markers = 1;
1076 if ( chapter_markers )
1078 job->chapter_markers = chapter_markers;
1080 if( marker_file != NULL )
1082 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1083 hb_csv_cell_t * cell;
1087 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1091 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1095 /* Parse the cells */
1096 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1098 /* We have a chapter number */
1099 if( cell->cell_col == 0 )
1101 row = cell->cell_row;
1102 chapter = atoi( cell->cell_text );
1105 /* We have a chapter name */
1106 if( cell->cell_col == 1 && row == cell->cell_row )
1108 /* If we have a valid chapter, copy the string an terminate it */
1109 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1111 hb_chapter_t * chapter_s;
1113 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1114 strncpy(chapter_s->title, cell->cell_text, 1023);
1115 chapter_s->title[1023] = '\0';
1120 hb_dispose_cell( cell );
1123 hb_close_csv_file( file );
1128 /* No marker file */
1130 int number_of_chapters = hb_list_count(job->title->list_chapter);
1133 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1135 hb_chapter_t * chapter_s;
1136 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1137 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1138 chapter_s->title[1023] = '\0';
1143 if( crop[0] >= 0 && crop[1] >= 0 &&
1144 crop[2] >= 0 && crop[3] >= 0 )
1146 memcpy( job->crop, crop, 4 * sizeof( int ) );
1149 job->deinterlace = deinterlace;
1150 job->grayscale = grayscale;
1152 /* Add selected filters */
1153 job->filters = hb_list_init();
1157 hb_filter_rotate.settings = rotate_opt;
1158 hb_list_add( job->filters, &hb_filter_rotate);
1162 hb_filter_detelecine.settings = detelecine_opt;
1163 hb_list_add( job->filters, &hb_filter_detelecine );
1167 hb_filter_decomb.settings = decomb_opt;
1168 hb_list_add( job->filters, &hb_filter_decomb );
1172 hb_filter_deinterlace.settings = deinterlace_opt;
1173 hb_list_add( job->filters, &hb_filter_deinterlace );
1177 hb_filter_deblock.settings = deblock_opt;
1178 hb_list_add( job->filters, &hb_filter_deblock );
1182 hb_filter_denoise.settings = denoise_opt;
1183 hb_list_add( job->filters, &hb_filter_denoise );
1186 switch( anamorphic_mode )
1188 case 0: // Non-anamorphic
1190 if( width && height )
1193 job->height = height;
1198 hb_fix_aspect( job, HB_KEEP_WIDTH );
1202 job->height = height;
1203 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1205 else if( !width && !height )
1207 hb_fix_aspect( job, HB_KEEP_WIDTH );
1212 case 1: // Strict anammorphic
1213 job->anamorphic.mode = anamorphic_mode;
1216 case 2: // Loose anamorphic
1217 job->anamorphic.mode = 2;
1221 job->modulus = modulus;
1226 job->anamorphic.itu_par = itu_par;
1233 else if( !width && !height )
1235 /* Default to full width when one isn't specified for loose anamorphic */
1236 job->width = title->width - job->crop[2] - job->crop[3];
1241 case 3: // Custom Anamorphic 3: Power User Jamboree
1242 job->anamorphic.mode = 3;
1246 job->modulus = modulus;
1251 job->anamorphic.itu_par = itu_par;
1254 if( par_width && par_height )
1256 job->anamorphic.par_width = par_width;
1257 job->anamorphic.par_height = par_height;
1260 if( keep_display_aspect )
1262 job->anamorphic.keep_display_aspect = 1;
1264 /* First, what *is* the display aspect? */
1265 int cropped_width = title->width - job->crop[2] - job->crop[3];
1266 int cropped_height = title->height - job->crop[0] - job->crop[1];
1268 /* XXX -- I'm assuming people want to keep the source
1269 display AR even though they might have already
1270 asked for ITU values instead. */
1271 float source_display_width = (float)cropped_width *
1272 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1273 float display_aspect = source_display_width / (float)cropped_height;
1274 /* When keeping display aspect, we have to rank some values
1275 by priority in order to consistently handle situations
1276 when more than one might be specified by default.
1278 * First off, PAR gets ignored. (err make this reality)
1279 * Height will be respected over all other settings,
1280 * If it isn't set, display_width will be followed.
1281 * If it isn't set, width will be followed. */
1284 /* We scale the display width to the new height */
1285 display_width = (int)( (double)height * display_aspect );
1287 else if( display_width )
1289 /* We scale the height to the new display width */
1290 height = (int)( (double)display_width / display_aspect );
1296 /* Adjust the PAR to create the new display width
1297 from the default job width. */
1298 job->anamorphic.dar_width = display_width;
1300 job->anamorphic.dar_height = height ?
1302 title->height - job->crop[0] - job->crop[1];
1305 if( width && height )
1307 /* Use these storage dimensions */
1309 job->height = height;
1313 /* Use just this storage width */
1315 job->height = title->height - job->crop[0] - job->crop[1];
1319 /* Use just this storage height. */
1320 job->height = height;
1321 job->width = title->width - job->crop[2] - job->crop[3];
1323 else if( !width && !height )
1325 /* Assume source dimensions after cropping. */
1326 job->width = title->width - job->crop[2] - job->crop[3];
1327 job->height = title->height - job->crop[0] - job->crop[1];
1333 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1335 job->vquality = vquality;
1340 job->vquality = -1.0;
1341 job->vbitrate = vbitrate;
1345 job->vcodec = vcodec;
1349 job->h264_level = 13;
1353 job->h264_level = 30;
1358 job->vrate = 27000000;
1359 job->vrate_base = vrate;
1363 // cfr or pfr flag with no rate specified implies
1364 // use the title rate.
1366 job->vrate = title->rate;
1367 job->vrate_base = title->rate_base;
1370 /* Grab audio tracks */
1373 char * token = strtok(atracks, ",");
1376 int track_start, track_end;
1377 while( token != NULL )
1379 audio = calloc(1, sizeof(*audio));
1380 hb_audio_config_init(audio);
1381 if (strlen(token) >= 3)
1383 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1386 for (i = track_start - 1; i < track_end; i++)
1388 if (i != track_start - 1)
1390 audio = calloc(1, sizeof(*audio));
1391 hb_audio_config_init(audio);
1393 audio->in.track = i;
1394 audio->out.track = num_audio_tracks++;
1395 hb_list_add(audios, audio);
1398 else if( !strcasecmp(token, "none" ) )
1400 audio->in.track = audio->out.track = -1;
1401 audio->out.codec = 0;
1402 hb_list_add(audios, audio);
1407 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1414 audio->in.track = atoi(token) - 1;
1415 audio->out.track = num_audio_tracks++;
1416 hb_list_add(audios, audio);
1418 token = strtok(NULL, ",");
1422 /* Parse audio tracks */
1423 if( native_language && native_dub )
1425 if( hb_list_count( audios ) == 0 || !audio_explicit )
1427 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1432 audio = hb_list_audio_config_item( title->list_audio, i );
1434 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1436 if( strncasecmp( native_language, audio_lang,
1437 sizeof( audio_lang ) ) == 0 &&
1438 audio->lang.type != 3 && // Directors 1
1439 audio->lang.type != 4) // Directors 2
1442 * Matched an audio to our native language - use it.
1443 * Replace any existing audio tracks that a preset may
1446 if( hb_list_count(audios) == 0) {
1447 audio = calloc(1, sizeof(*audio));
1448 hb_audio_config_init(audio);
1449 audio->in.track = track;
1450 audio->out.track = num_audio_tracks++;
1451 /* Add it to our audios */
1452 hb_list_add(audios, audio);
1455 * Update the track numbers on what is already in
1458 for( i=0; i < hb_list_count( audios ); i++ )
1460 audio = hb_list_item( audios, i );
1462 audio->in.track = track;
1469 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1473 if( hb_list_count(audios) == 0 &&
1474 hb_list_count(job->title->list_audio) > 0 )
1476 /* Create a new audio track with default settings */
1477 audio = calloc(1, sizeof(*audio));
1478 hb_audio_config_init(audio);
1479 /* Add it to our audios */
1480 hb_list_add(audios, audio);
1483 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1484 for (i = 0; i < tmp_num_audio_tracks; i++)
1486 audio = hb_list_item(audios, 0);
1487 if( (audio == NULL) || (audio->in.track == -1) ||
1488 (audio->out.track == -1) || (audio->out.codec == 0) )
1494 if( hb_audio_add( job, audio ) == 0 )
1496 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1497 audio->in.track + 1 );
1502 hb_list_rem(audios, audio);
1504 if( audio->out.name )
1506 free( audio->out.name);
1515 char * token = strtok(acodecs, ",");
1518 while ( token != NULL )
1520 if ((acodec = get_acodec_for_string(token)) == -1)
1522 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1523 acodec = default_acodec;
1525 if( i < num_audio_tracks )
1527 audio = hb_list_audio_config_item(job->list_audio, i);
1528 audio->out.codec = acodec;
1532 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1533 hb_audio_config_t audio;
1537 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1539 hb_audio_config_init(&audio);
1540 audio.in.track = last_audio->in.track;
1541 audio.out.track = num_audio_tracks++;
1542 audio.out.codec = acodec;
1543 hb_audio_add(job, &audio);
1547 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1550 token = strtok(NULL, ",");
1554 if( i < num_audio_tracks )
1556 /* We have fewer inputs than audio tracks, use the default codec for
1557 * this container for the remaining tracks. Unless we only have one input
1558 * then use that codec instead.
1561 acodec = default_acodec;
1562 for ( ; i < num_audio_tracks; i++)
1564 audio = hb_list_audio_config_item(job->list_audio, i);
1565 audio->out.codec = acodec;
1574 char * token = strtok(arates, ",");
1577 while ( token != NULL )
1579 arate = atoi(token);
1580 audio = hb_list_audio_config_item(job->list_audio, i);
1583 for( j = 0; j < hb_audio_rates_count; j++ )
1585 if( !strcmp( token, hb_audio_rates[j].string ) )
1587 arate = hb_audio_rates[j].rate;
1594 if (!is_sample_rate_valid(arate))
1596 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1597 arate = audio->in.samplerate;
1600 audio->out.samplerate = arate;
1601 if( (++i) >= num_audio_tracks )
1602 break; /* We have more inputs than audio tracks, oops */
1606 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1608 token = strtok(NULL, ",");
1611 if (i < num_audio_tracks)
1613 /* We have fewer inputs than audio tracks, use default sample rate.
1614 * Unless we only have one input, then use that for all tracks.
1617 arate = audio->in.samplerate;
1618 for ( ; i < num_audio_tracks; i++)
1620 audio = hb_list_audio_config_item(job->list_audio, i);
1621 audio->out.samplerate = arate;
1630 char * token = strtok(abitrates, ",");
1633 while ( token != NULL )
1635 abitrate = atoi(token);
1636 audio = hb_list_audio_config_item(job->list_audio, i);
1640 audio->out.bitrate = abitrate;
1641 if( (++i) >= num_audio_tracks )
1642 break; /* We have more inputs than audio tracks, oops */
1646 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1648 token = strtok(NULL, ",");
1651 if (i < num_audio_tracks)
1653 /* We have fewer inputs than audio tracks, use the default bitrate
1654 * for the remaining tracks. Unless we only have one input, then use
1655 * that for all tracks.
1658 abitrate = default_abitrate;
1659 for (; i < num_audio_tracks; i++)
1661 audio = hb_list_audio_config_item(job->list_audio, i);
1662 audio->out.bitrate = abitrate;
1669 if ( dynamic_range_compression )
1671 char * token = strtok(dynamic_range_compression, ",");
1673 token = dynamic_range_compression;
1674 while ( token != NULL )
1676 d_r_c = atof(token);
1677 audio = hb_list_audio_config_item(job->list_audio, i);
1680 audio->out.dynamic_range_compression = d_r_c;
1681 if( (++i) >= num_audio_tracks )
1682 break; /* We have more inputs than audio tracks, oops */
1686 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1688 token = strtok(NULL, ",");
1691 if (i < num_audio_tracks)
1693 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1694 * tracks. Unless we only have one input, then use the same DRC for all
1699 for (; i < num_audio_tracks; i++)
1701 audio = hb_list_audio_config_item(job->list_audio, i);
1702 audio->out.dynamic_range_compression = d_r_c;
1711 char * token = strtok(mixdowns, ",");
1714 while ( token != NULL )
1716 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1717 audio = hb_list_audio_config_item(job->list_audio, i);
1720 audio->out.mixdown = mixdown;
1721 if( (++i) >= num_audio_tracks )
1722 break; /* We have more inputs than audio tracks, oops */
1726 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1728 token = strtok(NULL, ",");
1731 if (i < num_audio_tracks)
1733 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1734 * we only have one input, then use that.
1737 mixdown = HB_AMIXDOWN_DOLBYPLII;
1738 for (; i < num_audio_tracks; i++)
1740 audio = hb_list_audio_config_item(job->list_audio, i);
1741 audio->out.mixdown = mixdown;
1746 /* Audio Track Names */
1750 char * token = strtok(anames, ",");
1753 while ( token != NULL )
1755 audio = hb_list_audio_config_item(job->list_audio, i);
1758 audio->out.name = strdup(token);
1759 if( (++i) >= num_audio_tracks )
1760 break; /* We have more names than audio tracks, oops */
1764 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1767 token = strtok(NULL, ",");
1770 if( i < num_audio_tracks && i == 1 )
1772 /* We have exactly one name and more than one audio track. Use the same
1773 * name for all tracks. */
1774 for ( ; i < num_audio_tracks; i++)
1776 audio = hb_list_audio_config_item(job->list_audio, i);
1777 audio->out.name = strdup(anames);
1780 /* Audio Track Names */
1784 job->vbitrate = hb_calc_bitrate( job, size );
1785 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1795 for ( i = 0; subtracks[i] != NULL; i++ )
1798 token = subtracks[i];
1799 if( strcasecmp(token, "scan" ) == 0 )
1801 int burn = 0, force = 0, def = 0;
1803 if ( subburn != NULL )
1805 burn = ( pos == 1 && subburn[0] == 0 ) ||
1806 ( strcmp( "scan", subburn ) == 0 );
1808 if ( subdefault != NULL )
1810 def = ( pos == 1 && subdefault[0] == 0 ) ||
1811 ( strcmp( "scan", subdefault ) == 0 );
1813 force = test_sub_list( subforce, "scan", pos );
1817 job->select_subtitle_config.dest = PASSTHRUSUB;
1827 job->select_subtitle_config.force = force;
1828 job->select_subtitle_config.default_track = def;
1833 hb_subtitle_t * subtitle;
1834 hb_subtitle_config_t sub_config;
1836 int burn = 0, force = 0, def = 0;
1838 track = atoi(token) - 1;
1839 subtitle = hb_list_item(title->list_subtitle, track);
1840 if( subtitle == NULL )
1842 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1845 sub_config = subtitle->config;
1847 if ( subburn != NULL )
1849 burn = ( pos == 1 && subburn[0] == 0 ) ||
1850 ( strcmp( token, subburn ) == 0 );
1852 if ( subdefault != NULL )
1854 def = ( pos == 1 && subdefault[0] == 0 ) ||
1855 ( strcmp( token, subdefault ) == 0 );
1858 force = test_sub_list(subforce, token, pos);
1860 if ( !burn && subtitle->format == PICTURESUB)
1862 sub_config.dest = PASSTHRUSUB;
1864 else if ( burn && subtitle->format == PICTURESUB )
1866 // Only allow one subtitle to be burned into video
1869 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1874 sub_config.force = force;
1875 sub_config.default_track = def;
1876 hb_subtitle_add( job, &sub_config, track );
1885 hb_subtitle_config_t sub_config;
1888 for( i=0; srtfile[i] != NULL; i++ )
1890 char *codeset = "L1";
1896 if( srtcodeset && srtcodeset[i] )
1898 codeset = srtcodeset[i];
1900 if( srtoffset && srtoffset[i] )
1902 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1904 if ( srtlang && srtlang[i] )
1908 sub_config.default_track =
1909 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1910 sub_config.force = 0;
1911 strncpy( sub_config.src_filename, srtfile[i], 128);
1912 strncpy( sub_config.src_codeset, codeset, 40);
1913 sub_config.offset = offset;
1915 hb_srt_add( job, &sub_config, lang);
1919 if( native_language )
1923 audio = hb_list_audio_config_item(job->list_audio, 0);
1927 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1929 if( strncasecmp( native_language, audio_lang,
1930 sizeof( audio_lang ) ) != 0 )
1933 * Audio language is not the same as our native language.
1934 * If we have any subtitles in our native language they
1935 * should be selected here if they haven't already been.
1937 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1938 int matched_track = 0;
1940 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1942 subtitle = hb_list_item( title->list_subtitle, i );
1944 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1947 * Found the first matching subtitle in our
1948 * native language. Is it already selected?
1950 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1952 subtitle2 = hb_list_item( job->list_subtitle, i );
1954 if( subtitle2->track == subtitle->track) {
1963 if( subtitle2 == NULL )
1966 * Not already selected, so select it.
1968 hb_subtitle_config_t sub_config;
1972 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1974 sub_config = subtitle->config;
1976 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1978 sub_config.dest = PASSTHRUSUB;
1981 sub_config.force = 0;
1982 sub_config.default_track = 1;
1983 hb_subtitle_add( job, &sub_config, matched_track);
2000 if ( largeFileSize )
2002 job->largeFileSize = 1;
2006 job->mp4_optimize = 1;
2013 job->file = strdup( output );
2017 job->color_matrix = color_matrix;
2020 if( x264opts != NULL && *x264opts != '\0' )
2022 job->x264opts = x264opts;
2024 else /*avoids a bus error crash when options aren't specified*/
2026 job->x264opts = NULL;
2029 job->maxWidth = maxWidth;
2031 job->maxHeight = maxHeight;
2033 if( start_at_preview )
2035 job->start_at_preview = start_at_preview - 1;
2036 job->seek_points = preview_count;
2041 job->pts_to_stop = stop_at_pts;
2047 job->frame_to_stop = stop_at_frame;
2053 job->pts_to_start = start_at_pts;
2057 if( start_at_frame )
2059 job->frame_to_start = start_at_frame;
2068 * When subtitle scan is enabled do a fast pre-scan job
2069 * which will determine which subtitles to enable, if any.
2073 x264opts_tmp = job->x264opts;
2075 job->x264opts = NULL;
2077 job->indepth_scan = subtitle_scan;
2078 fprintf( stderr, "Subtitle Scan Enabled - enabling "
2079 "subtitles if found for foreign language segments\n");
2082 * Add the pre-scan job
2086 job->x264opts = x264opts_tmp;
2092 * If subtitle_scan is enabled then only turn it on
2093 * for the first pass and then off again for the
2098 job->indepth_scan = 0;
2102 x264opts2 = strdup(x264opts);
2106 * If turbo options have been selected then append them
2107 * to the x264opts now (size includes one ':' and the '\0')
2109 if( turbo_opts_enabled )
2111 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2114 tmp_x264opts = malloc(size * sizeof(char));
2117 snprintf( tmp_x264opts, size, "%s:%s",
2118 x264opts, turbo_opts );
2122 * No x264opts to modify, but apply the turbo options
2123 * anyway as they may be modifying defaults
2125 snprintf( tmp_x264opts, size, "%s",
2128 x264opts = tmp_x264opts;
2130 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2133 job->x264opts = x264opts;
2139 * On the second pass we turn off subtitle scan so that we
2140 * can actually encode using any subtitles that were auto
2141 * selected in the first pass (using the whacky select-subtitle
2142 * attribute of the job).
2144 job->indepth_scan = 0;
2146 job->x264opts = x264opts2;
2153 * Turn on subtitle scan if requested, note that this option
2154 * precludes encoding of any actual subtitles.
2157 job->indepth_scan = 0;
2165 #define p s.param.working
2166 case HB_STATE_SEARCHING:
2167 fprintf( stdout, "\rEncoding: task %d of %d, Searching for start time, %.2f %%",
2168 p.job_cur, p.job_count, 100.0 * p.progress );
2169 if( p.seconds > -1 )
2171 fprintf( stdout, " (ETA %02dh%02dm%02ds)",
2172 p.hours, p.minutes, p.seconds );
2177 case HB_STATE_WORKING:
2178 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2179 p.job_cur, p.job_count, 100.0 * p.progress );
2180 if( p.seconds > -1 )
2182 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2183 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2184 p.hours, p.minutes, p.seconds );
2190 #define p s.param.muxing
2191 case HB_STATE_MUXING:
2193 if (show_mux_warning)
2195 fprintf( stdout, "\rMuxing: this may take awhile..." );
2197 show_mux_warning = 0;
2203 #define p s.param.workdone
2204 case HB_STATE_WORKDONE:
2205 /* Print error if any, then exit */
2209 fprintf( stderr, "\nRip done!\n" );
2211 case HB_ERROR_CANCELED:
2212 fprintf( stderr, "\nRip canceled.\n" );
2215 fprintf( stderr, "\nRip failed (error %x).\n",
2225 /****************************************************************************
2227 ****************************************************************************/
2228 static volatile int64_t i_die_date = 0;
2229 void SigHandler( int i_signal )
2234 i_die_date = hb_get_date();
2235 fprintf( stderr, "Signal %d received, terminating - do it "
2236 "again in case it gets stuck\n", i_signal );
2238 else if( i_die_date + 500 < hb_get_date() )
2240 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2245 /****************************************************************************
2247 ****************************************************************************/
2248 static void ShowHelp()
2251 FILE* const out = stdout;
2254 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2256 "### General Handbrake Options------------------------------------------------\n\n"
2257 " -h, --help Print help\n"
2258 " -u, --update Check for updates and exit\n"
2259 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2260 " -C, --cpu Set CPU count (default: autodetected)\n"
2261 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2262 " if the preset name has spaces, surround it with\n"
2263 " double quotation marks\n"
2264 " -z, --preset-list See a list of available built-in presets\n"
2265 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2266 " (experimental, enabled by default for testing)\n"
2269 "### Source Options-----------------------------------------------------------\n\n"
2270 " -i, --input <string> Set input device\n"
2271 " -t, --title <number> Select a title to encode (0 to scan all titles only,\n"
2273 " --scan Scan selected title only.\n"
2274 " --main-feature Detect and select the main feature title.\n"
2275 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2276 " 1 to 3, or \"3\" for chapter 3 only,\n"
2277 " default: all chapters)\n"
2278 " --angle <number> Select the DVD angle\n"
2279 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2280 " and whether or not they're stored to disk (0 or 1).\n"
2281 " (default: 10:0)\n"
2282 " --start-at-preview <#> Start encoding at a given preview.\n"
2283 " --start-at <unit:#> Start encoding at a given frame, duration (in seconds),\n"
2284 " or pts (on a 90kHz clock)\n"
2285 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2286 " or pts (on a 90kHz clock)"
2289 "### Destination Options------------------------------------------------------\n\n"
2290 " -o, --output <string> Set output file name\n"
2291 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2292 " autodetected from file name)\n"
2293 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2294 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2295 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2296 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2297 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2301 "### Video Options------------------------------------------------------------\n\n"
2302 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2303 " (default: ffmpeg)\n"
2304 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2305 " same style as mencoder:\n"
2306 " option1=value1:option2=value2\n"
2307 " -q, --quality <number> Set video quality\n"
2308 " -S, --size <MB> Set target size\n"
2309 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2310 " -2, --two-pass Use two-pass mode\n"
2311 " -T, --turbo When using 2-pass use the turbo options\n"
2312 " on the first pass to improve speed\n"
2313 " (only works with x264, affects PSNR by about 0.05dB,\n"
2314 " and increases first pass speed two to four times)\n"
2315 " -r, --rate Set video framerate (" );
2316 for( i = 0; i < hb_video_rates_count; i++ )
2318 fprintf( out, hb_video_rates[i].string );
2319 if( i != hb_video_rates_count - 1 )
2320 fprintf( out, "/" );
2323 " Be aware that not specifying a framerate lets\n"
2324 " HandBrake preserve a source's time stamps,\n"
2325 " potentially creating variable framerate video\n"
2326 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2327 " frame rate control. VFR preserves the source\n"
2328 " timing. CFR makes the output constant rate at\n"
2329 " the rate given by the -r flag (or the source's\n"
2330 " average rate if no -r is given). PFR doesn't\n"
2331 " allow the rate to go over the rate specified\n"
2332 " with the -r flag but won't change the source\n"
2333 " timing if it's below that rate.\n"
2334 " If none of these flags are given, the default\n"
2335 " is --cfr when -r is given and --vfr otherwise\n"
2338 "### Audio Options-----------------------------------------------------------\n\n"
2339 " -a, --audio <string> Select audio track(s), separated by commas\n"
2340 " More than one output track can be used for one\n"
2342 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2343 " tracks, default: first one)\n" );
2347 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2348 " ac3 and dts meaning passthrough\n"
2349 " Separated by commas for more than one audio track.\n"
2350 " (default: guessed)\n" );
2353 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2354 " ac3 and dts meaning passthrough\n"
2355 " Separated by commas for more than one audio track.\n"
2356 " (default: guessed)\n" );
2359 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2360 " Separated by commas for more than one audio track.\n"
2361 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2362 " Separated by commas for more than one audio track.\n"
2363 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2364 " -R, --arate Set audio samplerate(s) (" );
2365 for( i = 0; i < hb_audio_rates_count; i++ )
2367 fprintf( out, hb_audio_rates[i].string );
2368 if( i != hb_audio_rates_count - 1 )
2369 fprintf( out, "/" );
2371 fprintf( out, " kHz)\n"
2372 " Separated by commas for more than one audio track.\n"
2373 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2374 " making soft sounds louder. Range is 1.0 to 4.0\n"
2375 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2376 " Separated by commas for more than one audio track.\n"
2377 " -A, --aname <string> Audio track name(s),\n"
2378 " Separated by commas for more than one audio track.\n"
2381 "### Picture Settings---------------------------------------------------------\n\n"
2382 " -w, --width <number> Set picture width\n"
2383 " -l, --height <number> Set picture height\n"
2384 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2385 " -Y, --maxHeight <#> Set maximum height\n"
2386 " -X, --maxWidth <#> Set maximum width\n"
2387 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2388 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2389 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2390 " directly control all parameters.\n"
2391 " --display-width Set the width to scale the actual pixels to\n"
2392 " <number> at playback, for custom anamorphic.\n"
2393 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2394 " when using custom anamorphic\n"
2395 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2397 " (--display-width and --pixel-aspect are mutually\n"
2398 " exclusive and the former will override the latter)\n"
2399 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2400 " custom anamorphic, useful with underscanned sources\n"
2401 " --modulus Set the number you want the scaled pixel dimensions\n"
2402 " <number> to divide cleanly by. Does not affect strict\n"
2403 " anamorphic mode, which is always mod 2 (default: 16)\n"
2404 " -M --color-matrix Set the color space signaled by the output\n"
2405 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2406 " default: set by resolution)\n"
2409 "### Filters---------------------------------------------------------\n\n"
2411 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2412 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2414 " <fast/slow/slower>\n"
2415 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2416 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2417 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2418 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2419 " Note: this filter drops duplicate frames to\n"
2420 " restore the pre-telecine framerate, unless you\n"
2421 " specify a constant framerate (--rate 29.97)\n"
2422 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2423 " -8, --denoise Denoise video with hqdn3d filter\n"
2424 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2426 " <weak/medium/strong>\n"
2427 " -7, --deblock Deblock video with pp7 filter\n"
2428 " <QP:M> (default 5:2)\n"
2429 " --rotate Flips images axes\n"
2430 " <M> (default 3)\n"
2431 " -g, --grayscale Grayscale encoding\n"
2434 "### Subtitle Options------------------------------------------------------------\n\n"
2435 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2436 " More than one output track can be used for one\n"
2438 " (\"1,2,3\" for multiple tracks.\n"
2439 " A special track name \"scan\" adds an extra 1st pass.\n"
2440 " This extra pass scans subtitles matching the\n"
2441 " language of the first audio or the language \n"
2442 " selected by --native-language.\n"
2443 " The one that's only used 10 percent of the time\n"
2444 " or less is selected. This should locate subtitles\n"
2445 " for short foreign language segments. Best used in\n"
2446 " conjunction with --subtitle-forced.\n"
2447 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2448 " <string> the subtitle has the forced flag set. May be used in\n"
2449 " conjunction with \"scan\" track to auto-select\n"
2450 " a stream if it contains forced subtitles.\n"
2451 " Separated by commas for more than one audio track.\n"
2452 " (\"1,2,3\" for multiple tracks.\n"
2453 " If \"string\" is omitted, the first trac is forced.\n"
2454 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2455 " <number> If \"number\" is omitted, the first trac is burned.\n"
2456 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2457 " <number> to be displayed upon playback. Setting no default\n"
2458 " means no subtitle will be automatically displayed\n"
2459 " If \"number\" is omitted, the first trac is default.\n"
2460 " -N, --native-language Specifiy the your language preference. When the first\n"
2461 " <string> audio track does not match your native language then\n"
2462 " select the first subtitle that does. When used in\n"
2463 " conjunction with --native-dub the audio track is\n"
2464 " changed in preference to subtitles. Provide the\n"
2465 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2466 " --native-dub Used in conjunction with --native-language\n"
2467 " requests that if no audio tracks are selected the\n"
2468 " default selected audio track will be the first one\n"
2469 " that matches the --native-language. If there are no\n"
2470 " matching audio tracks then the first matching\n"
2471 " subtitle track is used instead.\n"
2472 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2473 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2474 " <string> encoded in, separted by commas.\n"
2475 " Use 'iconv -l' for a list of valid\n"
2476 " codesets. If not specified latin1 is assumed\n"
2477 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2478 " <string> separted by commas. If not specified zero is assumed.\n"
2479 " Offsets may be negative.\n"
2480 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2481 " for the SRT file(s) separated by commas. If not specified\n"
2482 " then 'und' is used.\n"
2483 " --srt-default Flag the selected srt as the default subtitle\n"
2484 " <number> to be displayed upon playback. Setting no default\n"
2485 " means no subtitle will be automatically displayed\n"
2486 " If \"number\" is omitted, the first srt is default.\n"
2487 " \"number\" is an 1 based index into the srt-file list\n"
2494 /****************************************************************************
2496 ****************************************************************************/
2497 static void ShowPresets()
2499 printf("\n< Apple\n");
2501 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -X 720 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6\n");
2503 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:subme=6:8x8dct=0:trellis=0\n");
2505 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0\n");
2507 printf("\n + iPad: -e x264 -q 20.0 -r 29.97 --pfr -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -4 -X 1024 --loose-anamorphic -m\n");
2509 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500\n");
2513 printf("\n< Regular\n");
2515 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 --strict-anamorphic -m -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0\n");
2517 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50\n");
2521 printf("\n< Legacy\n");
2523 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2525 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0\n");
2527 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:bframes=0:subme=6:8x8dct=0:trellis=0\n");
2529 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4 -I -X 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subme=6:8x8dct=0:trellis=0\n");
2535 static char * hb_strndup( char * str, int len )
2538 int str_len = strlen( str );
2540 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2541 strncpy( res, str, len );
2546 static char** str_split( char *str, char delem )
2553 if ( str == NULL || str[0] == 0 )
2555 ret = malloc( sizeof(char*) );
2560 // Find number of elements in the string
2563 while ( ( pos = strchr( pos, delem ) ) != NULL )
2569 ret = calloc( ( count + 1 ), sizeof(char*) );
2572 for ( i = 0; i < count - 1; i++ )
2574 end = strchr( pos, delem );
2575 ret[i] = hb_strndup(pos, end - pos);
2578 ret[i] = strdup(pos);
2583 /****************************************************************************
2585 ****************************************************************************/
2586 static int ParseOptions( int argc, char ** argv )
2589 #define PREVIEWS 257
2590 #define START_AT_PREVIEW 258
2591 #define START_AT 259
2595 #define DISPLAY_WIDTH 263
2596 #define PIXEL_ASPECT 264
2598 #define KEEP_DISPLAY_ASPECT 266
2599 #define SUB_BURNED 267
2600 #define SUB_DEFAULT 268
2601 #define NATIVE_DUB 269
2602 #define SRT_FILE 270
2603 #define SRT_CODESET 271
2604 #define SRT_OFFSET 272
2605 #define SRT_LANG 273
2606 #define SRT_DEFAULT 274
2607 #define ROTATE_FILTER 275
2608 #define SCAN_ONLY 276
2609 #define MAIN_FEATURE 277
2613 static struct option long_options[] =
2615 { "help", no_argument, NULL, 'h' },
2616 { "update", no_argument, NULL, 'u' },
2617 { "verbose", optional_argument, NULL, 'v' },
2618 { "cpu", required_argument, NULL, 'C' },
2619 { "no-dvdnav", no_argument, NULL, DVDNAV },
2621 { "format", required_argument, NULL, 'f' },
2622 { "input", required_argument, NULL, 'i' },
2623 { "output", required_argument, NULL, 'o' },
2624 { "large-file", no_argument, NULL, '4' },
2625 { "optimize", no_argument, NULL, 'O' },
2626 { "ipod-atom", no_argument, NULL, 'I' },
2628 { "title", required_argument, NULL, 't' },
2629 { "scan", no_argument, NULL, SCAN_ONLY },
2630 { "main-feature",no_argument, NULL, MAIN_FEATURE },
2631 { "chapters", required_argument, NULL, 'c' },
2632 { "angle", required_argument, NULL, ANGLE },
2633 { "markers", optional_argument, NULL, 'm' },
2634 { "audio", required_argument, NULL, 'a' },
2635 { "mixdown", required_argument, NULL, '6' },
2636 { "drc", required_argument, NULL, 'D' },
2637 { "subtitle", required_argument, NULL, 's' },
2638 { "subtitle-forced", optional_argument, NULL, 'F' },
2639 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2640 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2641 { "srt-file", required_argument, NULL, SRT_FILE },
2642 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2643 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2644 { "srt-lang", required_argument, NULL, SRT_LANG },
2645 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2646 { "native-language", required_argument, NULL,'N' },
2647 { "native-dub", no_argument, NULL, NATIVE_DUB },
2648 { "encoder", required_argument, NULL, 'e' },
2649 { "aencoder", required_argument, NULL, 'E' },
2650 { "two-pass", no_argument, NULL, '2' },
2651 { "deinterlace", optional_argument, NULL, 'd' },
2652 { "deblock", optional_argument, NULL, '7' },
2653 { "denoise", optional_argument, NULL, '8' },
2654 { "detelecine", optional_argument, NULL, '9' },
2655 { "decomb", optional_argument, NULL, '5' },
2656 { "grayscale", no_argument, NULL, 'g' },
2657 { "rotate", optional_argument, NULL, ROTATE_FILTER },
2658 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2659 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2660 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2661 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2662 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2663 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2664 { "modulus", required_argument, NULL, MODULUS },
2665 { "itu-par", no_argument, &itu_par, 1 },
2666 { "width", required_argument, NULL, 'w' },
2667 { "height", required_argument, NULL, 'l' },
2668 { "crop", required_argument, NULL, 'n' },
2670 { "vb", required_argument, NULL, 'b' },
2671 { "quality", required_argument, NULL, 'q' },
2672 { "size", required_argument, NULL, 'S' },
2673 { "ab", required_argument, NULL, 'B' },
2674 { "rate", required_argument, NULL, 'r' },
2675 { "arate", required_argument, NULL, 'R' },
2676 { "x264opts", required_argument, NULL, 'x' },
2677 { "turbo", no_argument, NULL, 'T' },
2678 { "maxHeight", required_argument, NULL, 'Y' },
2679 { "maxWidth", required_argument, NULL, 'X' },
2680 { "preset", required_argument, NULL, 'Z' },
2681 { "preset-list", no_argument, NULL, 'z' },
2683 { "aname", required_argument, NULL, 'A' },
2684 { "color-matrix",required_argument, NULL, 'M' },
2685 { "previews", required_argument, NULL, PREVIEWS },
2686 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2687 { "start-at", required_argument, NULL, START_AT },
2688 { "stop-at", required_argument, NULL, STOP_AT },
2689 { "vfr", no_argument, &cfr, 0 },
2690 { "cfr", no_argument, &cfr, 1 },
2691 { "pfr", no_argument, &cfr, 2 },
2695 int option_index = 0;
2699 cur_optind = optind;
2700 c = getopt_long( argc, argv,
2701 "hv::uC:f:4i:Io:t:c:m::M:a:A:6:s:UF::N:e:E:"
2702 "2dD:7895gOw:l:n:b:q:S:B:r:R:x:TY:X:Z:z",
2703 long_options, &option_index );
2712 /* option was handled entirely in getopt_long */
2721 if( optarg != NULL )
2723 debug = atoi( optarg );
2731 cpu = atoi( optarg );
2736 preset_name = strdup(optarg);
2746 format = strdup( optarg );
2749 input = strdup( optarg );
2751 char *devName = bsd_name_for_path( input ); // alloc
2754 if( device_is_dvd( devName ))
2757 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2758 sprintf( input, "/dev/%s", devName );
2765 output = strdup( optarg );
2778 titleindex = atoi( optarg );
2789 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2791 chapter_start = start;
2794 else if( sscanf( optarg, "%d", &start ) == 1 )
2796 chapter_start = start;
2797 chapter_end = chapter_start;
2801 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2808 angle = atoi( optarg );
2811 if( optarg != NULL )
2813 marker_file = strdup( optarg );
2815 chapter_markers = 1;
2818 if( optarg != NULL )
2820 atracks = strdup( optarg );
2829 if( optarg != NULL )
2831 mixdowns = strdup( optarg );
2835 if( optarg != NULL )
2837 dynamic_range_compression = strdup( optarg );
2841 subtracks = str_split( optarg, ',' );
2844 subforce = str_split( optarg, ',' );
2847 if( optarg != NULL )
2849 subburn = strdup( optarg );
2857 if( optarg != NULL )
2859 subdefault = strdup( optarg );
2867 native_language = strdup( optarg );
2873 srtfile = str_split( optarg, ',' );
2876 srtcodeset = str_split( optarg, ',' );
2879 srtoffset = str_split( optarg, ',' );
2882 srtlang = str_split( optarg, ',' );
2885 if( optarg != NULL )
2887 srtdefault = atoi( optarg );
2898 if( optarg != NULL )
2900 if (!( strcmp( optarg, "fast" ) ))
2902 deinterlace_opt = "-1";
2904 else if (!( strcmp( optarg, "slow" ) ))
2906 deinterlace_opt = "2";
2908 else if (!( strcmp( optarg, "slower" ) ))
2910 deinterlace_opt = "0";
2914 deinterlace_opt = strdup( optarg );
2920 if( optarg != NULL )
2922 deblock_opt = strdup( optarg );
2927 if( optarg != NULL )
2929 if (!( strcmp( optarg, "weak" ) ))
2931 denoise_opt = "2:1:2:3";
2933 else if (!( strcmp( optarg, "medium" ) ))
2935 denoise_opt = "3:2:2:3";
2937 else if (!( strcmp( optarg, "strong" ) ))
2939 denoise_opt = "7:7:5:5";
2943 denoise_opt = strdup( optarg );
2949 if( optarg != NULL )
2951 detelecine_opt = strdup( optarg );
2956 if( optarg != NULL )
2958 decomb_opt = strdup( optarg );
2966 if( optarg != NULL )
2968 rotate_opt = strdup( optarg );
2973 if( optarg != NULL )
2975 sscanf( optarg, "%i", &display_width );
2979 if( optarg != NULL )
2981 sscanf( optarg, "%i:%i", &par_width, &par_height );
2985 if( optarg != NULL )
2987 sscanf( optarg, "%i", &modulus );
2991 if( !strcasecmp( optarg, "ffmpeg" ) )
2993 vcodec = HB_VCODEC_FFMPEG;
2995 else if( !strcasecmp( optarg, "x264" ) )
2997 vcodec = HB_VCODEC_X264;
2999 else if( !strcasecmp( optarg, "x264b13" ) )
3001 vcodec = HB_VCODEC_X264;
3004 else if( !strcasecmp( optarg, "x264b30" ) )
3006 vcodec = HB_VCODEC_X264;
3009 else if( !strcasecmp( optarg, "theora" ) )
3011 vcodec = HB_VCODEC_THEORA;
3015 fprintf( stderr, "invalid codec (%s)\n", optarg );
3020 if( optarg != NULL )
3022 acodecs = strdup( optarg );
3026 width = atoi( optarg );
3029 height = atoi( optarg );
3034 char * tmp = optarg;
3035 for( i = 0; i < 4; i++ )
3039 crop[i] = strtol( tmp, &tmp, 0 );
3048 for( i = 0; i < hb_video_rates_count; i++ )
3050 if( !strcmp( optarg, hb_video_rates[i].string ) )
3052 vrate = hb_video_rates[i].rate;
3058 fprintf( stderr, "invalid framerate %s\n", optarg );
3060 else if ( cfr == 0 )
3067 if( optarg != NULL )
3069 arates = strdup( optarg );
3073 vbitrate = atoi( optarg );
3076 vquality = atof( optarg );
3079 size = atoi( optarg );
3082 if( optarg != NULL )
3084 abitrates = strdup( optarg );
3088 x264opts = strdup( optarg );
3091 turbo_opts_enabled = 1;
3094 maxHeight = atoi( optarg );
3097 maxWidth = atoi (optarg );
3100 if( optarg != NULL )
3102 anames = strdup( optarg );
3106 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
3108 case START_AT_PREVIEW:
3109 start_at_preview = atoi( optarg );
3112 start_at_string = strdup( optarg );
3113 start_at_token = strtok( start_at_string, ":");
3114 if( !strcmp( start_at_token, "frame" ) )
3116 start_at_token = strtok( NULL, ":");
3117 start_at_frame = atoi(start_at_token);
3119 else if( !strcmp( start_at_token, "pts" ) )
3121 start_at_token = strtok( NULL, ":");
3122 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3124 else if( !strcmp( start_at_token, "duration" ) )
3126 start_at_token = strtok( NULL, ":");
3127 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3128 start_at_pts *= 90000LL;
3132 stop_at_string = strdup( optarg );
3133 stop_at_token = strtok( stop_at_string, ":");
3134 if( !strcmp( stop_at_token, "frame" ) )
3136 stop_at_token = strtok( NULL, ":");
3137 stop_at_frame = atoi(stop_at_token);
3139 else if( !strcmp( stop_at_token, "pts" ) )
3141 stop_at_token = strtok( NULL, ":");
3142 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3144 else if( !strcmp( stop_at_token, "duration" ) )
3146 stop_at_token = strtok( NULL, ":");
3147 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3148 stop_at_pts *= 90000LL;
3152 if( atoi( optarg ) == 601 )
3154 else if( atoi( optarg ) == 709 )
3158 fprintf( stderr, "unknown option (%s)\n", argv[cur_optind] );
3166 static int CheckOptions( int argc, char ** argv )
3173 if( input == NULL || *input == '\0' )
3175 fprintf( stderr, "Missing input device. Run %s --help for "
3176 "syntax.\n", argv[0] );
3181 if( titleindex > 0 && !titlescan )
3183 if( output == NULL || *output == '\0' )
3185 fprintf( stderr, "Missing output file name. Run %s --help "
3186 "for syntax.\n", argv[0] );
3192 char * p = strrchr( output, '.' );
3195 if( p && ( !strcasecmp( p, ".mp4" ) ||
3196 !strcasecmp( p, ".m4v" ) ) )
3202 default_acodec = HB_ACODEC_FAAC;
3204 else if( p && !strcasecmp(p, ".mkv" ) )
3207 default_acodec = HB_ACODEC_AC3;
3211 fprintf( stderr, "Output format couldn't be guessed "
3212 "from file name, using default.\n" );
3216 else if( !strcasecmp( format, "mp4" ) ||
3217 !strcasecmp( format, "m4v" ) )
3223 default_acodec = HB_ACODEC_FAAC;
3225 else if( !strcasecmp( format, "mkv" ) )
3228 default_acodec = HB_ACODEC_AC3;
3232 fprintf( stderr, "Invalid output format (%s). Possible "
3233 "choices are mp4, m4v and mkv\n.", format );
3241 static int get_acodec_for_string( char *codec )
3243 if( !strcasecmp( codec, "ac3" ) )
3245 return HB_ACODEC_AC3;
3247 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3249 return HB_ACODEC_DCA;
3251 else if( !strcasecmp( codec, "lame" ) )
3253 return HB_ACODEC_LAME;
3255 else if( !strcasecmp( codec, "faac" ) )
3257 return HB_ACODEC_FAAC;
3259 else if( !strcasecmp( codec, "vorbis") )
3261 return HB_ACODEC_VORBIS;
3264 else if( !strcasecmp( codec, "ca_aac") )
3266 return HB_ACODEC_CA_AAC;
3275 static int is_sample_rate_valid(int rate)
3278 for( i = 0; i < hb_audio_rates_count; i++ )
3280 if (rate == hb_audio_rates[i].rate)
3287 /****************************************************************************
3290 * Returns the BSD device name for the block device that contains the
3291 * passed-in path. Returns NULL on failure.
3292 ****************************************************************************/
3293 static char* bsd_name_for_path(char *path)
3297 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3303 // Get the volume reference number.
3304 FSCatalogInfo catalogInfo;
3305 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3311 FSVolumeRefNum volRefNum = catalogInfo.volume;
3313 // Now let's get the device name
3314 GetVolParmsInfoBuffer volumeParms;
3315 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3321 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3322 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3323 if( volumeParms.vMVersion < 4 )
3328 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3329 if( !volumeParms.vMDeviceID )
3334 return strdup( volumeParms.vMDeviceID );
3337 /****************************************************************************
3340 * Returns whether or not the passed in BSD device represents a DVD, or other
3342 ****************************************************************************/
3343 static int device_is_dvd(char *device)
3345 io_service_t service = get_iokit_service(device);
3346 if( service == IO_OBJECT_NULL )
3350 int result = is_dvd_service(service);
3351 IOObjectRelease(service);
3355 /****************************************************************************
3358 * Returns the IOKit service object for the passed in BSD device name.
3359 ****************************************************************************/
3360 static io_service_t get_iokit_service( char *device )
3362 CFMutableDictionaryRef matchingDict;
3363 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3364 if( matchingDict == NULL )
3366 return IO_OBJECT_NULL;
3368 // Fetch the object with the matching BSD node name. There should only be
3369 // one match, so IOServiceGetMatchingService is used instead of
3370 // IOServiceGetMatchingServices to simplify the code.
3371 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3374 /****************************************************************************
3377 * Returns whether or not the service passed in is a DVD.
3379 * Searches for an IOMedia object that represents the entire (whole) media that
3380 * the volume is on. If the volume is on partitioned media, the whole media
3381 * object will be a parent of the volume's media object. If the media is not
3382 * partitioned, the volume's media object will be the whole media object.
3383 ****************************************************************************/
3384 static int is_dvd_service( io_service_t service )
3386 kern_return_t kernResult;
3389 // Create an iterator across all parents of the service object passed in.
3390 kernResult = IORegistryEntryCreateIterator( service,
3392 kIORegistryIterateRecursively | kIORegistryIterateParents,
3394 if( kernResult != KERN_SUCCESS )
3398 if( iter == IO_OBJECT_NULL )
3403 // A reference on the initial service object is released in the do-while
3404 // loop below, so add a reference to balance.
3405 IOObjectRetain( service );
3410 if( is_whole_media_service( service ) &&
3411 IOObjectConformsTo( service, kIODVDMediaClass) )
3415 IOObjectRelease( service );
3416 } while( !result && (service = IOIteratorNext( iter )) );
3417 IOObjectRelease( iter );
3422 /****************************************************************************
3423 * is_whole_media_service
3425 * Returns whether or not the service passed in is an IOMedia service and
3426 * represents the "whole" media instead of just a partition.
3428 * The whole media object is indicated in the IORegistry by the presence of a
3429 * property with the key "Whole" and value "Yes".
3430 ****************************************************************************/
3431 static int is_whole_media_service( io_service_t service )
3435 if( IOObjectConformsTo( service, kIOMediaClass ) )
3437 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3438 CFSTR( kIOMediaWholeKey ),
3439 kCFAllocatorDefault,
3445 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3446 CFRelease( wholeMedia );
3451 #endif // __APPLE_CC__