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 longest_title = 0;
41 static char * native_language = NULL;
42 static int native_dub = 0;
43 static int twoPass = 0;
44 static int deinterlace = 0;
45 static char * deinterlace_opt = 0;
46 static int deblock = 0;
47 static char * deblock_opt = 0;
48 static int denoise = 0;
49 static char * denoise_opt = 0;
50 static int detelecine = 0;
51 static char * detelecine_opt = 0;
52 static int decomb = 0;
53 static char * decomb_opt = 0;
54 static int rotate = 0;
55 static char * rotate_opt = 0;
56 static int grayscale = 0;
57 static int vcodec = HB_VCODEC_FFMPEG;
58 static int h264_13 = 0;
59 static int h264_30 = 0;
60 static hb_list_t * audios = NULL;
61 static hb_audio_config_t * audio = NULL;
62 static int num_audio_tracks = 0;
63 static char * mixdowns = NULL;
64 static char * dynamic_range_compression = NULL;
65 static char * atracks = NULL;
66 static char * arates = NULL;
67 static char * abitrates = NULL;
68 static char * acodecs = NULL;
69 static char * anames = NULL;
70 static int default_acodec = HB_ACODEC_FAAC;
71 static int default_abitrate = 160;
72 static int audio_explicit = 0;
73 static char ** subtracks = NULL;
74 static char ** subforce = NULL;
75 static char * subburn = NULL;
76 static char * subdefault = NULL;
77 static char ** srtfile = NULL;
78 static char ** srtcodeset = NULL;
79 static char ** srtoffset = NULL;
80 static char ** srtlang = NULL;
81 static int srtdefault = -1;
82 static int subtitle_scan = 0;
84 static int height = 0;
85 static int crop[4] = { -1,-1,-1,-1 };
88 static float vquality = -1.0;
89 static int vbitrate = 0;
92 static int anamorphic_mode = 0;
93 static int modulus = 0;
94 static int par_height = 0;
95 static int par_width = 0;
96 static int display_width = 0;
97 static int keep_display_aspect = 0;
98 static int itu_par = 0;
100 static int chapter_start = 0;
101 static int chapter_end = 0;
102 static int chapter_markers = 0;
103 static char * marker_file = NULL;
104 static char *x264opts = NULL;
105 static char *x264opts2 = NULL;
106 static int maxHeight = 0;
107 static int maxWidth = 0;
108 static int turbo_opts_enabled = 0;
109 static char * turbo_opts = "ref=1:subme=2:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
110 static int largeFileSize = 0;
111 static int preset = 0;
112 static char * preset_name = 0;
114 static int mp4_optimize = 0;
115 static int ipod_atom = 0;
116 static int color_matrix = 0;
117 static int preview_count = 10;
118 static int store_previews = 0;
119 static int start_at_preview = 0;
120 static int64_t start_at_pts = 0;
121 static int start_at_frame = 0;
122 static char * start_at_string = NULL;
123 static char * start_at_token = NULL;
124 static int64_t stop_at_pts = 0;
125 static int stop_at_frame = 0;
126 static char * stop_at_string = NULL;
127 static char * stop_at_token = NULL;
129 /* Exit cleanly on Ctrl-C */
130 static volatile int die = 0;
131 static void SigHandler( int );
134 static void ShowCommands();
135 static void ShowHelp();
136 static void ShowPresets();
138 static int ParseOptions( int argc, char ** argv );
139 static int CheckOptions( int argc, char ** argv );
140 static int HandleEvents( hb_handle_t * h );
142 static int get_acodec_for_string( char *codec );
143 static int is_sample_rate_valid(int rate);
146 static char* bsd_name_for_path(char *path);
147 static int device_is_dvd(char *device);
148 static io_service_t get_iokit_service( char *device );
149 static int is_dvd_service( io_service_t service );
150 static int is_whole_media_service( io_service_t service );
153 /* Only print the "Muxing..." message once */
154 static int show_mux_warning = 1;
156 /****************************************************************************
159 * When using the CLI just display using hb_log as we always did in the past
160 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
161 ****************************************************************************/
162 static void hb_cli_error_handler ( const char *errmsg )
164 fprintf( stderr, "ERROR: %s\n", errmsg );
167 int main( int argc, char ** argv )
173 /* win32 _IOLBF (line-buffering) is the same as _IOFBF (full-buffering).
174 * force it to unbuffered otherwise informative output is not easily parsed.
176 #if defined( _WIN32 ) || defined( __MINGW32__ )
177 setvbuf( stdout, NULL, _IONBF, 0 );
178 setvbuf( stderr, NULL, _IONBF, 0 );
181 audios = hb_list_init();
183 /* Parse command line */
184 if( ParseOptions( argc, argv ) ||
185 CheckOptions( argc, argv ) )
190 /* Register our error handler */
191 hb_register_error_handler(&hb_cli_error_handler);
194 h = hb_init( debug, update );
195 hb_dvd_set_dvdnav( dvdnav );
198 fprintf( stderr, "%s - %s - %s\n",
199 HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
201 /* Check for update */
204 if( ( build = hb_check_update( h, &version ) ) > -1 )
206 fprintf( stderr, "You are using an old version of "
207 "HandBrake.\nLatest is %s (build %d).\n", version,
212 fprintf( stderr, "Your version of HandBrake is up to "
220 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
221 hb_get_cpu_count( h ) > 1 ? "s" : "" );
224 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
225 cpu > 1 ? "s" : "" );
226 hb_set_cpu_count( h, cpu );
229 /* Exit ASAP on Ctrl-C */
230 signal( SIGINT, SigHandler );
232 /* Feed libhb with a DVD to scan */
233 fprintf( stderr, "Opening %s...\n", input );
237 * We need to scan for all the titles in order to find the longest
242 hb_scan( h, input, titleindex, preview_count, store_previews );
247 #if defined( __MINGW32__ )
251 case 0x03: /* ctrl-c */
253 fprintf( stdout, "\nEncoding Quit by user command\n" );
257 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
269 #elif !defined(SYS_BEOS)
279 FD_SET( STDIN_FILENO, &fds );
280 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
287 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
289 if( buf[size] == '\n' )
296 if( size >= 256 || buf[size] == '\n' )
301 fprintf( stdout, "\nEncoding Quit by user command\n" );
305 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
327 if( input ) free( input );
328 if( output ) free( output );
329 if( format ) free( format );
332 while( ( audio = hb_list_item( audios, 0 ) ) )
334 hb_list_rem( audios, audio );
335 if( audio->out.name )
337 free( audio->out.name );
341 hb_list_close( &audios );
343 if( mixdowns ) free( mixdowns );
344 if( dynamic_range_compression ) free( dynamic_range_compression );
345 if( atracks ) free( atracks );
346 if( arates ) free( arates );
347 if( abitrates ) free( abitrates );
348 if( acodecs ) free( acodecs );
349 if( anames ) free( anames );
350 if (native_language ) free (native_language );
351 if( x264opts ) free (x264opts );
352 if( x264opts2 ) free (x264opts2 );
353 if (preset_name) free (preset_name);
354 if( stop_at_string ) free( stop_at_string );
355 if( start_at_string ) free( start_at_string );
357 fprintf( stderr, "HandBrake has exited.\n" );
362 static void ShowCommands()
364 fprintf( stdout, "\nCommands:\n" );
365 fprintf( stdout, " [h]elp Show this message\n" );
366 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
367 fprintf( stdout, " [p]ause Pause encoding\n" );
368 fprintf( stdout, " [r]esume Resume encoding\n" );
371 static void PrintTitleInfo( hb_title_t * title )
373 hb_chapter_t * chapter;
374 hb_subtitle_t * subtitle;
377 fprintf( stderr, "+ title %d:\n", title->index );
378 if ( title->type == HB_STREAM_TYPE )
380 fprintf( stderr, " + stream: %s\n", title->path );
382 else if ( title->type == HB_DVD_TYPE )
384 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
385 title->vts, title->ttn, title->cell_start, title->cell_end,
386 title->block_count );
389 fprintf( stderr, " + angle(s) %d\n", title->angle_count );
390 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
391 title->hours, title->minutes, title->seconds );
392 fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
393 title->width, title->height,
394 title->pixel_aspect_width,
395 title->pixel_aspect_height,
396 (float) title->aspect,
397 (float) title->rate / title->rate_base );
398 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
399 title->crop[1], title->crop[2], title->crop[3] );
400 fprintf( stderr, " + chapters:\n" );
401 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
403 chapter = hb_list_item( title->list_chapter, i );
404 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
405 "%02d:%02d:%02d\n", chapter->index,
406 chapter->cell_start, chapter->cell_end,
407 chapter->block_count, chapter->hours, chapter->minutes,
410 fprintf( stderr, " + audio tracks:\n" );
411 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
413 audio = hb_list_audio_config_item( title->list_audio, i );
414 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
416 fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
418 audio->lang.description,
419 audio->lang.iso639_2,
420 audio->in.samplerate,
425 fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
427 audio->lang.description,
428 audio->lang.iso639_2 );
431 fprintf( stderr, " + subtitle tracks:\n" );
432 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
434 subtitle = hb_list_item( title->list_subtitle, i );
435 fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)\n",
436 i + 1, subtitle->lang,
438 (subtitle->format == TEXTSUB) ? "Text" : "Bitmap");
441 if(title->detected_interlacing)
443 /* Interlacing was found in half or more of the preview frames */
444 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
449 static int test_sub_list( char ** list, char * needle, int pos )
453 if ( list == NULL || needle == NULL )
456 if ( list[0] == NULL && pos == 1 )
459 for ( i = 0; list[i] != NULL; i++ )
461 if ( strcasecmp( list[i], needle ) == 0 )
467 static int HandleEvents( hb_handle_t * h )
470 int tmp_num_audio_tracks;
472 hb_get_state( h, &s );
479 #define p s.param.scanning
480 case HB_STATE_SCANNING:
481 /* Show what title is currently being scanned */
482 fprintf( stderr, "Scanning title %d", p.title_cur );
484 fprintf( stderr, " of %d", p.title_count );
485 fprintf( stderr, "...\n" );
489 case HB_STATE_SCANDONE:
497 /* Audio argument string parsing variables */
501 int mixdown = HB_AMIXDOWN_DOLBYPLII;
503 /* Audio argument string parsing variables */
505 list = hb_get_titles( h );
507 if( !hb_list_count( list ) )
509 /* No valid title, stop right there */
510 fprintf( stderr, "No title found.\n" );
517 int longest_title_idx=0;
518 int longest_title_pos=-1;
519 int longest_title_time=0;
522 fprintf( stderr, "Searching for longest title...\n" );
524 for( i = 0; i < hb_list_count( list ); i++ )
526 title = hb_list_item( list, i );
527 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
528 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
529 i, title->index, title_time );
530 if( longest_title_time < title_time )
532 longest_title_time = title_time;
533 longest_title_pos = i;
534 longest_title_idx = title->index;
537 if( longest_title_pos == -1 )
539 fprintf( stderr, "No longest title found.\n" );
543 titleindex = longest_title_idx;
544 fprintf( stderr, "Found longest title, setting title to %d\n",
547 title = hb_list_item( list, longest_title_pos);
549 title = hb_list_item( list, 0 );
554 /* Scan-only mode, print infos and exit */
556 for( i = 0; i < hb_list_count( list ); i++ )
558 title = hb_list_item( list, i );
559 PrintTitleInfo( title );
565 /* Set job settings */
568 PrintTitleInfo( title );
570 if( chapter_start && chapter_end && !stop_at_pts && !start_at_preview && !stop_at_frame && !start_at_pts && !start_at_frame )
572 job->chapter_start = MAX( job->chapter_start,
574 job->chapter_end = MIN( job->chapter_end,
576 job->chapter_end = MAX( job->chapter_start,
587 fprintf( stderr, "+ Using preset: %s", preset_name);
589 if (!strcmp(preset_name, "Universal"))
595 vcodec = HB_VCODEC_X264;
596 job->vquality = 20.0;
599 atracks = strdup("1,1");
603 acodecs = strdup("faac,ac3");
607 abitrates = strdup("160,160");
611 mixdowns = strdup("dpl2,auto");
615 arates = strdup("48,Auto");
617 if( !dynamic_range_compression )
619 dynamic_range_compression = strdup("0.0,0.0");
624 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subme=6");
627 job->chapter_markers = 1;
630 if (!strcmp(preset_name, "iPod"))
637 vcodec = HB_VCODEC_X264;
641 atracks = strdup("1");
645 acodecs = strdup("faac");
649 abitrates = strdup("160");
653 mixdowns = strdup("dpl2");
657 arates = strdup("48");
659 if( !dynamic_range_compression )
661 dynamic_range_compression = strdup("0.0");
666 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");
668 job->chapter_markers = 1;
671 if (!strcmp(preset_name, "iPhone & iPod Touch"))
677 vcodec = HB_VCODEC_X264;
678 job->vquality = 20.0;
681 atracks = strdup("1");
685 acodecs = strdup("faac");
689 abitrates = strdup("128");
693 mixdowns = strdup("dpl2");
697 arates = strdup("48");
699 if( !dynamic_range_compression )
701 dynamic_range_compression = strdup("0.0");
706 x264opts = strdup("cabac=0:ref=2:me=umh:bframes=0:subme=6:8x8dct=0:trellis=0");
708 job->chapter_markers = 1;
711 if (!strcmp(preset_name, "AppleTV"))
717 job->largeFileSize = 1;
718 vcodec = HB_VCODEC_X264;
719 job->vquality = 20.0;
722 atracks = strdup("1,1");
726 acodecs = strdup("faac,ac3");
730 abitrates = strdup("160,160");
734 mixdowns = strdup("dpl2,auto");
738 arates = strdup("48,Auto");
740 if( !dynamic_range_compression )
742 dynamic_range_compression = strdup("0.0,0.0");
747 x264opts = strdup("cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0:vbv-maxrate=9500:vbv-bufsize=9500");
750 job->chapter_markers = 1;
753 if (!strcmp(preset_name, "Normal"))
759 vcodec = HB_VCODEC_X264;
760 job->vquality = 20.0;
763 atracks = strdup("1");
767 acodecs = strdup("faac");
771 abitrates = strdup("160");
775 mixdowns = strdup("dpl2");
779 arates = strdup("48");
781 if( !dynamic_range_compression )
783 dynamic_range_compression = strdup("0.0");
787 x264opts = strdup("ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0");
790 job->chapter_markers = 1;
793 if (!strcmp(preset_name, "High Profile"))
799 vcodec = HB_VCODEC_X264;
800 job->vquality = 20.0;
803 atracks = strdup("1,1");
807 acodecs = strdup("faac,ac3");
811 abitrates = strdup("160,160");
815 mixdowns = strdup("dpl2,auto");
819 arates = strdup("48,Auto");
821 if( !dynamic_range_compression )
823 dynamic_range_compression = strdup("0.0,0.0");
827 x264opts = strdup("b-adapt=2:rc-lookahead=50");
832 job->chapter_markers = 1;
835 if (!strcmp(preset_name, "Classic"))
841 job->vbitrate = 1000;
844 atracks = strdup("1");
848 acodecs = strdup("faac");
852 abitrates = strdup("160");
856 mixdowns = strdup("dpl2");
860 arates = strdup("48");
862 if( !dynamic_range_compression )
864 dynamic_range_compression = strdup("0.0");
868 if (!strcmp(preset_name, "AppleTV Legacy"))
874 job->largeFileSize = 1;
875 vcodec = HB_VCODEC_X264;
876 job->vbitrate = 2500;
879 atracks = strdup("1,1");
883 acodecs = strdup("faac,ac3");
887 abitrates = strdup("160,160");
891 mixdowns = strdup("dpl2,auto");
895 arates = strdup("48,Auto");
897 if( !dynamic_range_compression )
899 dynamic_range_compression = strdup("0.0,0.0");
903 x264opts = strdup("ref=1:subme=5:me=umh:no-fast-pskip=1:cabac=0:weightb=0:8x8dct=0:trellis=0");
906 job->chapter_markers = 1;
909 if (!strcmp(preset_name, "iPhone Legacy"))
916 vcodec = HB_VCODEC_X264;
920 atracks = strdup("1");
924 acodecs = strdup("faac");
928 abitrates = strdup("128");
932 mixdowns = strdup("dpl2");
936 arates = strdup("48");
938 if( !dynamic_range_compression )
940 dynamic_range_compression = strdup("0.0");
945 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");
947 job->chapter_markers = 1;
950 if (!strcmp(preset_name, "iPod Legacy"))
957 vcodec = HB_VCODEC_X264;
958 job->vbitrate = 1500;
961 atracks = strdup("1");
965 acodecs = strdup("faac");
969 abitrates = strdup("160");
973 mixdowns = strdup("dpl2");
977 arates = strdup("48");
979 if( !dynamic_range_compression )
981 dynamic_range_compression = strdup("0.0");
986 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");
988 job->chapter_markers = 1;
992 if ( chapter_markers )
994 job->chapter_markers = chapter_markers;
996 if( marker_file != NULL )
998 hb_csv_file_t * file = hb_open_csv_file( marker_file );
999 hb_csv_cell_t * cell;
1003 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
1007 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
1011 /* Parse the cells */
1012 while( NULL != ( cell = hb_read_next_cell( file ) ) )
1014 /* We have a chapter number */
1015 if( cell->cell_col == 0 )
1017 row = cell->cell_row;
1018 chapter = atoi( cell->cell_text );
1021 /* We have a chapter name */
1022 if( cell->cell_col == 1 && row == cell->cell_row )
1024 /* If we have a valid chapter, copy the string an terminate it */
1025 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
1027 hb_chapter_t * chapter_s;
1029 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
1030 strncpy(chapter_s->title, cell->cell_text, 1023);
1031 chapter_s->title[1023] = '\0';
1036 hb_dispose_cell( cell );
1039 hb_close_csv_file( file );
1044 /* No marker file */
1046 int number_of_chapters = hb_list_count(job->title->list_chapter);
1049 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
1051 hb_chapter_t * chapter_s;
1052 chapter_s = hb_list_item( job->title->list_chapter, chapter);
1053 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
1054 chapter_s->title[1023] = '\0';
1059 if( crop[0] >= 0 && crop[1] >= 0 &&
1060 crop[2] >= 0 && crop[3] >= 0 )
1062 memcpy( job->crop, crop, 4 * sizeof( int ) );
1065 job->deinterlace = deinterlace;
1066 job->grayscale = grayscale;
1068 /* Add selected filters */
1069 job->filters = hb_list_init();
1073 hb_filter_rotate.settings = rotate_opt;
1074 hb_list_add( job->filters, &hb_filter_rotate);
1078 hb_filter_detelecine.settings = detelecine_opt;
1079 hb_list_add( job->filters, &hb_filter_detelecine );
1083 hb_filter_decomb.settings = decomb_opt;
1084 hb_list_add( job->filters, &hb_filter_decomb );
1088 hb_filter_deinterlace.settings = deinterlace_opt;
1089 hb_list_add( job->filters, &hb_filter_deinterlace );
1093 hb_filter_deblock.settings = deblock_opt;
1094 hb_list_add( job->filters, &hb_filter_deblock );
1098 hb_filter_denoise.settings = denoise_opt;
1099 hb_list_add( job->filters, &hb_filter_denoise );
1102 switch( anamorphic_mode )
1104 case 0: // Non-anamorphic
1106 if( width && height )
1109 job->height = height;
1114 hb_fix_aspect( job, HB_KEEP_WIDTH );
1118 job->height = height;
1119 hb_fix_aspect( job, HB_KEEP_HEIGHT );
1121 else if( !width && !height )
1123 hb_fix_aspect( job, HB_KEEP_WIDTH );
1128 case 1: // Strict anammorphic
1129 job->anamorphic.mode = anamorphic_mode;
1132 case 2: // Loose anamorphic
1133 job->anamorphic.mode = 2;
1137 job->modulus = modulus;
1142 job->anamorphic.itu_par = itu_par;
1149 else if( !width && !height )
1151 /* Default to full width when one isn't specified for loose anamorphic */
1152 job->width = title->width - job->crop[2] - job->crop[3];
1157 case 3: // Custom Anamorphic 3: Power User Jamboree
1158 job->anamorphic.mode = 3;
1162 job->modulus = modulus;
1167 job->anamorphic.itu_par = itu_par;
1170 if( par_width && par_height )
1172 job->anamorphic.par_width = par_width;
1173 job->anamorphic.par_height = par_height;
1176 if( keep_display_aspect )
1178 job->anamorphic.keep_display_aspect = 1;
1180 /* First, what *is* the display aspect? */
1181 int cropped_width = title->width - job->crop[2] - job->crop[3];
1182 int cropped_height = title->height - job->crop[0] - job->crop[1];
1184 /* XXX -- I'm assuming people want to keep the source
1185 display AR even though they might have already
1186 asked for ITU values instead. */
1187 float source_display_width = (float)cropped_width *
1188 (float)title->pixel_aspect_width / (float)title->pixel_aspect_height;
1189 float display_aspect = source_display_width / (float)cropped_height;
1190 /* When keeping display aspect, we have to rank some values
1191 by priority in order to consistently handle situations
1192 when more than one might be specified by default.
1194 * First off, PAR gets ignored. (err make this reality)
1195 * Height will be respected over all other settings,
1196 * If it isn't set, display_width will be followed.
1197 * If it isn't set, width will be followed. */
1200 /* We scale the display width to the new height */
1201 display_width = (int)( (double)height * display_aspect );
1203 else if( display_width )
1205 /* We scale the height to the new display width */
1206 height = (int)( (double)display_width / display_aspect );
1212 /* Adjust the PAR to create the new display width
1213 from the default job width. */
1214 job->anamorphic.dar_width = display_width;
1216 job->anamorphic.dar_height = height ?
1218 title->height - job->crop[0] - job->crop[1];
1221 if( width && height )
1223 /* Use these storage dimensions */
1225 job->height = height;
1229 /* Use just this storage width */
1231 job->height = title->height - job->crop[0] - job->crop[1];
1235 /* Use just this storage height. */
1236 job->height = height;
1237 job->width = title->width - job->crop[2] - job->crop[3];
1239 else if( !width && !height )
1241 /* Assume source dimensions after cropping. */
1242 job->width = title->width - job->crop[2] - job->crop[3];
1243 job->height = title->height - job->crop[0] - job->crop[1];
1249 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
1251 job->vquality = vquality;
1256 job->vquality = -1.0;
1257 job->vbitrate = vbitrate;
1261 job->vcodec = vcodec;
1265 job->h264_level = 13;
1269 job->h264_level = 30;
1274 job->vrate = 27000000;
1275 job->vrate_base = vrate;
1279 // cfr or pfr flag with no rate specified implies
1280 // use the title rate.
1282 job->vrate = title->rate;
1283 job->vrate_base = title->rate_base;
1286 /* Grab audio tracks */
1289 char * token = strtok(atracks, ",");
1292 int track_start, track_end;
1293 while( token != NULL )
1295 audio = calloc(1, sizeof(*audio));
1296 hb_audio_config_init(audio);
1297 if (strlen(token) >= 3)
1299 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1302 for (i = track_start - 1; i < track_end; i++)
1304 if (i != track_start - 1)
1306 audio = calloc(1, sizeof(*audio));
1307 hb_audio_config_init(audio);
1309 audio->in.track = i;
1310 audio->out.track = num_audio_tracks++;
1311 hb_list_add(audios, audio);
1314 else if( !strcasecmp(token, "none" ) )
1316 audio->in.track = audio->out.track = -1;
1317 audio->out.codec = 0;
1318 hb_list_add(audios, audio);
1323 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1330 audio->in.track = atoi(token) - 1;
1331 audio->out.track = num_audio_tracks++;
1332 hb_list_add(audios, audio);
1334 token = strtok(NULL, ",");
1338 /* Parse audio tracks */
1339 if( native_language && native_dub )
1341 if( hb_list_count( audios ) == 0 || !audio_explicit )
1343 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
1348 audio = hb_list_audio_config_item( title->list_audio, i );
1350 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1352 if( strncasecmp( native_language, audio_lang,
1353 sizeof( audio_lang ) ) == 0 &&
1354 audio->lang.type != 3 && // Directors 1
1355 audio->lang.type != 4) // Directors 2
1358 * Matched an audio to our native language - use it.
1359 * Replace any existing audio tracks that a preset may
1362 if( hb_list_count(audios) == 0) {
1363 audio = calloc(1, sizeof(*audio));
1364 hb_audio_config_init(audio);
1365 audio->in.track = track;
1366 audio->out.track = num_audio_tracks++;
1367 /* Add it to our audios */
1368 hb_list_add(audios, audio);
1371 * Update the track numbers on what is already in
1374 for( i=0; i < hb_list_count( audios ); i++ )
1376 audio = hb_list_item( audios, i );
1378 audio->in.track = track;
1385 fprintf( stderr, "Warning: Native language (dubbing) selection ignored since an audio track has already been selected\n");
1389 if( hb_list_count(audios) == 0 &&
1390 hb_list_count(job->title->list_audio) > 0 )
1392 /* Create a new audio track with default settings */
1393 audio = calloc(1, sizeof(*audio));
1394 hb_audio_config_init(audio);
1395 /* Add it to our audios */
1396 hb_list_add(audios, audio);
1399 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
1400 for (i = 0; i < tmp_num_audio_tracks; i++)
1402 audio = hb_list_item(audios, 0);
1403 if( (audio == NULL) || (audio->in.track == -1) ||
1404 (audio->out.track == -1) || (audio->out.codec == 0) )
1410 if( hb_audio_add( job, audio ) == 0 )
1412 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
1413 audio->in.track + 1 );
1418 hb_list_rem(audios, audio);
1420 if( audio->out.name )
1422 free( audio->out.name);
1431 char * token = strtok(acodecs, ",");
1434 while ( token != NULL )
1436 if ((acodec = get_acodec_for_string(token)) == -1)
1438 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
1439 acodec = default_acodec;
1441 if( i < num_audio_tracks )
1443 audio = hb_list_audio_config_item(job->list_audio, i);
1444 audio->out.codec = acodec;
1448 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1449 hb_audio_config_t audio;
1453 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1455 hb_audio_config_init(&audio);
1456 audio.in.track = last_audio->in.track;
1457 audio.out.track = num_audio_tracks++;
1458 audio.out.codec = acodec;
1459 hb_audio_add(job, &audio);
1463 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1466 token = strtok(NULL, ",");
1470 if( i < num_audio_tracks )
1472 /* We have fewer inputs than audio tracks, use the default codec for
1473 * this container for the remaining tracks. Unless we only have one input
1474 * then use that codec instead.
1477 acodec = default_acodec;
1478 for ( ; i < num_audio_tracks; i++)
1480 audio = hb_list_audio_config_item(job->list_audio, i);
1481 audio->out.codec = acodec;
1490 char * token = strtok(arates, ",");
1493 while ( token != NULL )
1495 arate = atoi(token);
1496 audio = hb_list_audio_config_item(job->list_audio, i);
1499 for( j = 0; j < hb_audio_rates_count; j++ )
1501 if( !strcmp( token, hb_audio_rates[j].string ) )
1503 arate = hb_audio_rates[j].rate;
1510 if (!is_sample_rate_valid(arate))
1512 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1513 arate = audio->in.samplerate;
1516 audio->out.samplerate = arate;
1517 if( (++i) >= num_audio_tracks )
1518 break; /* We have more inputs than audio tracks, oops */
1522 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1524 token = strtok(NULL, ",");
1527 if (i < num_audio_tracks)
1529 /* We have fewer inputs than audio tracks, use default sample rate.
1530 * Unless we only have one input, then use that for all tracks.
1533 arate = audio->in.samplerate;
1534 for ( ; i < num_audio_tracks; i++)
1536 audio = hb_list_audio_config_item(job->list_audio, i);
1537 audio->out.samplerate = arate;
1546 char * token = strtok(abitrates, ",");
1549 while ( token != NULL )
1551 abitrate = atoi(token);
1552 audio = hb_list_audio_config_item(job->list_audio, i);
1556 audio->out.bitrate = abitrate;
1557 if( (++i) >= num_audio_tracks )
1558 break; /* We have more inputs than audio tracks, oops */
1562 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1564 token = strtok(NULL, ",");
1567 if (i < num_audio_tracks)
1569 /* We have fewer inputs than audio tracks, use the default bitrate
1570 * for the remaining tracks. Unless we only have one input, then use
1571 * that for all tracks.
1574 abitrate = default_abitrate;
1575 for (; i < num_audio_tracks; i++)
1577 audio = hb_list_audio_config_item(job->list_audio, i);
1578 audio->out.bitrate = abitrate;
1585 if ( dynamic_range_compression )
1587 char * token = strtok(dynamic_range_compression, ",");
1589 token = dynamic_range_compression;
1590 while ( token != NULL )
1592 d_r_c = atof(token);
1593 audio = hb_list_audio_config_item(job->list_audio, i);
1596 audio->out.dynamic_range_compression = d_r_c;
1597 if( (++i) >= num_audio_tracks )
1598 break; /* We have more inputs than audio tracks, oops */
1602 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1604 token = strtok(NULL, ",");
1607 if (i < num_audio_tracks)
1609 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1610 * tracks. Unless we only have one input, then use the same DRC for all
1615 for (; i < num_audio_tracks; i++)
1617 audio = hb_list_audio_config_item(job->list_audio, i);
1618 audio->out.dynamic_range_compression = d_r_c;
1627 char * token = strtok(mixdowns, ",");
1630 while ( token != NULL )
1632 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1633 audio = hb_list_audio_config_item(job->list_audio, i);
1636 audio->out.mixdown = mixdown;
1637 if( (++i) >= num_audio_tracks )
1638 break; /* We have more inputs than audio tracks, oops */
1642 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1644 token = strtok(NULL, ",");
1647 if (i < num_audio_tracks)
1649 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1650 * we only have one input, then use that.
1653 mixdown = HB_AMIXDOWN_DOLBYPLII;
1654 for (; i < num_audio_tracks; i++)
1656 audio = hb_list_audio_config_item(job->list_audio, i);
1657 audio->out.mixdown = mixdown;
1662 /* Audio Track Names */
1666 char * token = strtok(anames, ",");
1669 while ( token != NULL )
1671 audio = hb_list_audio_config_item(job->list_audio, i);
1674 audio->out.name = strdup(token);
1675 if( (++i) >= num_audio_tracks )
1676 break; /* We have more names than audio tracks, oops */
1680 fprintf(stderr, "Ignoring aname '%s', no audio track\n",
1683 token = strtok(NULL, ",");
1686 if( i < num_audio_tracks && i == 1 )
1688 /* We have exactly one name and more than one audio track. Use the same
1689 * name for all tracks. */
1690 for ( ; i < num_audio_tracks; i++)
1692 audio = hb_list_audio_config_item(job->list_audio, i);
1693 audio->out.name = strdup(anames);
1696 /* Audio Track Names */
1700 job->vbitrate = hb_calc_bitrate( job, size );
1701 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1711 for ( i = 0; subtracks[i] != NULL; i++ )
1714 token = subtracks[i];
1715 if( strcasecmp(token, "scan" ) == 0 )
1717 int burn = 0, force = 0, def = 0;
1719 if ( subburn != NULL )
1721 burn = ( pos == 1 && subburn[0] == 0 ) ||
1722 ( strcmp( "scan", subburn ) == 0 );
1724 if ( subdefault != NULL )
1726 def = ( pos == 1 && subdefault[0] == 0 ) ||
1727 ( strcmp( "scan", subdefault ) == 0 );
1729 force = test_sub_list( subforce, "scan", pos );
1731 if ( !burn && mux == HB_MUX_MKV )
1733 job->select_subtitle_config.dest = PASSTHRUSUB;
1743 if ( !( !burn && mux == HB_MUX_MP4 ) )
1745 job->select_subtitle_config.force = force;
1746 job->select_subtitle_config.default_track = def;
1749 fprintf( stderr, "Warning: Subtitle Scan for MP4 requires the '--subtitle-burn' option to be selected\n");
1754 hb_subtitle_t * subtitle;
1755 hb_subtitle_config_t sub_config;
1757 int burn = 0, force = 0, def = 0;
1759 track = atoi(token) - 1;
1760 subtitle = hb_list_item(title->list_subtitle, track);
1761 if( subtitle == NULL )
1763 fprintf( stderr, "Warning: Could not find subtitle track %d, skipped\n", track+1 );
1766 sub_config = subtitle->config;
1768 if ( subburn != NULL )
1770 burn = ( pos == 1 && subburn[0] == 0 ) ||
1771 ( strcmp( token, subburn ) == 0 );
1773 if ( subdefault != NULL )
1775 def = ( pos == 1 && subdefault[0] == 0 ) ||
1776 ( strcmp( token, subdefault ) == 0 );
1779 force = test_sub_list(subforce, token, pos);
1781 if ( !burn && mux == HB_MUX_MKV &&
1782 subtitle->format == PICTURESUB)
1784 sub_config.dest = PASSTHRUSUB;
1786 else if (!burn && mux == HB_MUX_MP4 &&
1787 subtitle->format == PICTURESUB)
1789 // Skip any non-burned vobsubs when output is mp4
1790 fprintf( stderr, "Warning: Skipping subtitle track %d, can't pass-through VOBSUBs in an MP4 container,\nadd '--subtitle-burn %d' to the command line\n", track+1, track+1 );
1793 else if ( burn && subtitle->format == PICTURESUB )
1795 // Only allow one subtitle to be burned into video
1798 fprintf( stderr, "Warning: Skipping subtitle track %d, can't have more than one track burnt in\n", track+1 );
1803 sub_config.force = force;
1804 sub_config.default_track = def;
1805 hb_subtitle_add( job, &sub_config, track );
1814 hb_subtitle_config_t sub_config;
1817 for( i=0; srtfile[i] != NULL; i++ )
1819 char *codeset = "L1";
1825 if( srtcodeset && srtcodeset[i] )
1827 codeset = srtcodeset[i];
1829 if( srtoffset && srtoffset[i] )
1831 offset = strtoll( srtoffset[i], &srtoffset[i], 0 );
1833 if ( srtlang && srtlang[i] )
1837 sub_config.default_track =
1838 ( srtdefault != -1 ) && ( srtdefault == i + 1 );
1839 sub_config.force = 0;
1840 strncpy( sub_config.src_filename, srtfile[i], 128);
1841 strncpy( sub_config.src_codeset, codeset, 40);
1842 sub_config.offset = offset;
1844 hb_srt_add( job, &sub_config, lang);
1848 if( native_language )
1852 audio = hb_list_audio_config_item(job->list_audio, 0);
1856 strncpy( audio_lang, audio->lang.iso639_2, sizeof( audio_lang ) );
1858 if( strncasecmp( native_language, audio_lang,
1859 sizeof( audio_lang ) ) != 0 )
1862 * Audio language is not the same as our native language.
1863 * If we have any subtitles in our native language they
1864 * should be selected here if they haven't already been.
1866 hb_subtitle_t *subtitle, *subtitle2 = NULL;
1867 int matched_track = 0;
1869 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1871 subtitle = hb_list_item( title->list_subtitle, i );
1873 if( strcmp( subtitle->iso639_2, native_language ) == 0 )
1876 * Found the first matching subtitle in our
1877 * native language. Is it already selected?
1879 for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
1881 subtitle2 = hb_list_item( job->list_subtitle, i );
1883 if( subtitle2->track == subtitle->track) {
1892 if( subtitle2 == NULL )
1895 * Not already selected, so select it.
1897 hb_subtitle_config_t sub_config;
1901 fprintf( stderr, "Warning: no matching audio for native language - using subtitles instead.\n");
1903 sub_config = subtitle->config;
1905 if( mux == HB_MUX_MKV || subtitle->format == TEXTSUB)
1907 sub_config.dest = PASSTHRUSUB;
1910 sub_config.force = 0;
1911 sub_config.default_track = 1;
1912 hb_subtitle_add( job, &sub_config, matched_track);
1929 if ( largeFileSize )
1931 job->largeFileSize = 1;
1935 job->mp4_optimize = 1;
1942 job->file = strdup( output );
1946 job->color_matrix = color_matrix;
1949 if( x264opts != NULL && *x264opts != '\0' )
1951 job->x264opts = x264opts;
1953 else /*avoids a bus error crash when options aren't specified*/
1955 job->x264opts = NULL;
1958 job->maxWidth = maxWidth;
1960 job->maxHeight = maxHeight;
1962 if( start_at_preview )
1964 job->start_at_preview = start_at_preview - 1;
1965 job->seek_points = preview_count;
1970 job->pts_to_stop = stop_at_pts;
1976 job->frame_to_stop = stop_at_frame;
1982 job->pts_to_start = start_at_pts;
1986 if( start_at_frame )
1988 job->frame_to_start = start_at_frame;
1997 * When subtitle scan is enabled do a fast pre-scan job
1998 * which will determine which subtitles to enable, if any.
2002 x264opts_tmp = job->x264opts;
2004 job->x264opts = NULL;
2006 job->indepth_scan = subtitle_scan;
2007 fprintf( stderr, "Subtitle Scan Enabled - enabling "
2008 "subtitles if found for foreign language segments\n");
2011 * Add the pre-scan job
2015 job->x264opts = x264opts_tmp;
2021 * If subtitle_scan is enabled then only turn it on
2022 * for the first pass and then off again for the
2027 job->indepth_scan = 0;
2031 x264opts2 = strdup(x264opts);
2035 * If turbo options have been selected then append them
2036 * to the x264opts now (size includes one ':' and the '\0')
2038 if( turbo_opts_enabled )
2040 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
2043 tmp_x264opts = malloc(size * sizeof(char));
2046 snprintf( tmp_x264opts, size, "%s:%s",
2047 x264opts, turbo_opts );
2051 * No x264opts to modify, but apply the turbo options
2052 * anyway as they may be modifying defaults
2054 snprintf( tmp_x264opts, size, "%s",
2057 x264opts = tmp_x264opts;
2059 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
2062 job->x264opts = x264opts;
2068 * On the second pass we turn off subtitle scan so that we
2069 * can actually encode using any subtitles that were auto
2070 * selected in the first pass (using the whacky select-subtitle
2071 * attribute of the job).
2073 job->indepth_scan = 0;
2075 job->x264opts = x264opts2;
2082 * Turn on subtitle scan if requested, note that this option
2083 * precludes encoding of any actual subtitles.
2086 job->indepth_scan = 0;
2094 #define p s.param.working
2095 case HB_STATE_SEARCHING:
2096 fprintf( stdout, "\rEncoding: task %d of %d, Searching for start time, %.2f %%",
2097 p.job_cur, p.job_count, 100.0 * p.progress );
2098 if( p.seconds > -1 )
2100 fprintf( stdout, " (ETA %02dh%02dm%02ds)",
2101 p.hours, p.minutes, p.seconds );
2106 case HB_STATE_WORKING:
2107 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
2108 p.job_cur, p.job_count, 100.0 * p.progress );
2109 if( p.seconds > -1 )
2111 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
2112 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
2113 p.hours, p.minutes, p.seconds );
2119 #define p s.param.muxing
2120 case HB_STATE_MUXING:
2122 if (show_mux_warning)
2124 fprintf( stdout, "\rMuxing: this may take awhile..." );
2126 show_mux_warning = 0;
2132 #define p s.param.workdone
2133 case HB_STATE_WORKDONE:
2134 /* Print error if any, then exit */
2138 fprintf( stderr, "\nRip done!\n" );
2140 case HB_ERROR_CANCELED:
2141 fprintf( stderr, "\nRip canceled.\n" );
2144 fprintf( stderr, "\nRip failed (error %x).\n",
2154 /****************************************************************************
2156 ****************************************************************************/
2157 static volatile int64_t i_die_date = 0;
2158 void SigHandler( int i_signal )
2163 i_die_date = hb_get_date();
2164 fprintf( stderr, "Signal %d received, terminating - do it "
2165 "again in case it gets stuck\n", i_signal );
2167 else if( i_die_date + 500 < hb_get_date() )
2169 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
2174 /****************************************************************************
2176 ****************************************************************************/
2177 static void ShowHelp()
2180 FILE* const out = stdout;
2183 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
2185 "### General Handbrake Options------------------------------------------------\n\n"
2186 " -h, --help Print help\n"
2187 " -u, --update Check for updates and exit\n"
2188 " -v, --verbose <#> Be verbose (optional argument: logging level)\n"
2189 " -C, --cpu Set CPU count (default: autodetected)\n"
2190 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
2191 " if the preset name has spaces, surround it with\n"
2192 " double quotation marks\n"
2193 " -z, --preset-list See a list of available built-in presets\n"
2194 " --no-dvdnav Do not use dvdnav for reading DVDs\n"
2195 " (experimental, enabled by default for testing)\n"
2198 "### Source Options-----------------------------------------------------------\n\n"
2199 " -i, --input <string> Set input device\n"
2200 " -t, --title <number> Select a title to encode (0 to scan only,\n"
2202 " -L, --longest Select the longest title\n"
2203 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
2204 " 1 to 3, or \"3\" for chapter 3 only,\n"
2205 " default: all chapters)\n"
2206 " --angle <number> Select the DVD angle\n"
2207 " --previews <#:B> Select how many preview images are generated (max 30),\n"
2208 " and whether or not they're stored to disk (0 or 1).\n"
2209 " (default: 10:0)\n"
2210 " --start-at-preview <#> Start encoding at a given preview.\n"
2211 " --start-at <unit:#> Start encoding at a given frame, duration (in seconds),\n"
2212 " or pts (on a 90kHz clock)\n"
2213 " --stop-at <unit:#> Stop encoding at a given frame, duration (in seconds),\n"
2214 " or pts (on a 90kHz clock)"
2217 "### Destination Options------------------------------------------------------\n\n"
2218 " -o, --output <string> Set output file name\n"
2219 " -f, --format <string> Set output format (mp4/mkv, default:\n"
2220 " autodetected from file name)\n"
2221 " -m, --markers Add chapter markers (mp4 and mkv output formats only)\n"
2222 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
2223 " 4 GB. Note: Breaks iPod, PS3 compatibility.\n"""
2224 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
2225 " -I, --ipod-atom Mark mp4 files so 5.5G iPods will accept them\n"
2229 "### Video Options------------------------------------------------------------\n\n"
2230 " -e, --encoder <string> Set video library encoder (ffmpeg,x264,theora)\n"
2231 " (default: ffmpeg)\n"
2232 " -x, --x264opts <string> Specify advanced x264 options in the\n"
2233 " same style as mencoder:\n"
2234 " option1=value1:option2=value2\n"
2235 " -q, --quality <float> Set video quality (0.0..1.0)\n"
2236 " -S, --size <MB> Set target size\n"
2237 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
2238 " -2, --two-pass Use two-pass mode\n"
2239 " -T, --turbo When using 2-pass use the turbo options\n"
2240 " on the first pass to improve speed\n"
2241 " (only works with x264, affects PSNR by about 0.05dB,\n"
2242 " and increases first pass speed two to four times)\n"
2243 " -r, --rate Set video framerate (" );
2244 for( i = 0; i < hb_video_rates_count; i++ )
2246 fprintf( out, hb_video_rates[i].string );
2247 if( i != hb_video_rates_count - 1 )
2248 fprintf( out, "/" );
2251 " Be aware that not specifying a framerate lets\n"
2252 " HandBrake preserve a source's time stamps,\n"
2253 " potentially creating variable framerate video\n"
2254 " --vfr, --cfr, --pfr Select variable, constant or peak-limited\n"
2255 " frame rate control. VFR preserves the source\n"
2256 " timing. CFR makes the output constant rate at\n"
2257 " the rate given by the -r flag (or the source's\n"
2258 " average rate if no -r is given). PFR doesn't\n"
2259 " allow the rate to go over the rate specified\n"
2260 " with the -r flag but won't change the source\n"
2261 " timing if it's below that rate.\n"
2262 " If none of these flags are given, the default\n"
2263 " is --cfr when -r is given and --vfr otherwise\n"
2266 "### Audio Options-----------------------------------------------------------\n\n"
2267 " -a, --audio <string> Select audio track(s), separated by commas\n"
2268 " More than one output track can be used for one\n"
2270 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
2271 " tracks, default: first one)\n" );
2275 " -E, --aencoder <string> Audio encoder(s) (ca_aac/faac/lame/vorbis/ac3/dts) \n"
2276 " ac3 and dts meaning passthrough\n"
2277 " Separated by commas for more than one audio track.\n"
2278 " (default: guessed)\n" );
2281 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3/dts) \n"
2282 " ac3 and dts meaning passthrough\n"
2283 " Separated by commas for more than one audio track.\n"
2284 " (default: guessed)\n" );
2287 " -B, --ab <kb/s> Set audio bitrate(s) (default: 160)\n"
2288 " Separated by commas for more than one audio track.\n"
2289 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
2290 " Separated by commas for more than one audio track.\n"
2291 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
2292 " -R, --arate Set audio samplerate(s) (" );
2293 for( i = 0; i < hb_audio_rates_count; i++ )
2295 fprintf( out, hb_audio_rates[i].string );
2296 if( i != hb_audio_rates_count - 1 )
2297 fprintf( out, "/" );
2299 fprintf( out, " kHz)\n"
2300 " Separated by commas for more than one audio track.\n"
2301 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
2302 " making soft sounds louder. Range is 1.0 to 4.0\n"
2303 " (too loud), with 1.5 - 2.5 being a useful range.\n"
2304 " Separated by commas for more than one audio track.\n"
2305 " -A, --aname <string> Audio track name(s),\n"
2306 " Separated by commas for more than one audio track.\n"
2309 "### Picture Settings---------------------------------------------------------\n\n"
2310 " -w, --width <number> Set picture width\n"
2311 " -l, --height <number> Set picture height\n"
2312 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
2313 " -Y, --maxHeight <#> Set maximum height\n"
2314 " -X, --maxWidth <#> Set maximum width\n"
2315 " --strict-anamorphic Store pixel aspect ratio in video stream\n"
2316 " --loose-anamorphic Store pixel aspect ratio with specified width\n"
2317 " --custom-anamorphic Store pixel aspect ratio in video stream and\n"
2318 " directly control all parameters.\n"
2319 " --display-width Set the width to scale the actual pixels to\n"
2320 " <number> at playback, for custom anamorphic.\n"
2321 " --keep-display-aspect Preserve the source's display aspect ratio\n"
2322 " when using custom anamorphic\n"
2323 " --pixel-aspect Set a custom pixel aspect for custom anamorphic\n"
2325 " (--display-width and --pixel-aspect are mutually\n"
2326 " exclusive and the former will override the latter)\n"
2327 " --itu-par Use wider, ITU pixel aspect values for loose and\n"
2328 " custom anamorphic, useful with underscanned sources\n"
2329 " --modulus Set the number you want the scaled pixel dimensions\n"
2330 " <number> to divide cleanly by. Does not affect strict\n"
2331 " anamorphic mode, which is always mod 2 (default: 16)\n"
2332 " -M --color-matrix Set the color space signaled by the output\n"
2333 " <601 or 709> (Bt.601 is mostly for SD content, Bt.709 for HD,\n"
2334 " default: set by resolution)\n"
2337 "### Filters---------------------------------------------------------\n\n"
2339 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
2340 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
2342 " <fast/slow/slower>\n"
2343 " -5, --decomb Selectively deinterlaces when it detects combing\n"
2344 " <MO:ME:MT:ST:BT:BX:BY:MG:VA:LA:DI:ER:NO:MD:PP:FD>\n"
2345 " (default: 7:2:6:9:80:16:16:10:20:20:4:2:50:24:1:-1)\n"
2346 " -9, --detelecine Detelecine (ivtc) video with pullup filter\n"
2347 " Note: this filter drops duplicate frames to\n"
2348 " restore the pre-telecine framerate, unless you\n"
2349 " specify a constant framerate (--rate 29.97)\n"
2350 " <L:R:T:B:SB:MP:FD> (default 1:1:4:4:0:0:-1)\n"
2351 " -8, --denoise Denoise video with hqdn3d filter\n"
2352 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
2354 " <weak/medium/strong>\n"
2355 " -7, --deblock Deblock video with pp7 filter\n"
2356 " <QP:M> (default 5:2)\n"
2357 " --rotate Flips images axes\n"
2358 " <M> (default 3)\n"
2359 " -g, --grayscale Grayscale encoding\n"
2362 "### Subtitle Options------------------------------------------------------------\n\n"
2363 " -s, --subtitle <string> Select subtitle track(s), separated by commas\n"
2364 " More than one output track can be used for one\n"
2366 " (\"1,2,3\" for multiple tracks.\n"
2367 " A special track name \"scan\" adds an extra 1st pass.\n"
2368 " This extra pass scans subtitles matching the\n"
2369 " language of the first audio or the language \n"
2370 " selected by --native-language.\n"
2371 " The one that's only used 10 percent of the time\n"
2372 " or less is selected. This should locate subtitles\n"
2373 " for short foreign language segments. Best used in\n"
2374 " conjunction with --subtitle-forced.\n"
2375 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
2376 " <string> the subtitle has the forced flag set. May be used in\n"
2377 " conjunction with \"scan\" track to auto-select\n"
2378 " a stream if it contains forced subtitles.\n"
2379 " Separated by commas for more than one audio track.\n"
2380 " (\"1,2,3\" for multiple tracks.\n"
2381 " If \"string\" is omitted, the first trac is forced.\n"
2382 " --subtitle-burn \"Burn\" the selected subtitle into the video track\n"
2383 " <number> If \"number\" is omitted, the first trac is burned.\n"
2384 " --subtitle-default Flag the selected subtitle as the default subtitle\n"
2385 " <number> to be displayed upon playback. Setting no default\n"
2386 " means no subtitle will be automatically displayed\n"
2387 " If \"number\" is omitted, the first trac is default.\n"
2388 " -N, --native-language Specifiy the your language preference. When the first\n"
2389 " <string> audio track does not match your native language then\n"
2390 " select the first subtitle that does. When used in\n"
2391 " conjunction with --native-dub the audio track is\n"
2392 " changed in preference to subtitles. Provide the\n"
2393 " language's iso639-2 code (fre, eng, spa, dut, et cetera)\n"
2394 " --native-dub Used in conjunction with --native-language\n"
2395 " requests that if no audio tracks are selected the\n"
2396 " default selected audio track will be the first one\n"
2397 " that matches the --native-language. If there are no\n"
2398 " matching audio tracks then the first matching\n"
2399 " subtitle track is used instead.\n"
2400 " --srt-file <string> SubRip SRT filename(s), separated by commas.\n"
2401 " --srt-codeset Character codeset(s) that the SRT file(s) are\n"
2402 " <string> encoded in, separted by commas.\n"
2403 " Use 'iconv -l' for a list of valid\n"
2404 " codesets. If not specified latin1 is assumed\n"
2405 " --srt-offset Offset in milli-seconds to apply to the SRT file(s)\n"
2406 " <string> separted by commas. If not specified zero is assumed.\n"
2407 " Offsets may be negative.\n"
2408 " --srt-lang <string> Language as an iso639-2 code fra, eng, spa et cetera)\n"
2409 " for the SRT file(s) separated by commas. If not specified\n"
2410 " then 'und' is used.\n"
2411 " --srt-default Flag the selected srt as the default subtitle\n"
2412 " <number> to be displayed upon playback. Setting no default\n"
2413 " means no subtitle will be automatically displayed\n"
2414 " If \"number\" is omitted, the first srt is default.\n"
2415 " \"number\" is an 1 based index into the srt-file list\n"
2422 /****************************************************************************
2424 ****************************************************************************/
2425 static void ShowPresets()
2427 printf("\n< Apple\n");
2429 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");
2431 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");
2433 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");
2435 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");
2439 printf("\n< Regular\n");
2441 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");
2443 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");
2447 printf("\n< Legacy\n");
2449 printf("\n + Classic: -b 1000 -a 1 -E faac -B 160 -6 dpl2 -R 48 -D 0.0 -f mp4\n");
2451 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");
2453 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");
2455 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");
2460 static char * hb_strndup( char * str, int len )
2463 int str_len = strlen( str );
2465 res = malloc( len > str_len ? str_len + 1 : len + 1 );
2466 strncpy( res, str, len );
2471 static char** str_split( char *str, char delem )
2478 if ( str == NULL || str[0] == 0 )
2480 ret = malloc( sizeof(char*) );
2485 // Find number of elements in the string
2488 while ( ( pos = strchr( pos, delem ) ) != NULL )
2494 ret = calloc( ( count + 1 ), sizeof(char*) );
2497 for ( i = 0; i < count - 1; i++ )
2499 end = strchr( pos, delem );
2500 ret[i] = hb_strndup(pos, end - pos);
2503 ret[i] = strdup(pos);
2508 /****************************************************************************
2510 ****************************************************************************/
2511 static int ParseOptions( int argc, char ** argv )
2514 #define PREVIEWS 257
2515 #define START_AT_PREVIEW 258
2516 #define START_AT 259
2520 #define DISPLAY_WIDTH 263
2521 #define PIXEL_ASPECT 264
2523 #define KEEP_DISPLAY_ASPECT 266
2524 #define SUB_BURNED 267
2525 #define SUB_DEFAULT 268
2526 #define NATIVE_DUB 269
2527 #define SRT_FILE 270
2528 #define SRT_CODESET 271
2529 #define SRT_OFFSET 272
2530 #define SRT_LANG 273
2531 #define SRT_DEFAULT 274
2532 #define ROTATE_FILTER 275
2536 static struct option long_options[] =
2538 { "help", no_argument, NULL, 'h' },
2539 { "update", no_argument, NULL, 'u' },
2540 { "verbose", optional_argument, NULL, 'v' },
2541 { "cpu", required_argument, NULL, 'C' },
2542 { "no-dvdnav", no_argument, NULL, DVDNAV },
2544 { "format", required_argument, NULL, 'f' },
2545 { "input", required_argument, NULL, 'i' },
2546 { "output", required_argument, NULL, 'o' },
2547 { "large-file", no_argument, NULL, '4' },
2548 { "optimize", no_argument, NULL, 'O' },
2549 { "ipod-atom", no_argument, NULL, 'I' },
2551 { "title", required_argument, NULL, 't' },
2552 { "longest", no_argument, NULL, 'L' },
2553 { "chapters", required_argument, NULL, 'c' },
2554 { "angle", required_argument, NULL, ANGLE },
2555 { "markers", optional_argument, NULL, 'm' },
2556 { "audio", required_argument, NULL, 'a' },
2557 { "mixdown", required_argument, NULL, '6' },
2558 { "drc", required_argument, NULL, 'D' },
2559 { "subtitle", required_argument, NULL, 's' },
2560 { "subtitle-forced", optional_argument, NULL, 'F' },
2561 { "subtitle-burned", optional_argument, NULL, SUB_BURNED },
2562 { "subtitle-default", optional_argument, NULL, SUB_DEFAULT },
2563 { "srt-file", required_argument, NULL, SRT_FILE },
2564 { "srt-codeset", required_argument, NULL, SRT_CODESET },
2565 { "srt-offset", required_argument, NULL, SRT_OFFSET },
2566 { "srt-lang", required_argument, NULL, SRT_LANG },
2567 { "srt-default", optional_argument, NULL, SRT_DEFAULT },
2568 { "native-language", required_argument, NULL,'N' },
2569 { "native-dub", no_argument, NULL, NATIVE_DUB },
2570 { "encoder", required_argument, NULL, 'e' },
2571 { "aencoder", required_argument, NULL, 'E' },
2572 { "two-pass", no_argument, NULL, '2' },
2573 { "deinterlace", optional_argument, NULL, 'd' },
2574 { "deblock", optional_argument, NULL, '7' },
2575 { "denoise", optional_argument, NULL, '8' },
2576 { "detelecine", optional_argument, NULL, '9' },
2577 { "decomb", optional_argument, NULL, '5' },
2578 { "grayscale", no_argument, NULL, 'g' },
2579 { "rotate", optional_argument, NULL, ROTATE_FILTER },
2580 { "strict-anamorphic", no_argument, &anamorphic_mode, 1 },
2581 { "loose-anamorphic", no_argument, &anamorphic_mode, 2 },
2582 { "custom-anamorphic", no_argument, &anamorphic_mode, 3 },
2583 { "display-width", required_argument, NULL, DISPLAY_WIDTH },
2584 { "keep-display-aspect", no_argument, &keep_display_aspect, 1 },
2585 { "pixel-aspect", required_argument, NULL, PIXEL_ASPECT },
2586 { "modulus", required_argument, NULL, MODULUS },
2587 { "itu-par", no_argument, &itu_par, 1 },
2588 { "width", required_argument, NULL, 'w' },
2589 { "height", required_argument, NULL, 'l' },
2590 { "crop", required_argument, NULL, 'n' },
2592 { "vb", required_argument, NULL, 'b' },
2593 { "quality", required_argument, NULL, 'q' },
2594 { "size", required_argument, NULL, 'S' },
2595 { "ab", required_argument, NULL, 'B' },
2596 { "rate", required_argument, NULL, 'r' },
2597 { "arate", required_argument, NULL, 'R' },
2598 { "x264opts", required_argument, NULL, 'x' },
2599 { "turbo", no_argument, NULL, 'T' },
2600 { "maxHeight", required_argument, NULL, 'Y' },
2601 { "maxWidth", required_argument, NULL, 'X' },
2602 { "preset", required_argument, NULL, 'Z' },
2603 { "preset-list", no_argument, NULL, 'z' },
2605 { "aname", required_argument, NULL, 'A' },
2606 { "color-matrix",required_argument, NULL, 'M' },
2607 { "previews", required_argument, NULL, PREVIEWS },
2608 { "start-at-preview", required_argument, NULL, START_AT_PREVIEW },
2609 { "start-at", required_argument, NULL, START_AT },
2610 { "stop-at", required_argument, NULL, STOP_AT },
2611 { "vfr", no_argument, &cfr, 0 },
2612 { "cfr", no_argument, &cfr, 1 },
2613 { "pfr", no_argument, &cfr, 2 },
2617 int option_index = 0;
2620 c = getopt_long( argc, argv,
2621 "hv::uC:f:4i:Io:t:Lc:m::M:a:A:6:s:UF::N:e:E:2dD:7895gOw:l:n:b:q:S:B:r:R:x:TY:X:Z:z",
2622 long_options, &option_index );
2631 /* option was handled entirely in getopt_long */
2640 if( optarg != NULL )
2642 debug = atoi( optarg );
2650 cpu = atoi( optarg );
2655 preset_name = strdup(optarg);
2665 format = strdup( optarg );
2668 input = strdup( optarg );
2670 char *devName = bsd_name_for_path( input ); // alloc
2673 if( device_is_dvd( devName ))
2676 input = malloc( strlen( "/dev/" ) + strlen( devName ) + 1 );
2677 sprintf( input, "/dev/%s", devName );
2684 output = strdup( optarg );
2697 titleindex = atoi( optarg );
2705 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
2707 chapter_start = start;
2710 else if( sscanf( optarg, "%d", &start ) == 1 )
2712 chapter_start = start;
2713 chapter_end = chapter_start;
2717 fprintf( stderr, "chapters: invalid syntax (%s)\n",
2724 angle = atoi( optarg );
2727 if( optarg != NULL )
2729 marker_file = strdup( optarg );
2731 chapter_markers = 1;
2734 if( optarg != NULL )
2736 atracks = strdup( optarg );
2745 if( optarg != NULL )
2747 mixdowns = strdup( optarg );
2751 if( optarg != NULL )
2753 dynamic_range_compression = strdup( optarg );
2757 subtracks = str_split( optarg, ',' );
2760 subforce = str_split( optarg, ',' );
2763 if( optarg != NULL )
2765 subburn = strdup( optarg );
2773 if( optarg != NULL )
2775 subdefault = strdup( optarg );
2783 native_language = strdup( optarg );
2789 srtfile = str_split( optarg, ',' );
2792 srtcodeset = str_split( optarg, ',' );
2795 srtoffset = str_split( optarg, ',' );
2798 srtlang = str_split( optarg, ',' );
2801 if( optarg != NULL )
2803 srtdefault = atoi( optarg );
2814 if( optarg != NULL )
2816 if (!( strcmp( optarg, "fast" ) ))
2818 deinterlace_opt = "-1";
2820 else if (!( strcmp( optarg, "slow" ) ))
2822 deinterlace_opt = "2";
2824 else if (!( strcmp( optarg, "slower" ) ))
2826 deinterlace_opt = "0";
2830 deinterlace_opt = strdup( optarg );
2836 if( optarg != NULL )
2838 deblock_opt = strdup( optarg );
2843 if( optarg != NULL )
2845 if (!( strcmp( optarg, "weak" ) ))
2847 denoise_opt = "2:1:2:3";
2849 else if (!( strcmp( optarg, "medium" ) ))
2851 denoise_opt = "3:2:2:3";
2853 else if (!( strcmp( optarg, "strong" ) ))
2855 denoise_opt = "7:7:5:5";
2859 denoise_opt = strdup( optarg );
2865 if( optarg != NULL )
2867 detelecine_opt = strdup( optarg );
2872 if( optarg != NULL )
2874 decomb_opt = strdup( optarg );
2882 if( optarg != NULL )
2884 rotate_opt = strdup( optarg );
2889 if( optarg != NULL )
2891 sscanf( optarg, "%i", &display_width );
2895 if( optarg != NULL )
2897 sscanf( optarg, "%i:%i", &par_width, &par_height );
2901 if( optarg != NULL )
2903 sscanf( optarg, "%i", &modulus );
2907 if( !strcasecmp( optarg, "ffmpeg" ) )
2909 vcodec = HB_VCODEC_FFMPEG;
2911 else if( !strcasecmp( optarg, "x264" ) )
2913 vcodec = HB_VCODEC_X264;
2915 else if( !strcasecmp( optarg, "x264b13" ) )
2917 vcodec = HB_VCODEC_X264;
2920 else if( !strcasecmp( optarg, "x264b30" ) )
2922 vcodec = HB_VCODEC_X264;
2925 else if( !strcasecmp( optarg, "theora" ) )
2927 vcodec = HB_VCODEC_THEORA;
2931 fprintf( stderr, "invalid codec (%s)\n", optarg );
2936 if( optarg != NULL )
2938 acodecs = strdup( optarg );
2942 width = atoi( optarg );
2945 height = atoi( optarg );
2950 char * tmp = optarg;
2951 for( i = 0; i < 4; i++ )
2955 crop[i] = strtol( tmp, &tmp, 0 );
2964 for( i = 0; i < hb_video_rates_count; i++ )
2966 if( !strcmp( optarg, hb_video_rates[i].string ) )
2968 vrate = hb_video_rates[i].rate;
2974 fprintf( stderr, "invalid framerate %s\n", optarg );
2976 else if ( cfr == 0 )
2983 if( optarg != NULL )
2985 arates = strdup( optarg );
2989 vbitrate = atoi( optarg );
2992 vquality = atof( optarg );
2995 size = atoi( optarg );
2998 if( optarg != NULL )
3000 abitrates = strdup( optarg );
3004 x264opts = strdup( optarg );
3007 turbo_opts_enabled = 1;
3010 maxHeight = atoi( optarg );
3013 maxWidth = atoi (optarg );
3016 if( optarg != NULL )
3018 anames = strdup( optarg );
3022 sscanf( optarg, "%i:%i", &preview_count, &store_previews );
3024 case START_AT_PREVIEW:
3025 start_at_preview = atoi( optarg );
3028 start_at_string = strdup( optarg );
3029 start_at_token = strtok( start_at_string, ":");
3030 if( !strcmp( start_at_token, "frame" ) )
3032 start_at_token = strtok( NULL, ":");
3033 start_at_frame = atoi(start_at_token);
3035 else if( !strcmp( start_at_token, "pts" ) )
3037 start_at_token = strtok( NULL, ":");
3038 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3040 else if( !strcmp( start_at_token, "duration" ) )
3042 start_at_token = strtok( NULL, ":");
3043 sscanf( start_at_token, "%"SCNd64, &start_at_pts );
3044 start_at_pts *= 90000LL;
3048 stop_at_string = strdup( optarg );
3049 stop_at_token = strtok( stop_at_string, ":");
3050 if( !strcmp( stop_at_token, "frame" ) )
3052 stop_at_token = strtok( NULL, ":");
3053 stop_at_frame = atoi(stop_at_token);
3055 else if( !strcmp( stop_at_token, "pts" ) )
3057 stop_at_token = strtok( NULL, ":");
3058 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3060 else if( !strcmp( stop_at_token, "duration" ) )
3062 stop_at_token = strtok( NULL, ":");
3063 sscanf( stop_at_token, "%"SCNd64, &stop_at_pts );
3064 stop_at_pts *= 90000LL;
3068 if( atoi( optarg ) == 601 )
3070 else if( atoi( optarg ) == 709 )
3074 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
3082 static int CheckOptions( int argc, char ** argv )
3089 if( input == NULL || *input == '\0' )
3091 fprintf( stderr, "Missing input device. Run %s --help for "
3092 "syntax.\n", argv[0] );
3097 if( titleindex > 0 )
3099 if( output == NULL || *output == '\0' )
3101 fprintf( stderr, "Missing output file name. Run %s --help "
3102 "for syntax.\n", argv[0] );
3108 char * p = strrchr( output, '.' );
3111 if( p && ( !strcasecmp( p, ".mp4" ) ||
3112 !strcasecmp( p, ".m4v" ) ) )
3118 default_acodec = HB_ACODEC_FAAC;
3120 else if( p && !strcasecmp(p, ".mkv" ) )
3123 default_acodec = HB_ACODEC_AC3;
3127 fprintf( stderr, "Output format couldn't be guessed "
3128 "from file name, using default.\n" );
3132 else if( !strcasecmp( format, "mp4" ) ||
3133 !strcasecmp( format, "m4v" ) )
3139 default_acodec = HB_ACODEC_FAAC;
3141 else if( !strcasecmp( format, "mkv" ) )
3144 default_acodec = HB_ACODEC_AC3;
3148 fprintf( stderr, "Invalid output format (%s). Possible "
3149 "choices are mp4, m4v and mkv\n.", format );
3157 static int get_acodec_for_string( char *codec )
3159 if( !strcasecmp( codec, "ac3" ) )
3161 return HB_ACODEC_AC3;
3163 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
3165 return HB_ACODEC_DCA;
3167 else if( !strcasecmp( codec, "lame" ) )
3169 return HB_ACODEC_LAME;
3171 else if( !strcasecmp( codec, "faac" ) )
3173 return HB_ACODEC_FAAC;
3175 else if( !strcasecmp( codec, "vorbis") )
3177 return HB_ACODEC_VORBIS;
3180 else if( !strcasecmp( codec, "ca_aac") )
3182 return HB_ACODEC_CA_AAC;
3191 static int is_sample_rate_valid(int rate)
3194 for( i = 0; i < hb_audio_rates_count; i++ )
3196 if (rate == hb_audio_rates[i].rate)
3203 /****************************************************************************
3206 * Returns the BSD device name for the block device that contains the
3207 * passed-in path. Returns NULL on failure.
3208 ****************************************************************************/
3209 static char* bsd_name_for_path(char *path)
3213 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
3219 // Get the volume reference number.
3220 FSCatalogInfo catalogInfo;
3221 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
3227 FSVolumeRefNum volRefNum = catalogInfo.volume;
3229 // Now let's get the device name
3230 GetVolParmsInfoBuffer volumeParms;
3231 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
3237 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
3238 // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
3239 if( volumeParms.vMVersion < 4 )
3244 // vMDeviceID might be zero as is reported with experimental ZFS (zfs-119) support in Leopard.
3245 if( !volumeParms.vMDeviceID )
3250 return strdup( volumeParms.vMDeviceID );
3253 /****************************************************************************
3256 * Returns whether or not the passed in BSD device represents a DVD, or other
3258 ****************************************************************************/
3259 static int device_is_dvd(char *device)
3261 io_service_t service = get_iokit_service(device);
3262 if( service == IO_OBJECT_NULL )
3266 int result = is_dvd_service(service);
3267 IOObjectRelease(service);
3271 /****************************************************************************
3274 * Returns the IOKit service object for the passed in BSD device name.
3275 ****************************************************************************/
3276 static io_service_t get_iokit_service( char *device )
3278 CFMutableDictionaryRef matchingDict;
3279 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
3280 if( matchingDict == NULL )
3282 return IO_OBJECT_NULL;
3284 // Fetch the object with the matching BSD node name. There should only be
3285 // one match, so IOServiceGetMatchingService is used instead of
3286 // IOServiceGetMatchingServices to simplify the code.
3287 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
3290 /****************************************************************************
3293 * Returns whether or not the service passed in is a DVD.
3295 * Searches for an IOMedia object that represents the entire (whole) media that
3296 * the volume is on. If the volume is on partitioned media, the whole media
3297 * object will be a parent of the volume's media object. If the media is not
3298 * partitioned, the volume's media object will be the whole media object.
3299 ****************************************************************************/
3300 static int is_dvd_service( io_service_t service )
3302 kern_return_t kernResult;
3305 // Create an iterator across all parents of the service object passed in.
3306 kernResult = IORegistryEntryCreateIterator( service,
3308 kIORegistryIterateRecursively | kIORegistryIterateParents,
3310 if( kernResult != KERN_SUCCESS )
3314 if( iter == IO_OBJECT_NULL )
3319 // A reference on the initial service object is released in the do-while
3320 // loop below, so add a reference to balance.
3321 IOObjectRetain( service );
3326 if( is_whole_media_service( service ) &&
3327 IOObjectConformsTo( service, kIODVDMediaClass) )
3331 IOObjectRelease( service );
3332 } while( !result && (service = IOIteratorNext( iter )) );
3333 IOObjectRelease( iter );
3338 /****************************************************************************
3339 * is_whole_media_service
3341 * Returns whether or not the service passed in is an IOMedia service and
3342 * represents the "whole" media instead of just a partition.
3344 * The whole media object is indicated in the IORegistry by the presence of a
3345 * property with the key "Whole" and value "Yes".
3346 ****************************************************************************/
3347 static int is_whole_media_service( io_service_t service )
3351 if( IOObjectConformsTo( service, kIOMediaClass ) )
3353 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
3354 CFSTR( kIOMediaWholeKey ),
3355 kCFAllocatorDefault,
3361 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
3362 CFRelease( wholeMedia );
3367 #endif // __APPLE_CC__