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;
129 static uint64_t min_title_duration = 900000LL;
131 /* Exit cleanly on Ctrl-C */
132 static volatile int die = 0;
133 static void SigHandler( int );
136 static void ShowCommands();
137 static void ShowHelp();
138 static void ShowPresets();
140 static int ParseOptions( int argc, char ** argv );
141 static int CheckOptions( int argc, char ** argv );
142 static int HandleEvents( hb_handle_t * h );
144 static int get_acodec_for_string( char *codec );
145 static int is_sample_rate_valid(int rate);
148 static char* bsd_name_for_path(char *path);
149 static int device_is_dvd(char *device);
150 static io_service_t get_iokit_service( char *device );
151 static int is_dvd_service( io_service_t service );
152 static int is_whole_media_service( io_service_t service );
155 /* Only print the "Muxing..." message once */
156 static int show_mux_warning = 1;
158 /****************************************************************************
161 * When using the CLI just display using hb_log as we always did in the past
162 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
163 ****************************************************************************/
164 static void hb_cli_error_handler ( const char *errmsg )
166 fprintf( stderr, "ERROR: %s\n", errmsg );
169 int main( int argc, char ** argv )
175 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
176 * force it to unbuffered otherwise informative output is not easily parsed.
178 #if defined( _WIN32 ) || defined( __MINGW32__ )
179 setvbuf( stdout, NULL, _IONBF, 0 );
180 setvbuf( stderr, NULL, _IONBF, 0 );
183 audios = hb_list_init();
185 /* Parse command line */
186 if( ParseOptions( argc, argv ) ||
187 CheckOptions( argc, argv ) )
192 /* Register our error handler */
193 hb_register_error_handler(&hb_cli_error_handler);
196 h = hb_init( debug, update );
197 hb_dvd_set_dvdnav( dvdnav );
200 fprintf( stderr, "%s - %s - %s\n",
201 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
203 /* Check for update */
206 if( ( build = hb_check_update( h, &version ) ) > -1 )
208 fprintf( stderr, "You are using an old version of "
209 "HandBrake.\nLatest is %s (build %d).\n", version,
214 fprintf( stderr, "Your version of HandBrake is up to "
223 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
224 hb_get_cpu_count( h ) > 1 ? "s" : "" );
227 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
228 cpu > 1 ? "s" : "" );
229 hb_set_cpu_count( h, cpu );
232 /* Exit ASAP on Ctrl-C */
233 signal( SIGINT, SigHandler );
235 /* Feed libhb with a DVD to scan */
236 fprintf( stderr, "Opening %s...\n", input );
240 * We need to scan for all the titles in order to find the main feature
245 hb_scan( h, input, titleindex, preview_count, store_previews, min_title_duration );
250 #if defined( __MINGW32__ )
254 case 0x03: /* ctrl-c */
256 fprintf( stdout, "\nEncoding Quit by user command\n" );
260 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
272 #elif !defined(SYS_BEOS)
282 FD_SET( STDIN_FILENO, &fds );
283 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
290 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
292 if( buf[size] == '\n' )
299 if( size >= 256 || buf[size] == '\n' )
304 fprintf( stdout, "\nEncoding Quit by user command\n" );
308 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
331 if( input ) free( input );
332 if( output ) free( output );
333 if( format ) free( format );
336 while( ( audio = hb_list_item( audios, 0 ) ) )
338 hb_list_rem( audios, audio );
339 if( audio->out.name )
341 free( audio->out.name );
345 hb_list_close( &audios );
347 if( mixdowns ) free( mixdowns );
348 if( dynamic_range_compression ) free( dynamic_range_compression );
349 if( atracks ) free( atracks );
350 if( arates ) free( arates );
351 if( abitrates ) free( abitrates );
352 if( acodecs ) free( acodecs );
353 if( anames ) free( anames );
354 if (native_language ) free (native_language );
355 if( x264opts ) free (x264opts );
356 if( x264opts2 ) free (x264opts2 );
357 if (preset_name) free (preset_name);
358 if( stop_at_string ) free( stop_at_string );
359 if( start_at_string ) free( start_at_string );
361 fprintf( stderr, "HandBrake has exited.\n" );
366 static void ShowCommands()
368 fprintf( stdout, "\nCommands:\n" );
369 fprintf( stdout, " [h]elp Show this message\n" );
370 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
371 fprintf( stdout, " [p]ause Pause encoding\n" );
372 fprintf( stdout, " [r]esume Resume encoding\n" );
375 static void PrintTitleInfo( hb_title_t * title )
377 hb_chapter_t * chapter;
378 hb_subtitle_t * subtitle;
381 fprintf( stderr, "+ title %d:\n", title->index );
382 if ( title->index == title->job->feature )
384 fprintf( stderr, " + Main Feature\n" );
386 if ( title->type == HB_STREAM_TYPE )
388 fprintf( stderr, " + stream: %s\n", title->path );
390 else if ( title->type == HB_DVD_TYPE )
392 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%"PRIu64" blocks)\n",
393 title->vts, title->ttn, title->cell_start, title->cell_end,
394 title->block_count );
396 if (title->angle_count > 1)
397 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
398 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
399 title->hours, title->minutes, title->seconds );
400 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
401 title->width, title->height,
402 title->pixel_aspect_width,
403 title->pixel_aspect_height,
404 (float) title->aspect,
405 (float) title->rate / title->rate_base );
406 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
407 title->crop[1], title->crop[2], title->crop[3] );
408 fprintf( stderr, " + chapters:\n" );
409 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
411 chapter = hb_list_item( title->list_chapter, i );
412 fprintf( stderr, " + %d: cells %d->%d, %"PRIu64" blocks, duration "
413 "%02d:%02d:%02d\n", chapter->index,
414 chapter->cell_start, chapter->cell_end,
415 chapter->block_count, chapter->hours, chapter->minutes,
418 fprintf( stderr, " + audio tracks:\n" );
419 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
421 audio = hb_list_audio_config_item( title->list_audio, i );
422 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
424 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
426 audio->lang.description,
427 audio->lang.iso639_2,
428 audio->in.samplerate,
433 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
435 audio->lang.description,
436 audio->lang.iso639_2 );
439 fprintf( stderr, " + subtitle tracks:\n" );
440 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
442 subtitle = hb_list_item( title->list_subtitle, i );
443 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
444 i + 1, subtitle->lang,
446 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
449 if(title->detected_interlacing)
451 /* Interlacing was found in half or more of the preview frames */
452 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
457 static int test_sub_list( char ** list, char * needle, int pos )
461 if ( list == NULL || needle == NULL )
464 if ( list[0] == NULL && pos == 1 )
467 for ( i = 0; list[i] != NULL; i++ )
469 if ( strcasecmp( list[i], needle ) == 0 )
475 static int HandleEvents( hb_handle_t * h )
478 int tmp_num_audio_tracks;
480 hb_get_state( h, &s );
487 #define p s.param.scanning
488 case HB_STATE_SCANNING:
489 /* Show what title is currently being scanned */
490 fprintf( stderr, "Scanning title %d", p.title_cur );
491 if( !titleindex || titlescan )
492 fprintf( stderr, " of %d", p.title_count );
493 fprintf( stderr, "...\n" );
497 case HB_STATE_SCANDONE:
505 /* Audio argument string parsing variables */
509 int mixdown = HB_AMIXDOWN_DOLBYPLII;
511 /* Audio argument string parsing variables */
513 list = hb_get_titles( h );
515 if( !hb_list_count( list ) )
517 /* No valid title, stop right there */
518 fprintf( stderr, "No title found.\n" );
525 int main_feature_idx=0;
526 int main_feature_pos=-1;
527 int main_feature_time=0;
530 fprintf( stderr, "Searching for main feature title...\n" );
532 for( i = 0; i < hb_list_count( list ); i++ )
534 title = hb_list_item( list, i );
535 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
536 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
537 i, title->index, title_time );
538 if( main_feature_time < title_time )
540 main_feature_time = title_time;
541 main_feature_pos = i;
542 main_feature_idx = title->index;
544 if( title->job->feature == title->index )
546 main_feature_time = title_time;
547 main_feature_pos = i;
548 main_feature_idx = title->index;
552 if( main_feature_pos == -1 )
554 fprintf( stderr, "No main feature title found.\n" );
558 titleindex = main_feature_idx;
559 fprintf( stderr, "Found main feature title, setting title to %d\n",
562 title = hb_list_item( list, main_feature_pos);
564 title = hb_list_item( list, 0 );
567 if( !titleindex || titlescan )
569 /* Scan-only mode, print infos and exit */
571 for( i = 0; i < hb_list_count( list ); i++ )
573 title = hb_list_item( list, i );
574 PrintTitleInfo( title );
580 /* Set job settings */
583 PrintTitleInfo( title );
585 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame && !start_at_pts && !start_at_frame )
587 job->chapter_start = MAX( job->chapter_start,
589 job->chapter_end = MIN( job->chapter_end,
591 job->chapter_end = MAX( job->chapter_start,
602 fprintf( stderr, "+ Using preset: %s", preset_name);
604 if (!strcmp(preset_name, "Universal"))
610 vcodec = HB_VCODEC_X264;
611 job->vquality = 20.0;
614 atracks = strdup("1,1");
618 acodecs = strdup("faac,ac3");
622 abitrates = strdup("160,160");
626 mixdowns = strdup("dpl2,auto");
630 arates = strdup("Auto,Auto");
632 if( !dynamic_range_compression )
634 dynamic_range_compression = strdup("0.0,0.0");
639 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6");
641 if( !anamorphic_mode )
645 job->chapter_markers = 1;
649 if (!strcmp(preset_name, "iPod"))
656 vcodec = HB_VCODEC_X264;
660 atracks = strdup("1");
664 acodecs = strdup("faac");
668 abitrates = strdup("160");
672 mixdowns = strdup("dpl2");
676 arates = strdup("Auto");
678 if( !dynamic_range_compression )
680 dynamic_range_compression = strdup("0.0");
685 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");
687 job->chapter_markers = 1;
691 if (!strcmp(preset_name, "iPhone & iPod Touch"))
697 vcodec = HB_VCODEC_X264;
698 job->vquality = 20.0;
701 atracks = strdup("1");
705 acodecs = strdup("faac");
709 abitrates = strdup("128");
713 mixdowns = strdup("dpl2");
717 arates = strdup("Auto");
719 if( !dynamic_range_compression )
721 dynamic_range_compression = strdup("0.0");
726 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0");
728 job->chapter_markers = 1;
732 if (!strcmp(preset_name, "iPad"))
738 job->largeFileSize = 1;
739 vcodec = HB_VCODEC_X264;
740 job->vquality = 20.0;
741 job->vrate_base = 900900;
745 atracks = strdup("1");
749 acodecs = strdup("faac");
753 abitrates = strdup("160");
757 mixdowns = strdup("dpl2");
761 arates = strdup("Auto");
763 if( !dynamic_range_compression )
765 dynamic_range_compression = strdup("0.0");
768 if( !anamorphic_mode )
772 job->chapter_markers = 1;
776 if (!strcmp(preset_name, "AppleTV"))
782 job->largeFileSize = 1;
783 vcodec = HB_VCODEC_X264;
784 job->vquality = 20.0;
787 atracks = strdup("1,1");
791 acodecs = strdup("faac,ac3");
795 abitrates = strdup("160,160");
799 mixdowns = strdup("dpl2,auto");
803 arates = strdup("Auto,Auto");
805 if( !dynamic_range_compression )
807 dynamic_range_compression = strdup("0.0,0.0");
812 x264opts = strdup("cabac=0:ref=2:me=umh:b-pyramid=none:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500");
814 if( !anamorphic_mode )
818 job->chapter_markers = 1;
822 if (!strcmp(preset_name, "Normal"))
828 vcodec = HB_VCODEC_X264;
829 job->vquality = 20.0;
832 atracks = strdup("1");
836 acodecs = strdup("faac");
840 abitrates = strdup("160");
844 mixdowns = strdup("dpl2");
848 arates = strdup("Auto");
850 if( !dynamic_range_compression )
852 dynamic_range_compression = strdup("0.0");
856 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
858 if( !anamorphic_mode )
862 job->chapter_markers = 1;
866 if (!strcmp(preset_name, "High Profile"))
872 vcodec = HB_VCODEC_X264;
873 job->vquality = 20.0;
876 atracks = strdup("1,1");
880 acodecs = strdup("faac,ac3");
884 abitrates = strdup("160,160");
888 mixdowns = strdup("dpl2,auto");
892 arates = strdup("Auto,Auto");
894 if( !dynamic_range_compression )
896 dynamic_range_compression = strdup("0.0,0.0");
900 x264opts = strdup("b-adapt=2:rc-lookahead=50");
904 if( !anamorphic_mode )
908 job->chapter_markers = 1;
912 if (!strcmp(preset_name, "Classic"))
918 job->vbitrate = 1000;
921 atracks = strdup("1");
925 acodecs = strdup("faac");
929 abitrates = strdup("160");
933 mixdowns = strdup("dpl2");
937 arates = strdup("Auto");
939 if( !dynamic_range_compression )
941 dynamic_range_compression = strdup("0.0");
946 if (!strcmp(preset_name, "AppleTV Legacy"))
952 job->largeFileSize = 1;
953 vcodec = HB_VCODEC_X264;
954 job->vbitrate = 2500;
957 atracks = strdup("1,1");
961 acodecs = strdup("faac,ac3");
965 abitrates = strdup("160,160");
969 mixdowns = strdup("dpl2,auto");
973 arates = strdup("Auto,Auto");
975 if( !dynamic_range_compression )
977 dynamic_range_compression = strdup("0.0,0.0");
981 x264opts = strdup("ref=1:b-pyramid=none:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
983 if( !anamorphic_mode )
987 job->chapter_markers = 1;
991 if (!strcmp(preset_name, "iPhone Legacy"))
998 vcodec = HB_VCODEC_X264;
1002 atracks = strdup("1");
1006 acodecs = strdup("faac");
1010 abitrates = strdup("128");
1014 mixdowns = strdup("dpl2");
1018 arates = strdup("Auto");
1020 if( !dynamic_range_compression )
1022 dynamic_range_compression = strdup("0.0");
1027 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");
1029 job->chapter_markers = 1;
1033 if (!strcmp(preset_name, "iPod Legacy"))
1040 vcodec = HB_VCODEC_X264;
1041 job->vbitrate = 1500;
1044 atracks = strdup("1");
1048 acodecs = strdup("faac");
1052 abitrates = strdup("160");
1056 mixdowns = strdup("dpl2");
1060 arates = strdup("Auto");
1062 if( !dynamic_range_compression )
1064 dynamic_range_compression = strdup("0.0");
1069 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");
1071 job->chapter_markers = 1;
1077 if ( chapter_markers )
1079 job->chapter_markers = chapter_markers;
1081 if( marker_file != NULL )
1083 hb_csv_file_t * file = hb_open_csv_file( marker_file );
1084 hb_csv_cell_t * cell;
1088 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1092 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1096 /* Parse the cells */
1097 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1099 /* We have a chapter number */
1100 if( cell->cell_col == 0 )
1102 row = cell->cell_row;
1103 chapter = atoi( cell->cell_text );
1106 /* We have a chapter name */
1107 if( cell->cell_col == 1 && row == cell->cell_row )
1109 /* If we have a valid chapter, copy the string an terminate it */
1110 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1112 hb_chapter_t * chapter_s;
1114 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1115 strncpy(chapter_s->title, cell->cell_text, 1023);
1116 chapter_s->title[1023] = '\0';
1121 hb_dispose_cell( cell );
1124 hb_close_csv_file( file );
1129 /* No marker file */
1131 int number_of_chapters = hb_list_count(job->title->list_chapter);
1134 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1136 hb_chapter_t * chapter_s;
1137 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1138 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1139 chapter_s->title[1023] = '\0';
1144 if( crop[0] >= 0 && crop[1] >= 0 &&
1145 crop[2] >= 0 && crop[3] >= 0 )
1147 memcpy( job->crop, crop, 4 * sizeof( int ) );
1150 job->deinterlace = deinterlace;
1151 job->grayscale = grayscale;
1153 /* Add selected filters */
1154 job->filters = hb_list_init();
1158 hb_filter_rotate.settings = rotate_opt;
1159 hb_list_add( job->filters, &hb_filter_rotate);
1163 hb_filter_detelecine.settings = detelecine_opt;
1164 hb_list_add( job->filters, &hb_filter_detelecine );
1168 hb_filter_decomb.settings = decomb_opt;
1169 hb_list_add( job->filters, &hb_filter_decomb );
1173 hb_filter_deinterlace.settings = deinterlace_opt;
1174 hb_list_add( job->filters, &hb_filter_deinterlace );
1178 hb_filter_deblock.settings = deblock_opt;
1179 hb_list_add( job->filters, &hb_filter_deblock );
1183 hb_filter_denoise.settings = denoise_opt;
1184 hb_list_add( job->filters, &hb_filter_denoise );
1187 switch( anamorphic_mode )
1189 case 0: // Non-anamorphic
1191 if( width && height )
1194 job->height = height;
1199 hb_fix_aspect( job, HB_KEEP_WIDTH );
1203 job->height = height;
1204 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1206 else if( !width && !height )
1208 hb_fix_aspect( job, HB_KEEP_WIDTH );
1213 case 1: // Strict anammorphic
1214 job->anamorphic.mode = anamorphic_mode;
1217 case 2: // Loose anamorphic
1218 job->anamorphic.mode = 2;
1222 job->modulus = modulus;
1227 job->anamorphic.itu_par = itu_par;
1234 else if( !width && !height )
1236 /* Default to full width when one isn't specified for loose anamorphic */
1237 job->width = title->width - job->crop[2] - job->crop[3];
1242 case 3: // Custom Anamorphic 3: Power User Jamboree
1243 job->anamorphic.mode = 3;
1247 job->modulus = modulus;
1252 job->anamorphic.itu_par = itu_par;
1255 if( par_width && par_height )
1257 job->anamorphic.par_width = par_width;
1258 job->anamorphic.par_height = par_height;
1261 if( keep_display_aspect )
1263 job->anamorphic.keep_display_aspect = 1;
1265 /* First, what *is* the display aspect? */
1266 int cropped_width = title->width - job->crop[2] - job->crop[3];
1267 int cropped_height = title->height - job->crop[0] - job->crop[1];
1269 /* XXX -- I'm assuming people want to keep the source
1270 display AR even though they might have already
1271 asked for ITU values instead. */
1272 float source_display_width = (float)cropped_width *
1273 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1274 float display_aspect = source_display_width / (float)cropped_height;
1275 /* When keeping display aspect, we have to rank some values
1276 by priority in order to consistently handle situations
1277 when more than one might be specified by default.
1279 * First off, PAR gets ignored. (err make this reality)
1280 * Height will be respected over all other settings,
1281 * If it isn't set, display_width will be followed.
1282 * If it isn't set, width will be followed. */
1285 /* We scale the display width to the new height */
1286 display_width = (int)( (double)height * display_aspect );
1288 else if( display_width )
1290 /* We scale the height to the new display width */
1291 height = (int)( (double)display_width / display_aspect );
1297 /* Adjust the PAR to create the new display width
1298 from the default job width. */
1299 job->anamorphic.dar_width = display_width;
1301 job->anamorphic.dar_height = height ?
1303 title->height - job->crop[0] - job->crop[1];
1306 if( width && height )
1308 /* Use these storage dimensions */
1310 job->height = height;
1314 /* Use just this storage width */
1316 job->height = title->height - job->crop[0] - job->crop[1];
1320 /* Use just this storage height. */
1321 job->height = height;
1322 job->width = title->width - job->crop[2] - job->crop[3];
1324 else if( !width && !height )
1326 /* Assume source dimensions after cropping. */
1327 job->width = title->width - job->crop[2] - job->crop[3];
1328 job->height = title->height - job->crop[0] - job->crop[1];
1334 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1336 job->vquality = vquality;
1341 job->vquality = -1.0;
1342 job->vbitrate = vbitrate;
1346 job->vcodec = vcodec;
1350 job->h264_level = 13;
1354 job->h264_level = 30;
1359 job->vrate = 27000000;
1360 job->vrate_base = vrate;
1364 // cfr or pfr flag with no rate specified implies
1365 // use the title rate.
1367 job->vrate = title->rate;
1368 job->vrate_base = title->rate_base;
1371 /* Grab audio tracks */
1374 char * token = strtok(atracks, ",");
1377 int track_start, track_end;
1378 while( token != NULL )
1380 audio = calloc(1, sizeof(*audio));
1381 hb_audio_config_init(audio);
1382 if (strlen(token) >= 3)
1384 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1387 for (i = track_start - 1; i < track_end; i++)
1389 if (i != track_start - 1)
1391 audio = calloc(1, sizeof(*audio));
1392 hb_audio_config_init(audio);
1394 audio->in.track = i;
1395 audio->out.track = num_audio_tracks++;
1396 hb_list_add(audios, audio);
1399 else if( !strcasecmp(token, "none" ) )
1401 audio->in.track = audio->out.track = -1;
1402 audio->out.codec = 0;
1403 hb_list_add(audios, audio);
1408 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1415 audio->in.track = atoi(token) - 1;
1416 audio->out.track = num_audio_tracks++;
1417 hb_list_add(audios, audio);
1419 token = strtok(NULL, ",");
1423 /* Parse audio tracks */
1424 if( native_language && native_dub )
1426 if( hb_list_count( audios ) == 0 || !audio_explicit )
1428 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1433 audio = hb_list_audio_config_item( title->list_audio, i );
1435 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1437 if( strncasecmp( native_language, audio_lang,
1438 sizeof( audio_lang ) ) == 0 &&
1439 audio->lang.type != 3 && // Directors 1
1440 audio->lang.type != 4) // Directors 2
1443 * Matched an audio to our native language - use it.
1444 * Replace any existing audio tracks that a preset may
1447 if( hb_list_count(audios) == 0) {
1448 audio = calloc(1, sizeof(*audio));
1449 hb_audio_config_init(audio);
1450 audio->in.track = track;
1451 audio->out.track = num_audio_tracks++;
1452 /* Add it to our audios */
1453 hb_list_add(audios, audio);
1456 * Update the track numbers on what is already in
1459 for( i=0; i < hb_list_count( audios ); i++ )
1461 audio = hb_list_item( audios, i );
1463 audio->in.track = track;
1470 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1474 if( hb_list_count(audios) == 0 &&
1475 hb_list_count(job->title->list_audio) > 0 )
1477 /* Create a new audio track with default settings */
1478 audio = calloc(1, sizeof(*audio));
1479 hb_audio_config_init(audio);
1480 /* Add it to our audios */
1481 hb_list_add(audios, audio);
1484 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1485 for (i = 0; i < tmp_num_audio_tracks; i++)
1487 audio = hb_list_item(audios, 0);
1488 if( (audio == NULL) || (audio->in.track == -1) ||
1489 (audio->out.track == -1) || (audio->out.codec == 0) )
1495 if( hb_audio_add( job, audio ) == 0 )
1497 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1498 audio->in.track + 1 );
1503 hb_list_rem(audios, audio);
1505 if( audio->out.name )
1507 free( audio->out.name);
1516 char * token = strtok(acodecs, ",");
1519 while ( token != NULL )
1521 if ((acodec = get_acodec_for_string(token)) == -1)
1523 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1524 acodec = default_acodec;
1526 if( i < num_audio_tracks )
1528 audio = hb_list_audio_config_item(job->list_audio, i);
1529 audio->out.codec = acodec;
1533 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1534 hb_audio_config_t audio;
1538 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1540 hb_audio_config_init(&audio);
1541 audio.in.track = last_audio->in.track;
1542 audio.out.track = num_audio_tracks++;
1543 audio.out.codec = acodec;
1544 hb_audio_add(job, &audio);
1548 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1551 token = strtok(NULL, ",");
1555 if( i < num_audio_tracks )
1557 /* We have fewer inputs than audio tracks, use the default codec for
1558 * this container for the remaining tracks. Unless we only have one input
1559 * then use that codec instead.
1562 acodec = default_acodec;
1563 for ( ; i < num_audio_tracks; i++)
1565 audio = hb_list_audio_config_item(job->list_audio, i);
1566 audio->out.codec = acodec;
1575 char * token = strtok(arates, ",");
1578 while ( token != NULL )
1580 arate = atoi(token);
1581 audio = hb_list_audio_config_item(job->list_audio, i);
1584 for( j = 0; j < hb_audio_rates_count; j++ )
1586 if( !strcmp( token, hb_audio_rates[j].string ) )
1588 arate = hb_audio_rates[j].rate;
1595 if (!is_sample_rate_valid(arate))
1597 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1598 arate = audio->in.samplerate;
1601 audio->out.samplerate = arate;
1602 if( (++i) >= num_audio_tracks )
1603 break; /* We have more inputs than audio tracks, oops */
1607 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1609 token = strtok(NULL, ",");
1612 if (i < num_audio_tracks)
1614 /* We have fewer inputs than audio tracks, use default sample rate.
1615 * Unless we only have one input, then use that for all tracks.
1618 arate = audio->in.samplerate;
1619 for ( ; i < num_audio_tracks; i++)
1621 audio = hb_list_audio_config_item(job->list_audio, i);
1622 audio->out.samplerate = arate;
1631 char * token = strtok(abitrates, ",");
1634 while ( token != NULL )
1636 abitrate = atoi(token);
1637 audio = hb_list_audio_config_item(job->list_audio, i);
1641 audio->out.bitrate = abitrate;
1642 if( (++i) >= num_audio_tracks )
1643 break; /* We have more inputs than audio tracks, oops */
1647 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1649 token = strtok(NULL, ",");
1652 if (i < num_audio_tracks)
1654 /* We have fewer inputs than audio tracks, use the default bitrate
1655 * for the remaining tracks. Unless we only have one input, then use
1656 * that for all tracks.
1659 abitrate = default_abitrate;
1660 for (; i < num_audio_tracks; i++)
1662 audio = hb_list_audio_config_item(job->list_audio, i);
1663 audio->out.bitrate = abitrate;
1670 if ( dynamic_range_compression )
1672 char * token = strtok(dynamic_range_compression, ",");
1674 token = dynamic_range_compression;
1675 while ( token != NULL )
1677 d_r_c = atof(token);
1678 audio = hb_list_audio_config_item(job->list_audio, i);
1681 audio->out.dynamic_range_compression = d_r_c;
1682 if( (++i) >= num_audio_tracks )
1683 break; /* We have more inputs than audio tracks, oops */
1687 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1689 token = strtok(NULL, ",");
1692 if (i < num_audio_tracks)
1694 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1695 * tracks. Unless we only have one input, then use the same DRC for all
1700 for (; i < num_audio_tracks; i++)
1702 audio = hb_list_audio_config_item(job->list_audio, i);
1703 audio->out.dynamic_range_compression = d_r_c;
1712 char * token = strtok(mixdowns, ",");
1715 while ( token != NULL )
1717 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1718 audio = hb_list_audio_config_item(job->list_audio, i);
1721 audio->out.mixdown = mixdown;
1722 if( (++i) >= num_audio_tracks )
1723 break; /* We have more inputs than audio tracks, oops */
1727 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1729 token = strtok(NULL, ",");
1732 if (i < num_audio_tracks)
1734 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1735 * we only have one input, then use that.
1738 mixdown = HB_AMIXDOWN_DOLBYPLII;
1739 for (; i < num_audio_tracks; i++)
1741 audio = hb_list_audio_config_item(job->list_audio, i);
1742 audio->out.mixdown = mixdown;
1747 /* Audio Track Names */
1751 char * token = strtok(anames, ",");
1754 while ( token != NULL )
1756 audio = hb_list_audio_config_item(job->list_audio, i);
1759 audio->out.name = strdup(token);
1760 if( (++i) >= num_audio_tracks )
1761 break; /* We have more names than audio tracks, oops */
1765 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1768 token = strtok(NULL, ",");
1771 if( i < num_audio_tracks && i == 1 )
1773 /* We have exactly one name and more than one audio track. Use the same
1774 * name for all tracks. */
1775 for ( ; i < num_audio_tracks; i++)
1777 audio = hb_list_audio_config_item(job->list_audio, i);
1778 audio->out.name = strdup(anames);
1781 /* Audio Track Names */
1785 job->vbitrate = hb_calc_bitrate( job, size );
1786 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1796 for ( i = 0; subtracks[i] != NULL; i++ )
1799 token = subtracks[i];
1800 if( strcasecmp(token, "scan" ) == 0 )
1802 int burn = 0, force = 0, def = 0;
1804 if ( subburn != NULL )
1806 burn = ( pos == 1 && subburn[0] == 0 ) ||
1807 ( strcmp( "scan", subburn ) == 0 );
1809 if ( subdefault != NULL )
1811 def = ( pos == 1 && subdefault[0] == 0 ) ||
1812 ( strcmp( "scan", subdefault ) == 0 );
1814 force = test_sub_list( subforce, "scan", pos );
1818 job->select_subtitle_config.dest = PASSTHRUSUB;
1828 job->select_subtitle_config.force = force;
1829 job->select_subtitle_config.default_track = def;
1834 hb_subtitle_t * subtitle;
1835 hb_subtitle_config_t sub_config;
1837 int burn = 0, force = 0, def = 0;
1839 track = atoi(token) - 1;
1840 subtitle = hb_list_item(title->list_subtitle, track);
1841 if( subtitle == NULL )
1843 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1846 sub_config = subtitle->config;
1848 if ( subburn != NULL )
1850 burn = ( pos == 1 && subburn[0] == 0 ) ||
1851 ( strcmp( token, subburn ) == 0 );
1853 if ( subdefault != NULL )
1855 def = ( pos == 1 && subdefault[0] == 0 ) ||
1856 ( strcmp( token, subdefault ) == 0 );
1859 force = test_sub_list(subforce, token, pos);
1861 if ( !burn && subtitle->format == PICTURESUB)
1863 sub_config.dest = PASSTHRUSUB;
1865 else if ( burn && subtitle->format == PICTURESUB )
1867 // Only allow one subtitle to be burned into video
1870 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1875 sub_config.force = force;
1876 sub_config.default_track = def;
1877 hb_subtitle_add( job, &sub_config, track );
1886 hb_subtitle_config_t sub_config;
1889 for( i=0; srtfile[i] != NULL; i++ )
1891 char *codeset = "L1";
1897 if( srtcodeset && srtcodeset[i] )
1899 codeset = srtcodeset[i];
1901 if( srtoffset && srtoffset[i] )
1903 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1905 if ( srtlang && srtlang[i] )
1909 sub_config.default_track =
1910 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1911 sub_config.force = 0;
1912 strncpy( sub_config.src_filename, srtfile[i], 128);
1913 strncpy( sub_config.src_codeset, codeset, 40);
1914 sub_config.offset = offset;
1916 hb_srt_add( job, &sub_config, lang);
1920 if( native_language )
1924 audio = hb_list_audio_config_item(job->list_audio, 0);
1928 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1930 if( strncasecmp( native_language, audio_lang,
1931 sizeof( audio_lang ) ) != 0 )
1934 * Audio language is not the same as our native language.
1935 * If we have any subtitles in our native language they
1936 * should be selected here if they haven't already been.
1938 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1939 int matched_track = 0;
1941 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1943 subtitle = hb_list_item( title->list_subtitle, i );
1945 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1948 * Found the first matching subtitle in our
1949 * native language. Is it already selected?
1951 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1953 subtitle2 = hb_list_item( job->list_subtitle, i );
1955 if( subtitle2->track == subtitle->track) {
1964 if( subtitle2 == NULL )
1967 * Not already selected, so select it.
1969 hb_subtitle_config_t sub_config;
1973 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1975 sub_config = subtitle->config;
1977 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1979 sub_config.dest = PASSTHRUSUB;
1982 sub_config.force = 0;
1983 sub_config.default_track = 1;
1984 hb_subtitle_add( job, &sub_config, matched_track);
2001 if ( largeFileSize )
2003 job->largeFileSize = 1;
2007 job->mp4_optimize = 1;
2014 job->file = strdup( output );
2018 job->color_matrix = color_matrix;
2021 if( x264opts != NULL && *x264opts != '\0' )
2023 job->x264opts = x264opts;
2025 else /*avoids a bus error crash when options aren't specified*/
2027 job->x264opts = NULL;
2030 job->maxWidth = maxWidth;
2032 job->maxHeight = maxHeight;
2034 if( start_at_preview )
2036 job->start_at_preview = start_at_preview - 1;
2037 job->seek_points = preview_count;
2042 job->pts_to_stop = stop_at_pts;
2048 job->frame_to_stop = stop_at_frame;
2054 job->pts_to_start = start_at_pts;
2058 if( start_at_frame )
2060 job->frame_to_start = start_at_frame;
2069 * When subtitle scan is enabled do a fast pre-scan job
2070 * which will determine which subtitles to enable, if any.
2074 x264opts_tmp = job->x264opts;
2076 job->x264opts = NULL;
2078 job->indepth_scan = subtitle_scan;
2079 fprintf( stderr, "Subtitle Scan Enabled - enabling "
2080 "subtitles if found for foreign language segments\n");
2083 * Add the pre-scan job
2087 job->x264opts = x264opts_tmp;
2093 * If subtitle_scan is enabled then only turn it on
2094 * for the first pass and then off again for the
2099 job->indepth_scan = 0;
2103 x264opts2 = strdup(x264opts);
2107 * If turbo options have been selected then append them
2108 * to the x264opts now (size includes one ':' and the '\0')
2110 if( turbo_opts_enabled )
2112 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2115 tmp_x264opts = malloc(size * sizeof(char));
2118 snprintf( tmp_x264opts, size, "%s:%s",
2119 x264opts, turbo_opts );
2123 * No x264opts to modify, but apply the turbo options
2124 * anyway as they may be modifying defaults
2126 snprintf( tmp_x264opts, size, "%s",
2129 x264opts = tmp_x264opts;
2131 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2134 job->x264opts = x264opts;
2140 * On the second pass we turn off subtitle scan so that we
2141 * can actually encode using any subtitles that were auto
2142 * selected in the first pass (using the whacky select-subtitle
2143 * attribute of the job).
2145 job->indepth_scan = 0;
2147 job->x264opts = x264opts2;
2154 * Turn on subtitle scan if requested, note that this option
2155 * precludes encoding of any actual subtitles.
2158 job->indepth_scan = 0;
2166 #define p s.param.working
2167 case HB_STATE_SEARCHING:
2168 fprintf( stdout, "\rEncoding: task %d of %d, Searching for start time, %.2f %%",
2169 p.job_cur, p.job_count, 100.0 * p.progress );
2170 if( p.seconds > -1 )
2172 fprintf( stdout, " (ETA %02dh%02dm%02ds)",
2173 p.hours, p.minutes, p.seconds );
2178 case HB_STATE_WORKING:
2179 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2180 p.job_cur, p.job_count, 100.0 * p.progress );
2181 if( p.seconds > -1 )
2183 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2184 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2185 p.hours, p.minutes, p.seconds );
2191 #define p s.param.muxing
2192 case HB_STATE_MUXING:
2194 if (show_mux_warning)
2196 fprintf( stdout, "\rMuxing: this may take awhile..." );
2198 show_mux_warning = 0;
2204 #define p s.param.workdone
2205 case HB_STATE_WORKDONE:
2206 /* Print error if any, then exit */
2210 fprintf( stderr, "\nRip done!\n" );
2212 case HB_ERROR_CANCELED:
2213 fprintf( stderr, "\nRip canceled.\n" );
2216 fprintf( stderr, "\nRip failed (error %x).\n",
2226 /****************************************************************************
2228 ****************************************************************************/
2229 static volatile int64_t i_die_date = 0;
2230 void SigHandler( int i_signal )
2235 i_die_date = hb_get_date();
2236 fprintf( stderr, "Signal %d received, terminating - do it "
2237 "again in case it gets stuck\n", i_signal );
2239 else if( i_die_date + 500 < hb_get_date() )
2241 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2246 /****************************************************************************
2248 ****************************************************************************/
2249 static void ShowHelp()
2252 FILE* const out = stdout;
2255 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2257 "### General Handbrake Options------------------------------------------------\n\n"
2258 " -h, --help Print help\n"
2259 " -u, --update Check for updates and exit\n"
2260 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2261 " -C, --cpu Set CPU count (default: autodetected)\n"
2262 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2263 " if the preset name has spaces, surround it with\n"
2264 " double quotation marks\n"
2265 " -z, --preset-list See a list of available built-in presets\n"
2266 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2267 " (experimental, enabled by default for testing)\n"
2270 "### Source Options-----------------------------------------------------------\n\n"
2271 " -i, --input <string> Set input device\n"
2272 " -t, --title <number> Select a title to encode (0 to scan all titles only,\n"
2274 " --scan Scan selected title only.\n"
2275 " --main-feature Detect and select the main feature title.\n"
2276 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2277 " 1 to 3, or \"3\" for chapter 3 only,\n"
2278 " default: all chapters)\n"
2279 " --angle <number> Select the DVD angle\n"
2280 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2281 " and whether or not they're stored to disk (0 or 1).\n"
2282 " (default: 10:0)\n"
2283 " --start-at-preview <#> Start encoding at a given preview.\n"
2284 " --start-at <unit:#> Start encoding at a given frame, duration (in seconds),\n"
2285 " or pts (on a 90kHz clock)\n"
2286 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2287 " or pts (on a 90kHz clock)"
2290 "### Destination Options------------------------------------------------------\n\n"
2291 " -o, --output <string> Set output file name\n"
2292 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2293 " autodetected from file name)\n"
2294 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2295 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2296 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2297 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2298 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2302 "### Video Options------------------------------------------------------------\n\n"
2303 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2304 " (default: ffmpeg)\n"
2305 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2306 " same style as mencoder:\n"
2307 " option1=value1:option2=value2\n"
2308 " -q, --quality <number> Set video quality\n"
2309 " -S, --size <MB> Set target size\n"
2310 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2311 " -2, --two-pass Use two-pass mode\n"
2312 " -T, --turbo When using 2-pass use the turbo options\n"
2313 " on the first pass to improve speed\n"
2314 " (only works with x264, affects PSNR by about 0.05dB,\n"
2315 " and increases first pass speed two to four times)\n"
2316 " -r, --rate Set video framerate (" );
2317 for( i = 0; i < hb_video_rates_count; i++ )
2319 fprintf( out, hb_video_rates[i].string );
2320 if( i != hb_video_rates_count - 1 )
2321 fprintf( out, "/" );
2324 " Be aware that not specifying a framerate lets\n"
2325 " HandBrake preserve a source's time stamps,\n"
2326 " potentially creating variable framerate video\n"
2327 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2328 " frame rate control. VFR preserves the source\n"
2329 " timing. CFR makes the output constant rate at\n"
2330 " the rate given by the -r flag (or the source's\n"
2331 " average rate if no -r is given). PFR doesn't\n"
2332 " allow the rate to go over the rate specified\n"
2333 " with the -r flag but won't change the source\n"
2334 " timing if it's below that rate.\n"
2335 " If none of these flags are given, the default\n"
2336 " is --cfr when -r is given and --vfr otherwise\n"
2339 "### Audio Options-----------------------------------------------------------\n\n"
2340 " -a, --audio <string> Select audio track(s), separated by commas\n"
2341 " More than one output track can be used for one\n"
2343 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2344 " tracks, default: first one)\n" );
2348 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2349 " ac3 and dts meaning passthrough\n"
2350 " Separated by commas for more than one audio track.\n"
2351 " (default: guessed)\n" );
2354 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2355 " ac3 and dts meaning passthrough\n"
2356 " Separated by commas for more than one audio track.\n"
2357 " (default: guessed)\n" );
2360 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2361 " Separated by commas for more than one audio track.\n"
2362 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2363 " Separated by commas for more than one audio track.\n"
2364 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2365 " -R, --arate Set audio samplerate(s) (" );
2366 for( i = 0; i < hb_audio_rates_count; i++ )
2368 fprintf( out, hb_audio_rates[i].string );
2369 if( i != hb_audio_rates_count - 1 )
2370 fprintf( out, "/" );
2372 fprintf( out, " kHz)\n"
2373 " Separated by commas for more than one audio track.\n"
2374 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2375 " making soft sounds louder. Range is 1.0 to 4.0\n"
2376 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2377 " Separated by commas for more than one audio track.\n"
2378 " -A, --aname <string> Audio track name(s),\n"
2379 " Separated by commas for more than one audio track.\n"
2382 "### Picture Settings---------------------------------------------------------\n\n"
2383 " -w, --width <number> Set picture width\n"
2384 " -l, --height <number> Set picture height\n"
2385 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2386 " -Y, --maxHeight <#> Set maximum height\n"
2387 " -X, --maxWidth <#> Set maximum width\n"
2388 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2389 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2390 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2391 " directly control all parameters.\n"
2392 " --display-width Set the width to scale the actual pixels to\n"
2393 " <number> at playback, for custom anamorphic.\n"
2394 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2395 " when using custom anamorphic\n"
2396 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2398 " (--display-width and --pixel-aspect are mutually\n"
2399 " exclusive and the former will override the latter)\n"
2400 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2401 " custom anamorphic, useful with underscanned sources\n"
2402 " --modulus Set the number you want the scaled pixel dimensions\n"
2403 " <number> to divide cleanly by. Does not affect strict\n"
2404 " anamorphic mode, which is always mod 2 (default: 16)\n"
2405 " -M --color-matrix Set the color space signaled by the output\n"
2406 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2407 " default: set by resolution)\n"
2410 "### Filters---------------------------------------------------------\n\n"
2412 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2413 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2415 " <fast/slow/slower>\n"
2416 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2417 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2418 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2419 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2420 " Note: this filter drops duplicate frames to\n"
2421 " restore the pre-telecine framerate, unless you\n"
2422 " specify a constant framerate (--rate 29.97)\n"
2423 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2424 " -8, --denoise Denoise video with hqdn3d filter\n"
2425 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2427 " <weak/medium/strong>\n"
2428 " -7, --deblock Deblock video with pp7 filter\n"
2429 " <QP:M> (default 5:2)\n"
2430 " --rotate Flips images axes\n"
2431 " <M> (default 3)\n"
2432 " -g, --grayscale Grayscale encoding\n"
2435 "### Subtitle Options------------------------------------------------------------\n\n"
2436 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2437 " More than one output track can be used for one\n"
2439 " (\"1,2,3\" for multiple tracks.\n"
2440 " A special track name \"scan\" adds an extra 1st pass.\n"
2441 " This extra pass scans subtitles matching the\n"
2442 " language of the first audio or the language \n"
2443 " selected by --native-language.\n"
2444 " The one that's only used 10 percent of the time\n"
2445 " or less is selected. This should locate subtitles\n"
2446 " for short foreign language segments. Best used in\n"
2447 " conjunction with --subtitle-forced.\n"
2448 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2449 " <string> the subtitle has the forced flag set. May be used in\n"
2450 " conjunction with \"scan\" track to auto-select\n"
2451 " a stream if it contains forced subtitles.\n"
2452 " Separated by commas for more than one audio track.\n"
2453 " (\"1,2,3\" for multiple tracks.\n"
2454 " If \"string\" is omitted, the first trac is forced.\n"
2455 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2456 " <number> If \"number\" is omitted, the first trac is burned.\n"
2457 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2458 " <number> to be displayed upon playback. Setting no default\n"
2459 " means no subtitle will be automatically displayed\n"
2460 " If \"number\" is omitted, the first trac is default.\n"
2461 " -N, --native-language Specifiy the your language preference. When the first\n"
2462 " <string> audio track does not match your native language then\n"
2463 " select the first subtitle that does. When used in\n"
2464 " conjunction with --native-dub the audio track is\n"
2465 " changed in preference to subtitles. Provide the\n"
2466 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2467 " --native-dub Used in conjunction with --native-language\n"
2468 " requests that if no audio tracks are selected the\n"
2469 " default selected audio track will be the first one\n"
2470 " that matches the --native-language. If there are no\n"
2471 " matching audio tracks then the first matching\n"
2472 " subtitle track is used instead.\n"
2473 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2474 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2475 " <string> encoded in, separted by commas.\n"
2476 " Use 'iconv -l' for a list of valid\n"
2477 " codesets. If not specified latin1 is assumed\n"
2478 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2479 " <string> separted by commas. If not specified zero is assumed.\n"
2480 " Offsets may be negative.\n"
2481 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2482 " for the SRT file(s) separated by commas. If not specified\n"
2483 " then 'und' is used.\n"
2484 " --srt-default Flag the selected srt as the default subtitle\n"
2485 " <number> to be displayed upon playback. Setting no default\n"
2486 " means no subtitle will be automatically displayed\n"
2487 " If \"number\" is omitted, the first srt is default.\n"
2488 " \"number\" is an 1 based index into the srt-file list\n"
2495 /****************************************************************************
2497 ****************************************************************************/
2498 static void ShowPresets()
2500 printf("\n< Apple\n");
2502 printf("\n + Universal: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R Auto,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");
2504 printf("\n + iPod: -e x264 -b 700 -a 1 -E faac -B 160 -6 dpl2 -R Auto -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");
2506 printf("\n + iPhone & iPod Touch: -e x264 -q 20.0 -a 1 -E faac -B 128 -6 dpl2 -R Auto -D 0.0 -f mp4 -X 480 -m -x cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0\n");
2508 printf("\n + iPad: -e x264 -q 20.0 -r 29.97 --pfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4 -4 -X 1024 --loose-anamorphic -m\n");
2510 printf("\n + AppleTV: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 -X 960 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-pyramid=none:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500\n");
2514 printf("\n< Regular\n");
2516 printf("\n + Normal: -e x264 -q 20.0 -a 1 -E faac -B 160 -6 dpl2 -R Auto -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");
2518 printf("\n + High Profile: -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 --detelecine --decomb --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50\n");
2522 printf("\n< Legacy\n");
2524 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 -f mp4\n");
2526 printf("\n + AppleTV Legacy: -e x264 -b 2500 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 -4 --strict-anamorphic -m -x ref=1:b-pyramid=none:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0\n");
2528 printf("\n + iPhone Legacy: -e x264 -b 960 -a 1 -E faac -B 128 -6 dpl2 -R Auto -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");
2530 printf("\n + iPod Legacy: -e x264 -b 1500 -a 1 -E faac -B 160 -6 dpl2 -R Auto -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");
2536 static char * hb_strndup( char * str, int len )
2539 int str_len = strlen( str );
2541 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2542 strncpy( res, str, len );
2547 static char** str_split( char *str, char delem )
2554 if ( str == NULL || str[0] == 0 )
2556 ret = malloc( sizeof(char*) );
2561 // Find number of elements in the string
2564 while ( ( pos = strchr( pos, delem ) ) != NULL )
2570 ret = calloc( ( count + 1 ), sizeof(char*) );
2573 for ( i = 0; i < count - 1; i++ )
2575 end = strchr( pos, delem );
2576 ret[i] = hb_strndup(pos, end - pos);
2579 ret[i] = strdup(pos);
2584 /****************************************************************************
2586 ****************************************************************************/
2587 static int ParseOptions( int argc, char ** argv )
2590 #define PREVIEWS 257
2591 #define START_AT_PREVIEW 258
2592 #define START_AT 259
2596 #define DISPLAY_WIDTH 263
2597 #define PIXEL_ASPECT 264
2599 #define KEEP_DISPLAY_ASPECT 266
2600 #define SUB_BURNED 267
2601 #define SUB_DEFAULT 268
2602 #define NATIVE_DUB 269
2603 #define SRT_FILE 270
2604 #define SRT_CODESET 271
2605 #define SRT_OFFSET 272
2606 #define SRT_LANG 273
2607 #define SRT_DEFAULT 274
2608 #define ROTATE_FILTER 275
2609 #define SCAN_ONLY 276
2610 #define MAIN_FEATURE 277
2611 #define MIN_DURATION 278
2615 static struct option long_options[] =
2617 { "help", no_argument, NULL, 'h' },
2618 { "update", no_argument, NULL, 'u' },
2619 { "verbose", optional_argument, NULL, 'v' },
2620 { "cpu", required_argument, NULL, 'C' },
2621 { "no-dvdnav", no_argument, NULL, DVDNAV },
2623 { "format", required_argument, NULL, 'f' },
2624 { "input", required_argument, NULL, 'i' },
2625 { "output", required_argument, NULL, 'o' },
2626 { "large-file", no_argument, NULL, '4' },
2627 { "optimize", no_argument, NULL, 'O' },
2628 { "ipod-atom", no_argument, NULL, 'I' },
2630 { "title", required_argument, NULL, 't' },
2631 { "min-duration",required_argument, NULL, MIN_DURATION },
2632 { "scan", no_argument, NULL, SCAN_ONLY },
2633 { "main-feature",no_argument, NULL, MAIN_FEATURE },
2634 { "chapters", required_argument, NULL, 'c' },
2635 { "angle", required_argument, NULL, ANGLE },
2636 { "markers", optional_argument, NULL, 'm' },
2637 { "audio", required_argument, NULL, 'a' },
2638 { "mixdown", required_argument, NULL, '6' },
2639 { "drc", required_argument, NULL, 'D' },
2640 { "subtitle", required_argument, NULL, 's' },
2641 { "subtitle-forced", optional_argument, NULL, 'F' },
2642 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2643 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2644 { "srt-file", required_argument, NULL, SRT_FILE },
2645 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2646 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2647 { "srt-lang", required_argument, NULL, SRT_LANG },
2648 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2649 { "native-language", required_argument, NULL,'N' },
2650 { "native-dub", no_argument, NULL, NATIVE_DUB },
2651 { "encoder", required_argument, NULL, 'e' },
2652 { "aencoder", required_argument, NULL, 'E' },
2653 { "two-pass", no_argument, NULL, '2' },
2654 { "deinterlace", optional_argument, NULL, 'd' },
2655 { "deblock", optional_argument, NULL, '7' },
2656 { "denoise", optional_argument, NULL, '8' },
2657 { "detelecine", optional_argument, NULL, '9' },
2658 { "decomb", optional_argument, NULL, '5' },
2659 { "grayscale", no_argument, NULL, 'g' },
2660 { "rotate", optional_argument, NULL, ROTATE_FILTER },
2661 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2662 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2663 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2664 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2665 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2666 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2667 { "modulus", required_argument, NULL, MODULUS },
2668 { "itu-par", no_argument, &itu_par, 1 },
2669 { "width", required_argument, NULL, 'w' },
2670 { "height", required_argument, NULL, 'l' },
2671 { "crop", required_argument, NULL, 'n' },
2673 { "vb", required_argument, NULL, 'b' },
2674 { "quality", required_argument, NULL, 'q' },
2675 { "size", required_argument, NULL, 'S' },
2676 { "ab", required_argument, NULL, 'B' },
2677 { "rate", required_argument, NULL, 'r' },
2678 { "arate", required_argument, NULL, 'R' },
2679 { "x264opts", required_argument, NULL, 'x' },
2680 { "turbo", no_argument, NULL, 'T' },
2681 { "maxHeight", required_argument, NULL, 'Y' },
2682 { "maxWidth", required_argument, NULL, 'X' },
2683 { "preset", required_argument, NULL, 'Z' },
2684 { "preset-list", no_argument, NULL, 'z' },
2686 { "aname", required_argument, NULL, 'A' },
2687 { "color-matrix",required_argument, NULL, 'M' },
2688 { "previews", required_argument, NULL, PREVIEWS },
2689 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2690 { "start-at", required_argument, NULL, START_AT },
2691 { "stop-at", required_argument, NULL, STOP_AT },
2692 { "vfr", no_argument, &cfr, 0 },
2693 { "cfr", no_argument, &cfr, 1 },
2694 { "pfr", no_argument, &cfr, 2 },
2698 int option_index = 0;
2702 cur_optind = optind;
2703 c = getopt_long( argc, argv,
2704 "hv::uC:f:4i:Io:t:c:m::M:a:A:6:s:UF::N:e:E:"
2705 "2dD:7895gOw:l:n:b:q:S:B:r:R:x:TY:X:Z:z",
2706 long_options, &option_index );
2715 /* option was handled entirely in getopt_long */
2724 if( optarg != NULL )
2726 debug = atoi( optarg );
2734 cpu = atoi( optarg );
2739 preset_name = strdup(optarg);
2749 format = strdup( optarg );
2752 input = strdup( optarg );
2754 char *devName = bsd_name_for_path( input ); // alloc
2757 if( device_is_dvd( devName ))
2760 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2761 sprintf( input, "/dev/%s", devName );
2768 output = strdup( optarg );
2781 titleindex = atoi( optarg );
2792 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2794 chapter_start = start;
2797 else if( sscanf( optarg, "%d", &start ) == 1 )
2799 chapter_start = start;
2800 chapter_end = chapter_start;
2804 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2811 angle = atoi( optarg );
2814 if( optarg != NULL )
2816 marker_file = strdup( optarg );
2818 chapter_markers = 1;
2821 if( optarg != NULL )
2823 atracks = strdup( optarg );
2832 if( optarg != NULL )
2834 mixdowns = strdup( optarg );
2838 if( optarg != NULL )
2840 dynamic_range_compression = strdup( optarg );
2844 subtracks = str_split( optarg, ',' );
2847 subforce = str_split( optarg, ',' );
2850 if( optarg != NULL )
2852 subburn = strdup( optarg );
2860 if( optarg != NULL )
2862 subdefault = strdup( optarg );
2870 native_language = strdup( optarg );
2876 srtfile = str_split( optarg, ',' );
2879 srtcodeset = str_split( optarg, ',' );
2882 srtoffset = str_split( optarg, ',' );
2885 srtlang = str_split( optarg, ',' );
2888 if( optarg != NULL )
2890 srtdefault = atoi( optarg );
2901 if( optarg != NULL )
2903 if (!( strcmp( optarg, "fast" ) ))
2905 deinterlace_opt = "-1";
2907 else if (!( strcmp( optarg, "slow" ) ))
2909 deinterlace_opt = "2";
2911 else if (!( strcmp( optarg, "slower" ) ))
2913 deinterlace_opt = "0";
2917 deinterlace_opt = strdup( optarg );
2923 if( optarg != NULL )
2925 deblock_opt = strdup( optarg );
2930 if( optarg != NULL )
2932 if (!( strcmp( optarg, "weak" ) ))
2934 denoise_opt = "2:1:2:3";
2936 else if (!( strcmp( optarg, "medium" ) ))
2938 denoise_opt = "3:2:2:3";
2940 else if (!( strcmp( optarg, "strong" ) ))
2942 denoise_opt = "7:7:5:5";
2946 denoise_opt = strdup( optarg );
2952 if( optarg != NULL )
2954 detelecine_opt = strdup( optarg );
2959 if( optarg != NULL )
2961 decomb_opt = strdup( optarg );
2969 if( optarg != NULL )
2971 rotate_opt = strdup( optarg );
2976 if( optarg != NULL )
2978 sscanf( optarg, "%i", &display_width );
2982 if( optarg != NULL )
2984 sscanf( optarg, "%i:%i", &par_width, &par_height );
2988 if( optarg != NULL )
2990 sscanf( optarg, "%i", &modulus );
2994 if( !strcasecmp( optarg, "ffmpeg" ) )
2996 vcodec = HB_VCODEC_FFMPEG;
2998 else if( !strcasecmp( optarg, "x264" ) )
3000 vcodec = HB_VCODEC_X264;
3002 else if( !strcasecmp( optarg, "x264b13" ) )
3004 vcodec = HB_VCODEC_X264;
3007 else if( !strcasecmp( optarg, "x264b30" ) )
3009 vcodec = HB_VCODEC_X264;
3012 else if( !strcasecmp( optarg, "theora" ) )
3014 vcodec = HB_VCODEC_THEORA;
3018 fprintf( stderr, "invalid codec (%s)\n", optarg );
3023 if( optarg != NULL )
3025 acodecs = strdup( optarg );
3029 width = atoi( optarg );
3032 height = atoi( optarg );
3037 char * tmp = optarg;
3038 for( i = 0; i < 4; i++ )
3042 crop[i] = strtol( tmp, &tmp, 0 );
3051 for( i = 0; i < hb_video_rates_count; i++ )
3053 if( !strcmp( optarg, hb_video_rates[i].string ) )
3055 vrate = hb_video_rates[i].rate;
3061 fprintf( stderr, "invalid framerate %s\n", optarg );
3063 else if ( cfr == 0 )
3070 if( optarg != NULL )
3072 arates = strdup( optarg );
3076 vbitrate = atoi( optarg );
3079 vquality = atof( optarg );
3082 size = atoi( optarg );
3085 if( optarg != NULL )
3087 abitrates = strdup( optarg );
3091 x264opts = strdup( optarg );
3094 turbo_opts_enabled = 1;
3097 maxHeight = atoi( optarg );
3100 maxWidth = atoi (optarg );
3103 if( optarg != NULL )
3105 anames = strdup( optarg );
3109 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
3111 case START_AT_PREVIEW:
3112 start_at_preview = atoi( optarg );
3115 start_at_string = strdup( optarg );
3116 start_at_token = strtok( start_at_string, ":");
3117 if( !strcmp( start_at_token, "frame" ) )
3119 start_at_token = strtok( NULL, ":");
3120 start_at_frame = atoi(start_at_token);
3122 else if( !strcmp( start_at_token, "pts" ) )
3124 start_at_token = strtok( NULL, ":");
3125 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3127 else if( !strcmp( start_at_token, "duration" ) )
3129 start_at_token = strtok( NULL, ":");
3130 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3131 start_at_pts *= 90000LL;
3135 stop_at_string = strdup( optarg );
3136 stop_at_token = strtok( stop_at_string, ":");
3137 if( !strcmp( stop_at_token, "frame" ) )
3139 stop_at_token = strtok( NULL, ":");
3140 stop_at_frame = atoi(stop_at_token);
3142 else if( !strcmp( stop_at_token, "pts" ) )
3144 stop_at_token = strtok( NULL, ":");
3145 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3147 else if( !strcmp( stop_at_token, "duration" ) )
3149 stop_at_token = strtok( NULL, ":");
3150 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3151 stop_at_pts *= 90000LL;
3155 if( atoi( optarg ) == 601 )
3157 else if( atoi( optarg ) == 709 )
3161 min_title_duration = strtol( optarg, NULL, 0 );
3164 fprintf( stderr, "unknown option (%s)\n", argv[cur_optind] );
3172 static int CheckOptions( int argc, char ** argv )
3179 if( input == NULL || *input == '\0' )
3181 fprintf( stderr, "Missing input device. Run %s --help for "
3182 "syntax.\n", argv[0] );
3187 if( titleindex > 0 && !titlescan )
3189 if( output == NULL || *output == '\0' )
3191 fprintf( stderr, "Missing output file name. Run %s --help "
3192 "for syntax.\n", argv[0] );
3198 char * p = strrchr( output, '.' );
3201 if( p && ( !strcasecmp( p, ".mp4" ) ||
3202 !strcasecmp( p, ".m4v" ) ) )
3208 default_acodec = HB_ACODEC_FAAC;
3210 else if( p && !strcasecmp(p, ".mkv" ) )
3213 default_acodec = HB_ACODEC_AC3;
3217 fprintf( stderr, "Output format couldn't be guessed "
3218 "from file name, using default.\n" );
3222 else if( !strcasecmp( format, "mp4" ) ||
3223 !strcasecmp( format, "m4v" ) )
3229 default_acodec = HB_ACODEC_FAAC;
3231 else if( !strcasecmp( format, "mkv" ) )
3234 default_acodec = HB_ACODEC_AC3;
3238 fprintf( stderr, "Invalid output format (%s). Possible "
3239 "choices are mp4, m4v and mkv\n.", format );
3247 static int get_acodec_for_string( char *codec )
3249 if( !strcasecmp( codec, "ac3" ) )
3251 return HB_ACODEC_AC3;
3253 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3255 return HB_ACODEC_DCA;
3257 else if( !strcasecmp( codec, "lame" ) )
3259 return HB_ACODEC_LAME;
3261 else if( !strcasecmp( codec, "faac" ) )
3263 return HB_ACODEC_FAAC;
3265 else if( !strcasecmp( codec, "vorbis") )
3267 return HB_ACODEC_VORBIS;
3270 else if( !strcasecmp( codec, "ca_aac") )
3272 return HB_ACODEC_CA_AAC;
3281 static int is_sample_rate_valid(int rate)
3284 for( i = 0; i < hb_audio_rates_count; i++ )
3286 if (rate == hb_audio_rates[i].rate)
3293 /****************************************************************************
3296 * Returns the BSD device name for the block device that contains the
3297 * passed-in path. Returns NULL on failure.
3298 ****************************************************************************/
3299 static char* bsd_name_for_path(char *path)
3303 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3309 // Get the volume reference number.
3310 FSCatalogInfo catalogInfo;
3311 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3317 FSVolumeRefNum volRefNum = catalogInfo.volume;
3319 // Now let's get the device name
3320 GetVolParmsInfoBuffer volumeParms;
3321 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3327 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3328 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3329 if( volumeParms.vMVersion < 4 )
3334 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3335 if( !volumeParms.vMDeviceID )
3340 return strdup( volumeParms.vMDeviceID );
3343 /****************************************************************************
3346 * Returns whether or not the passed in BSD device represents a DVD, or other
3348 ****************************************************************************/
3349 static int device_is_dvd(char *device)
3351 io_service_t service = get_iokit_service(device);
3352 if( service == IO_OBJECT_NULL )
3356 int result = is_dvd_service(service);
3357 IOObjectRelease(service);
3361 /****************************************************************************
3364 * Returns the IOKit service object for the passed in BSD device name.
3365 ****************************************************************************/
3366 static io_service_t get_iokit_service( char *device )
3368 CFMutableDictionaryRef matchingDict;
3369 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3370 if( matchingDict == NULL )
3372 return IO_OBJECT_NULL;
3374 // Fetch the object with the matching BSD node name. There should only be
3375 // one match, so IOServiceGetMatchingService is used instead of
3376 // IOServiceGetMatchingServices to simplify the code.
3377 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3380 /****************************************************************************
3383 * Returns whether or not the service passed in is a DVD.
3385 * Searches for an IOMedia object that represents the entire (whole) media that
3386 * the volume is on. If the volume is on partitioned media, the whole media
3387 * object will be a parent of the volume's media object. If the media is not
3388 * partitioned, the volume's media object will be the whole media object.
3389 ****************************************************************************/
3390 static int is_dvd_service( io_service_t service )
3392 kern_return_t kernResult;
3395 // Create an iterator across all parents of the service object passed in.
3396 kernResult = IORegistryEntryCreateIterator( service,
3398 kIORegistryIterateRecursively | kIORegistryIterateParents,
3400 if( kernResult != KERN_SUCCESS )
3404 if( iter == IO_OBJECT_NULL )
3409 // A reference on the initial service object is released in the do-while
3410 // loop below, so add a reference to balance.
3411 IOObjectRetain( service );
3416 if( is_whole_media_service( service ) &&
3417 IOObjectConformsTo( service, kIODVDMediaClass) )
3421 IOObjectRelease( service );
3422 } while( !result && (service = IOIteratorNext( iter )) );
3423 IOObjectRelease( iter );
3428 /****************************************************************************
3429 * is_whole_media_service
3431 * Returns whether or not the service passed in is an IOMedia service and
3432 * represents the "whole" media instead of just a partition.
3434 * The whole media object is indicated in the IORegistry by the presence of a
3435 * property with the key "Whole" and value "Yes".
3436 ****************************************************************************/
3437 static int is_whole_media_service( io_service_t service )
3441 if( IOObjectConformsTo( service, kIOMediaClass ) )
3443 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3444 CFSTR( kIOMediaWholeKey ),
3445 kCFAllocatorDefault,
3451 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3452 CFRelease( wholeMedia );
3457 #endif // __APPLE_CC__