1 /* $Id: test.c,v 1.82 2005/11/19 08:25:54 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
17 #import <CoreServices/CoreServices.h>
18 #include <IOKit/IOKitLib.h>
19 #include <IOKit/storage/IOMedia.h>
20 #include <IOKit/storage/IODVDMedia.h>
24 static int debug = HB_DEBUG_NONE;
25 static int update = 0;
26 static char * input = NULL;
27 static char * output = NULL;
28 static char * format = NULL;
29 static int titleindex = 1;
30 static int longest_title = 0;
31 static int subtitle_scan = 0;
32 static int subtitle_force = 0;
33 static char * native_language = NULL;
34 static int twoPass = 0;
35 static int deinterlace = 0;
36 static char * deinterlace_opt = 0;
37 static int deblock = 0;
38 static char * deblock_opt = 0;
39 static int denoise = 0;
40 static char * denoise_opt = 0;
41 static int detelecine = 0;
42 static char * detelecine_opt = 0;
43 static int decomb = 0;
44 static char * decomb_opt = 0;
45 static int grayscale = 0;
46 static int vcodec = HB_VCODEC_FFMPEG;
47 static int h264_13 = 0;
48 static int h264_30 = 0;
49 static hb_list_t * audios = NULL;
50 static hb_audio_config_t * audio = NULL;
51 static int num_audio_tracks = 0;
52 static char * mixdowns = NULL;
53 static char * dynamic_range_compression = NULL;
54 static char * atracks = NULL;
55 static char * arates = NULL;
56 static char * abitrates = NULL;
57 static char * acodecs = NULL;
58 static int default_acodec = HB_ACODEC_FAAC;
59 static int default_arate = 48000;
60 static int default_abitrate = 160;
63 static int height = 0;
64 static int crop[4] = { -1,-1,-1,-1 };
67 static float vquality = -1.0;
68 static int vbitrate = 0;
71 static int pixelratio = 0;
72 static int loosePixelratio = 0;
73 static int modulus = 0;
74 static int par_height = 0;
75 static int par_width = 0;
76 static int chapter_start = 0;
77 static int chapter_end = 0;
78 static int chapter_markers = 0;
79 static char * marker_file = NULL;
81 static char *x264opts = NULL;
82 static char *x264opts2 = NULL;
83 static int maxHeight = 0;
84 static int maxWidth = 0;
85 static int turbo_opts_enabled = 0;
86 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
87 static int largeFileSize = 0;
88 static int preset = 0;
89 static char * preset_name = 0;
92 static int mp4_optimize = 0;
93 static int ipod_atom = 0;
95 /* Exit cleanly on Ctrl-C */
96 static volatile int die = 0;
97 static void SigHandler( int );
100 static void ShowCommands();
101 static void ShowHelp();
102 static void ShowPresets();
104 static int ParseOptions( int argc, char ** argv );
105 static int CheckOptions( int argc, char ** argv );
106 static int HandleEvents( hb_handle_t * h );
108 static int get_acodec_for_string( char *codec );
109 static int is_sample_rate_valid(int rate);
112 static char* bsd_name_for_path(char *path);
113 static int device_is_dvd(char *device);
114 static io_service_t get_iokit_service( char *device );
115 static int is_dvd_service( io_service_t service );
116 static is_whole_media_service( io_service_t service );
119 /* Only print the "Muxing..." message once */
120 static int show_mux_warning = 1;
122 /****************************************************************************
125 * When using the CLI just display using hb_log as we always did in the past
126 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
127 ****************************************************************************/
128 static void hb_cli_error_handler ( const char *errmsg )
130 fprintf( stderr, "ERROR: %s\n", errmsg );
133 int main( int argc, char ** argv )
139 audios = hb_list_init();
141 /* Parse command line */
142 if( ParseOptions( argc, argv ) ||
143 CheckOptions( argc, argv ) )
148 /* Register our error handler */
149 hb_register_error_handler(&hb_cli_error_handler);
152 h = hb_init( debug, update );
155 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
156 hb_get_version( h ), hb_get_build( h ) );
158 /* Check for update */
161 if( ( build = hb_check_update( h, &version ) ) > -1 )
163 fprintf( stderr, "You are using an old version of "
164 "HandBrake.\nLatest is %s (build %d).\n", version,
169 fprintf( stderr, "Your version of HandBrake is up to "
177 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
178 hb_get_cpu_count( h ) > 1 ? "s" : "" );
181 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
182 cpu > 1 ? "s" : "" );
183 hb_set_cpu_count( h, cpu );
186 /* Exit ASAP on Ctrl-C */
187 signal( SIGINT, SigHandler );
189 /* Feed libhb with a DVD to scan */
190 fprintf( stderr, "Opening %s...\n", input );
194 * We need to scan for all the titles in order to find the longest
198 hb_scan( h, input, titleindex );
203 #if !defined(SYS_BEOS)
213 FD_SET( STDIN_FILENO, &fds );
214 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
221 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
223 if( buf[size] == '\n' )
230 if( size >= 256 || buf[size] == '\n' )
235 fprintf( stdout, "\nEncoding Quit by user command\n" );
239 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
261 if( input ) free( input );
262 if( output ) free( output );
263 if( format ) free( format );
266 while( ( audio = hb_list_item( audios, 0 ) ) )
268 hb_list_rem( audios, audio );
271 hb_list_close( &audios );
273 if( mixdowns ) free( mixdowns );
274 if( dynamic_range_compression ) free( dynamic_range_compression );
275 if( atracks ) free( atracks );
276 if( arates ) free( arates );
277 if( abitrates ) free( abitrates );
278 if( acodecs ) free( acodecs );
279 if (native_language ) free (native_language );
280 if( x264opts ) free (x264opts );
281 if( x264opts2 ) free (x264opts2 );
282 if (preset_name) free (preset_name);
284 fprintf( stderr, "HandBrake has exited.\n" );
289 static void ShowCommands()
291 fprintf( stdout, "\nCommands:\n" );
292 fprintf( stdout, " [h]elp Show this message\n" );
293 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
294 fprintf( stdout, " [p]ause Pause encoding\n" );
295 fprintf( stdout, " [r]esume Resume encoding\n" );
298 static void PrintTitleInfo( hb_title_t * title )
300 hb_chapter_t * chapter;
301 hb_audio_config_t * audio;
302 hb_subtitle_t * subtitle;
305 fprintf( stderr, "+ title %d:\n", title->index );
306 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
307 title->vts, title->ttn, title->cell_start, title->cell_end,
308 title->block_count );
309 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
310 title->hours, title->minutes, title->seconds );
311 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
312 title->width, title->height,
313 (float) title->aspect,
314 (float) title->rate / title->rate_base );
315 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
316 title->crop[1], title->crop[2], title->crop[3] );
317 fprintf( stderr, " + chapters:\n" );
318 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
320 chapter = hb_list_item( title->list_chapter, i );
321 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
322 "%02d:%02d:%02d\n", chapter->index,
323 chapter->cell_start, chapter->cell_end,
324 chapter->block_count, chapter->hours, chapter->minutes,
327 fprintf( stderr, " + audio tracks:\n" );
328 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
330 audio = hb_list_audio_config_item( title->list_audio, i );
331 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
333 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
334 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
338 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
341 fprintf( stderr, " + subtitle tracks:\n" );
342 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
344 subtitle = hb_list_item( title->list_subtitle, i );
345 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
349 if(title->detected_interlacing)
351 /* Interlacing was found in half or more of the preview frames */
352 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
357 static int HandleEvents( hb_handle_t * h )
360 int tmp_num_audio_tracks;
362 hb_get_state( h, &s );
369 #define p s.param.scanning
370 case HB_STATE_SCANNING:
371 /* Show what title is currently being scanned */
372 fprintf( stderr, "Scanning title %d", p.title_cur );
374 fprintf( stderr, " of %d", p.title_count );
375 fprintf( stderr, "...\n" );
379 case HB_STATE_SCANDONE:
386 /* Audio argument string parsing variables */
390 int mixdown = HB_AMIXDOWN_DOLBYPLII;
392 /* Audio argument string parsing variables */
394 list = hb_get_titles( h );
396 if( !hb_list_count( list ) )
398 /* No valid title, stop right there */
399 fprintf( stderr, "No title found.\n" );
406 int longest_title_idx=0;
407 int longest_title_pos=-1;
408 int longest_title_time=0;
411 fprintf( stderr, "Searching for longest title...\n" );
413 for( i = 0; i < hb_list_count( list ); i++ )
415 title = hb_list_item( list, i );
416 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
417 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
418 i, title->index, title_time );
419 if( longest_title_time < title_time )
421 longest_title_time = title_time;
422 longest_title_pos = i;
423 longest_title_idx = title->index;
426 if( longest_title_pos == -1 )
428 fprintf( stderr, "No longest title found.\n" );
432 titleindex = longest_title_idx;
433 fprintf( stderr, "Found longest title, setting title to %d\n",
436 title = hb_list_item( list, longest_title_pos);
438 title = hb_list_item( list, 0 );
443 /* Scan-only mode, print infos and exit */
445 for( i = 0; i < hb_list_count( list ); i++ )
447 title = hb_list_item( list, i );
448 PrintTitleInfo( title );
454 /* Set job settings */
457 PrintTitleInfo( title );
459 if( chapter_start && chapter_end )
461 job->chapter_start = MAX( job->chapter_start,
463 job->chapter_end = MIN( job->chapter_end,
465 job->chapter_end = MAX( job->chapter_start,
471 fprintf( stderr, "+ Using preset: %s", preset_name);
473 if (!strcmp(preset_name, "Animation"))
476 vcodec = HB_VCODEC_X264;
477 job->vbitrate = 1000;
478 default_abitrate = 160;
479 default_acodec = HB_ACODEC_FAAC;
480 x264opts = strdup("ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2");
482 deinterlace_opt = "0";
483 job->chapter_markers = 1;
486 turbo_opts_enabled = 1;
489 if (!strcmp(preset_name, "AppleTV"))
493 acodecs = strdup("faac,ac3");
497 job->largeFileSize = 1;
498 vcodec = HB_VCODEC_X264;
499 job->vbitrate = 2500;
500 default_abitrate = 160;
501 default_arate = 48000;
502 default_acodec = HB_ACODEC_FAAC;
503 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
504 job->chapter_markers = 1;
508 if (!strcmp(preset_name, "Bedlam"))
511 vcodec = HB_VCODEC_X264;
512 job->vbitrate = 1800;
513 default_acodec = HB_ACODEC_AC3;
514 x264opts = strdup("ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1");
515 job->chapter_markers = 1;
518 turbo_opts_enabled = 1;
521 if (!strcmp(preset_name, "Blind"))
525 default_abitrate = 128;
526 default_acodec = HB_ACODEC_FAAC;
528 job->chapter_markers = 1;
531 if (!strcmp(preset_name, "Broke"))
534 vcodec = HB_VCODEC_X264;
536 default_abitrate = 128;
537 default_acodec = HB_ACODEC_FAAC;
539 x264opts = strdup("ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip");
540 job->chapter_markers = 1;
542 turbo_opts_enabled = 1;
545 if (!strcmp(preset_name, "Classic"))
548 job->vbitrate = 1000;
549 default_abitrate = 160;
550 default_acodec = HB_ACODEC_FAAC;
553 if (!strcmp(preset_name, "Constant Quality Rate"))
556 vcodec = HB_VCODEC_X264;
557 job->vquality = 0.64709997177124023;
559 default_acodec = HB_ACODEC_AC3;
560 x264opts = strdup("ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh");
561 job->chapter_markers = 1;
565 if (!strcmp(preset_name, "Deux Six Quatre"))
568 vcodec = HB_VCODEC_X264;
569 job->vbitrate = 1600;
570 default_acodec = HB_ACODEC_AC3;
571 x264opts = strdup("ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip");
572 job->chapter_markers = 1;
575 turbo_opts_enabled = 1;
578 if (!strcmp(preset_name, "Film"))
581 vcodec = HB_VCODEC_X264;
582 job->vbitrate = 1800;
583 default_acodec = HB_ACODEC_AC3;
584 x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip");
585 job->chapter_markers = 1;
588 turbo_opts_enabled = 1;
591 if (!strcmp(preset_name, "iPhone / iPod Touch"))
595 vcodec = HB_VCODEC_X264;
597 default_abitrate = 128;
598 default_arate = 48000;
599 default_acodec = HB_ACODEC_FAAC;
601 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
602 job->chapter_markers = 1;
605 if (!strcmp(preset_name, "iPod High-Rez"))
609 vcodec = HB_VCODEC_X264;
610 job->vbitrate = 1500;
611 default_abitrate = 160;
612 default_arate = 48000;
613 default_acodec = HB_ACODEC_FAAC;
615 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
616 job->chapter_markers = 1;
619 if (!strcmp(preset_name, "iPod Low-Rez"))
623 vcodec = HB_VCODEC_X264;
625 default_abitrate = 160;
626 default_arate = 48000;
627 default_acodec = HB_ACODEC_FAAC;
629 x264opts = strdup("level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
630 job->chapter_markers = 1;
633 if (!strcmp(preset_name, "Normal"))
636 vcodec = HB_VCODEC_X264;
637 job->vbitrate = 1500;
638 default_abitrate = 160;
639 default_acodec = HB_ACODEC_FAAC;
640 x264opts = strdup("ref=2:bframes=2:subme=5:me=umh");
641 job->chapter_markers = 1;
644 turbo_opts_enabled = 1;
647 if (!strcmp(preset_name, "PS3"))
650 vcodec = HB_VCODEC_X264;
651 job->vbitrate = 2500;
652 default_abitrate = 160;
653 default_acodec = HB_ACODEC_FAAC;
654 x264opts = strdup("level=41:subme=5:me=umh");
658 if (!strcmp(preset_name, "PSP"))
661 job->vbitrate = 1024;
662 default_abitrate = 128;
663 default_arate = 48000;
664 default_acodec = HB_ACODEC_FAAC;
667 job->chapter_markers = 1;
670 if (!strcmp(preset_name, "QuickTime"))
673 vcodec = HB_VCODEC_X264;
674 job->vbitrate = 2000;
675 default_abitrate = 160;
676 default_acodec = HB_ACODEC_FAAC;
677 x264opts = strdup("ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip");
678 job->chapter_markers = 1;
681 turbo_opts_enabled = 1;
684 if (!strcmp(preset_name, "Television"))
687 vcodec = HB_VCODEC_X264;
688 job->vbitrate = 1300;
689 default_abitrate = 160;
690 default_acodec = HB_ACODEC_FAAC;
691 x264opts = strdup("ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip");
693 deinterlace_opt = "0";
695 denoise_opt = "2:1:2:3";
696 job->chapter_markers = 1;
698 turbo_opts_enabled = 1;
701 if (!strcmp(preset_name, "Xbox 360"))
704 vcodec = HB_VCODEC_X264;
705 job->vbitrate = 2000;
706 default_abitrate = 160;
707 default_acodec = HB_ACODEC_FAAC;
708 x264opts = strdup("level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1");
713 if ( chapter_markers )
715 job->chapter_markers = chapter_markers;
717 if( marker_file != NULL )
719 hb_csv_file_t * file = hb_open_csv_file( marker_file );
720 hb_csv_cell_t * cell;
724 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
728 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
732 /* Parse the cells */
733 while( NULL != ( cell = hb_read_next_cell( file ) ) )
735 /* We have a chapter number */
736 if( cell->cell_col == 0 )
738 row = cell->cell_row;
739 chapter = atoi( cell->cell_text );
742 /* We have a chapter name */
743 if( cell->cell_col == 1 && row == cell->cell_row )
745 /* If we have a valid chapter, copy the string an terminate it */
746 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
748 hb_chapter_t * chapter_s;
750 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
751 strncpy(chapter_s->title, cell->cell_text, 1023);
752 chapter_s->title[1023] = '\0';
757 hb_dispose_cell( cell );
760 hb_close_csv_file( file );
767 int number_of_chapters = hb_list_count(job->title->list_chapter);
770 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
772 hb_chapter_t * chapter_s;
773 chapter_s = hb_list_item( job->title->list_chapter, chapter);
774 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
775 chapter_s->title[1023] = '\0';
780 if( crop[0] >= 0 && crop[1] >= 0 &&
781 crop[2] >= 0 && crop[3] >= 0 )
783 memcpy( job->crop, crop, 4 * sizeof( int ) );
786 job->deinterlace = deinterlace;
787 job->grayscale = grayscale;
790 job->pixel_ratio = 2;
793 job->modulus = modulus;
795 if( par_width && par_height )
797 job->pixel_ratio = 3;
798 job->pixel_aspect_width = par_width;
799 job->pixel_aspect_height = par_height;
804 job->pixel_ratio = pixelratio;
813 /* Add selected filters */
814 job->filters = hb_list_init();
817 hb_filter_detelecine.settings = detelecine_opt;
818 hb_list_add( job->filters, &hb_filter_detelecine );
822 hb_filter_decomb.settings = decomb_opt;
823 hb_list_add( job->filters, &hb_filter_decomb );
827 hb_filter_deinterlace.settings = deinterlace_opt;
828 hb_list_add( job->filters, &hb_filter_deinterlace );
832 hb_filter_deblock.settings = deblock_opt;
833 hb_list_add( job->filters, &hb_filter_deblock );
837 hb_filter_denoise.settings = denoise_opt;
838 hb_list_add( job->filters, &hb_filter_denoise );
841 if( width && height )
844 job->height = height;
849 hb_fix_aspect( job, HB_KEEP_WIDTH );
851 else if( height && !loosePixelratio)
853 job->height = height;
854 hb_fix_aspect( job, HB_KEEP_HEIGHT );
856 else if( !width && !height && !pixelratio && !loosePixelratio )
858 hb_fix_aspect( job, HB_KEEP_WIDTH );
860 else if (!width && loosePixelratio)
862 /* Default to full width when one isn't specified for loose anamorphic */
863 job->width = title->width - job->crop[2] - job->crop[3];
864 /* The height will be thrown away in hb.c but calculate it anyway */
865 hb_fix_aspect( job, HB_KEEP_WIDTH );
868 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) || (vcodec == HB_VCODEC_FFMPEG) ) )
870 job->vquality = vquality;
875 job->vquality = -1.0;
876 job->vbitrate = vbitrate;
880 job->vcodec = vcodec;
884 job->h264_level = 13;
888 job->h264_level = 30;
893 job->vrate = 27000000;
894 job->vrate_base = vrate;
897 /* Grab audio tracks */
900 char * token = strtok(atracks, ",");
903 int track_start, track_end;
904 while( token != NULL )
906 audio = calloc(1, sizeof(*audio));
907 hb_audio_config_init(audio);
908 if (strlen(token) >= 3)
910 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
913 for (i = track_start - 1; i < track_end; i++)
915 if (i != track_start - 1)
917 audio = calloc(1, sizeof(*audio));
918 hb_audio_config_init(audio);
921 audio->out.track = num_audio_tracks++;
922 hb_list_add(audios, audio);
925 else if( !strcasecmp(token, "none" ) )
927 audio->in.track = audio->out.track = -1;
928 audio->out.codec = 0;
929 hb_list_add(audios, audio);
934 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
941 audio->in.track = atoi(token) - 1;
942 audio->out.track = num_audio_tracks++;
943 hb_list_add(audios, audio);
945 token = strtok(NULL, ",");
949 /* Parse audio tracks */
950 if( hb_list_count(audios) == 0 )
952 /* Create a new audio track with default settings */
953 audio = calloc(1, sizeof(*audio));
954 hb_audio_config_init(audio);
955 /* Add it to our audios */
956 hb_list_add(audios, audio);
959 tmp_num_audio_tracks = num_audio_tracks = hb_list_count(audios);
960 for (i = 0; i < tmp_num_audio_tracks; i++)
962 audio = hb_list_item(audios, 0);
963 if( (audio == NULL) || (audio->in.track == -1) ||
964 (audio->out.track == -1) || (audio->out.codec == 0) )
970 if( hb_audio_add( job, audio ) == 0 )
972 fprintf(stderr, "ERROR: Invalid audio input track '%u', exiting.\n",
973 audio->in.track + 1 );
978 hb_list_rem(audios, audio);
987 char * token = strtok(acodecs, ",");
990 while ( token != NULL )
992 if ((acodec = get_acodec_for_string(token)) == -1)
994 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
995 acodec = default_acodec;
997 if( i < num_audio_tracks )
999 audio = hb_list_audio_config_item(job->list_audio, i);
1000 audio->out.codec = acodec;
1004 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, i - 1 );
1005 hb_audio_config_t audio;
1009 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
1011 hb_audio_config_init(&audio);
1012 audio.in.track = last_audio->in.track;
1013 audio.out.track = num_audio_tracks++;
1014 audio.out.codec = acodec;
1015 hb_audio_add(job, &audio);
1019 fprintf(stderr, "Audio codecs and no valid audio tracks, skipping codec %s\n", token);
1022 token = strtok(NULL, ",");
1026 if( i < num_audio_tracks )
1028 /* We have fewer inputs than audio tracks, use the default codec for
1029 * this container for the remaining tracks. Unless we only have one input
1030 * then use that codec instead.
1033 acodec = default_acodec;
1034 for ( ; i < num_audio_tracks; i++)
1036 audio = hb_list_audio_config_item(job->list_audio, i);
1037 audio->out.codec = acodec;
1046 char * token = strtok(arates, ",");
1049 while ( token != NULL )
1051 arate = atoi(token);
1052 audio = hb_list_audio_config_item(job->list_audio, i);
1055 for( j = 0; j < hb_audio_rates_count; j++ )
1057 if( !strcmp( token, hb_audio_rates[j].string ) )
1059 arate = hb_audio_rates[j].rate;
1066 if (!is_sample_rate_valid(arate))
1068 fprintf(stderr, "Invalid sample rate %d, using input rate %d\n", arate, audio->in.samplerate);
1069 arate = audio->in.samplerate;
1072 audio->out.samplerate = arate;
1073 if( (++i) >= num_audio_tracks )
1074 break; /* We have more inputs than audio tracks, oops */
1078 fprintf(stderr, "Ignoring sample rate %d, no audio tracks\n", arate);
1080 token = strtok(NULL, ",");
1083 if (i < num_audio_tracks)
1085 /* We have fewer inputs than audio tracks, use default sample rate.
1086 * Unless we only have one input, then use that for all tracks.
1089 arate = audio->in.samplerate;
1090 for ( ; i < num_audio_tracks; i++)
1092 audio = hb_list_audio_config_item(job->list_audio, i);
1093 audio->out.samplerate = arate;
1102 char * token = strtok(abitrates, ",");
1105 while ( token != NULL )
1107 abitrate = atoi(token);
1108 audio = hb_list_audio_config_item(job->list_audio, i);
1112 audio->out.bitrate = abitrate;
1113 if( (++i) >= num_audio_tracks )
1114 break; /* We have more inputs than audio tracks, oops */
1118 fprintf(stderr, "Ignoring bitrate %d, no audio tracks\n", abitrate);
1120 token = strtok(NULL, ",");
1123 if (i < num_audio_tracks)
1125 /* We have fewer inputs than audio tracks, use the default bitrate
1126 * for the remaining tracks. Unless we only have one input, then use
1127 * that for all tracks.
1130 abitrate = default_abitrate;
1131 for (; i < num_audio_tracks; i++)
1133 audio = hb_list_audio_config_item(job->list_audio, i);
1134 audio->out.bitrate = abitrate;
1141 if ( dynamic_range_compression )
1143 char * token = strtok(dynamic_range_compression, ",");
1145 token = dynamic_range_compression;
1146 while ( token != NULL )
1148 d_r_c = atof(token);
1149 audio = hb_list_audio_config_item(job->list_audio, i);
1152 audio->out.dynamic_range_compression = d_r_c;
1153 if( (++i) >= num_audio_tracks )
1154 break; /* We have more inputs than audio tracks, oops */
1158 fprintf(stderr, "Ignoring drc, no audio tracks\n");
1160 token = strtok(NULL, ",");
1163 if (i < num_audio_tracks)
1165 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1166 * tracks. Unless we only have one input, then use the same DRC for all
1171 for (; i < num_audio_tracks; i++)
1173 audio = hb_list_audio_config_item(job->list_audio, i);
1174 audio->out.dynamic_range_compression = d_r_c;
1183 char * token = strtok(mixdowns, ",");
1186 while ( token != NULL )
1188 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1189 audio = hb_list_audio_config_item(job->list_audio, i);
1192 audio->out.mixdown = mixdown;
1193 if( (++i) >= num_audio_tracks )
1194 break; /* We have more inputs than audio tracks, oops */
1198 fprintf(stderr, "Ignoring mixdown, no audio tracks\n");
1200 token = strtok(NULL, ",");
1203 if (i < num_audio_tracks)
1205 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1206 * we only have one input, then use that.
1209 mixdown = HB_AMIXDOWN_DOLBYPLII;
1210 for (; i < num_audio_tracks; i++)
1212 audio = hb_list_audio_config_item(job->list_audio, i);
1213 audio->out.mixdown = mixdown;
1220 job->vbitrate = hb_calc_bitrate( job, size );
1221 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1227 job->subtitle = sub - 1;
1230 if( native_language )
1232 job->native_language = strdup( native_language );
1240 if ( largeFileSize )
1242 job->largeFileSize = 1;
1246 job->mp4_optimize = 1;
1253 job->file = strdup( output );
1260 if( x264opts != NULL && *x264opts != '\0' )
1262 job->x264opts = x264opts;
1264 else /*avoids a bus error crash when options aren't specified*/
1266 job->x264opts = NULL;
1269 job->maxWidth = maxWidth;
1271 job->maxHeight = maxHeight;
1273 if( subtitle_force )
1275 job->subtitle_force = subtitle_force;
1283 * When subtitle scan is enabled do a fast pre-scan job
1284 * which will determine which subtitles to enable, if any.
1288 x264opts_tmp = job->x264opts;
1290 job->x264opts = NULL;
1292 job->indepth_scan = subtitle_scan;
1293 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1294 "subtitles if found for foreign language segments\n");
1295 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1296 *(job->select_subtitle) = NULL;
1299 * Add the pre-scan job
1303 job->x264opts = x264opts_tmp;
1309 * If subtitle_scan is enabled then only turn it on
1310 * for the first pass and then off again for the
1313 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1315 job->select_subtitle = NULL;
1319 job->indepth_scan = 0;
1323 x264opts2 = strdup(x264opts);
1327 * If turbo options have been selected then append them
1328 * to the x264opts now (size includes one ':' and the '\0')
1330 if( turbo_opts_enabled )
1332 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1335 tmp_x264opts = malloc(size * sizeof(char));
1338 snprintf( tmp_x264opts, size, "%s:%s",
1339 x264opts, turbo_opts );
1343 * No x264opts to modify, but apply the turbo options
1344 * anyway as they may be modifying defaults
1346 snprintf( tmp_x264opts, size, "%s",
1349 x264opts = tmp_x264opts;
1351 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1354 job->x264opts = x264opts;
1358 job->select_subtitle = subtitle_tmp;
1362 * On the second pass we turn off subtitle scan so that we
1363 * can actually encode using any subtitles that were auto
1364 * selected in the first pass (using the whacky select-subtitle
1365 * attribute of the job).
1367 job->indepth_scan = 0;
1369 job->x264opts = x264opts2;
1376 * Turn on subtitle scan if requested, note that this option
1377 * precludes encoding of any actual subtitles.
1380 job->indepth_scan = 0;
1388 #define p s.param.working
1389 case HB_STATE_WORKING:
1390 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1391 p.job_cur, p.job_count, 100.0 * p.progress );
1392 if( p.seconds > -1 )
1394 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1395 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1396 p.hours, p.minutes, p.seconds );
1402 #define p s.param.muxing
1403 case HB_STATE_MUXING:
1405 if (show_mux_warning)
1407 fprintf( stdout, "\rMuxing: this may take awhile..." );
1409 show_mux_warning = 0;
1415 #define p s.param.workdone
1416 case HB_STATE_WORKDONE:
1417 /* Print error if any, then exit */
1421 fprintf( stderr, "\nRip done!\n" );
1423 case HB_ERROR_CANCELED:
1424 fprintf( stderr, "\nRip canceled.\n" );
1427 fprintf( stderr, "\nRip failed (error %x).\n",
1437 /****************************************************************************
1439 ****************************************************************************/
1440 static volatile int64_t i_die_date = 0;
1441 void SigHandler( int i_signal )
1446 i_die_date = hb_get_date();
1447 fprintf( stderr, "Signal %d received, terminating - do it "
1448 "again in case it gets stuck\n", i_signal );
1450 else if( i_die_date + 500 < hb_get_date() )
1452 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1457 /****************************************************************************
1459 ****************************************************************************/
1460 static void ShowHelp()
1465 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1467 "### General Handbrake Options------------------------------------------------\n\n"
1468 " -h, --help Print help\n"
1469 " -u, --update Check for updates and exit\n"
1470 " -v, --verbose Be verbose\n"
1471 " -C, --cpu Set CPU count (default: autodetected)\n"
1472 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1473 " if the preset name has spaces, surround it with\n"
1474 " double quotation marks\n"
1475 " -z, --preset-list See a list of available built-in presets\n"
1478 "### Source Options-----------------------------------------------------------\n\n"
1479 " -i, --input <string> Set input device\n"
1480 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1482 " -L, --longest Select the longest title\n"
1483 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1484 " 1 to 3, or \"3\" for chapter 3 only,\n"
1485 " default: all chapters)\n"
1488 "### Destination Options------------------------------------------------------\n\n"
1489 " -o, --output <string> Set output file name\n"
1490 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1491 " autodetected from file name)\n"
1492 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1493 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1494 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1495 " -I, --ipod-atom Mark mp4 files so iPods will accept them\n"
1498 "### Picture Settings---------------------------------------------------------\n\n"
1499 " -w, --width <number> Set picture width\n"
1500 " -l, --height <number> Set picture height\n"
1501 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1502 " -Y, --maxHeight <#> Set maximum height\n"
1503 " -X, --maxWidth <#> Set maximum width\n"
1504 " -s, --subtitle <number> Select subtitle (default: none)\n"
1505 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1506 " the one that's only used 10 percent of the time\n"
1507 " or less. This should locate subtitles for short\n"
1508 " foreign language segments. Best used in conjunction\n"
1509 " with --subtitle-forced.\n"
1510 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1511 " the subtitle has the forced flag set. May be used in\n"
1512 " conjunction with --subtitle-scan to auto-select\n"
1513 " a stream if it contains forced subtitles.\n"
1514 " -N, --native-language Select subtitles with this language if it does not\n"
1515 " <string> match the Audio language. Provide the language's\n"
1516 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1517 " -m, --markers Add chapter markers (mp4 output format only)\n"
1520 "### Video Options------------------------------------------------------------\n\n"
1521 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1522 " x264,theora default: ffmpeg)\n"
1523 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1524 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1525 " -S, --size <MB> Set target size\n"
1526 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1527 " -r, --rate Set video framerate (" );
1528 for( i = 0; i < hb_video_rates_count; i++ )
1530 fprintf( stderr, hb_video_rates[i].string );
1531 if( i != hb_video_rates_count - 1 )
1532 fprintf( stderr, "/" );
1534 fprintf( stderr, ")\n"
1536 " -2, --two-pass Use two-pass mode\n"
1537 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1538 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1540 " <fast/slow/slower>\n"
1541 " -7, --deblock Deblock video with pp7 filter\n"
1542 " <QP:M> (default 0:2)\n"
1543 " -8, --denoise Denoise video with hqdn3d filter\n"
1544 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1546 " <weak/medium/strong>\n"
1547 " -9, --detelecine Detelecine video with pullup filter\n"
1548 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1549 " -5, --decomb Selectively deinterlaces when it detects combing\n"
1550 " <MO:ME:MT:ST:BT:BX:BY> (default: 1:2:6:9:80:16:16)\n"
1551 " -g, --grayscale Grayscale encoding\n"
1552 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1553 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1554 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
1555 " the dimensions to divide cleanly by (default 16)\n"
1556 " and the pixel ratio to use (default autodetected)\n)"
1562 "### Audio Options-----------------------------------------------------------\n\n"
1563 " -a, --audio <string> Select audio channel(s), separated by commas\n"
1564 " More than one output track can be used for one\n"
1566 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1567 " tracks, default: first one)\n"
1568 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1569 " ac3 meaning passthrough\n"
1570 " Seperated by commas for more than one audio track.\n"
1571 " (default: guessed)\n"
1572 " -B, --ab <kb/s> Set audio bitrate(s) (default: 128)\n"
1573 " Seperated by commas for more than one audio track.\n"
1574 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1575 " Seperated by commas for more than one audio track.\n"
1576 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1577 " -R, --arate Set audio samplerate(s) (" );
1578 for( i = 0; i < hb_audio_rates_count; i++ )
1580 fprintf( stderr, hb_audio_rates[i].string );
1581 if( i != hb_audio_rates_count - 1 )
1582 fprintf( stderr, "/" );
1584 fprintf( stderr, " kHz)\n"
1585 " Seperated by commas for more than one audio track.\n"
1586 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1587 " making soft sounds louder. Range is 1.0 to 4.0\n"
1588 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1589 " Seperated by commas for more than one audio track.\n"
1595 "### Advanced Options---------------------------------------------------------\n\n"
1596 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1597 " same style as mencoder:\n"
1598 " option1=value1:option2=value2\n"
1599 " -T, --turbo When using 2-pass use the turbo options\n"
1600 " on the first pass to improve speed\n"
1601 " (only works with x264, affects PSNR by about 0.05dB,\n"
1602 " and increases first pass speed two to four times)\n"
1603 " -V, --vfr Perform variable framerate detelecine on NTSC video\n"
1607 /****************************************************************************
1609 ****************************************************************************/
1610 static void ShowPresets()
1612 printf("\n+ Animation: -e x264 -b 1000 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" -m -p -2 -T -x ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2\n");
1614 printf("\n+ AppleTV: -e x264 -b 2500 -B 160 -R 48 -E faac,ac3 -f mp4 -4 -m -p -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0\n");
1616 printf("\n+ Bedlam: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=esa:subme=7:me-range=64:analyse=all:8x8dct:trellis=1:no-fast-pskip:no-dct-decimate:filter=-2,-1\n");
1618 printf("\n+ Blind: -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1620 printf("\n+ Broke: -e x264 -S 695 -B 128 -R 48 -E faac -f mp4 -w 640 -m -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1622 printf("\n+ Classic: -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1624 printf("\n+ Constant Quality Rate: -e x264 -q 0.64709997177124023 -E ac3 -f mkv -m -p -x ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh\n");
1626 printf("\n+ Deux Six Quatre: -e x264 -b 1600 -E ac3 -f mkv -m -p -2 -T -x ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1628 printf("\n+ Film: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:analyse=all:8x8dct:trellis=1:no-fast-pskip\n");
1630 printf("\n+ iPhone / iPod Touch: -e x264 -b 960 -B 128 -R 48 -E faac -f mp4 -I -w 480 -m -x level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1\n");
1632 printf("\n+ iPod High-Rez: -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -I -w 640 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1634 printf("\n+ iPod Low-Rez: -e x264 -b 700 -B 160 -R 48 -E faac -f mp4 -I -w 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1636 printf("\n+ Normal: -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=2:bframes=2:subme=5:me=umh\n");
1638 printf("\n+ PS3: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1640 printf("\n+ PSP: -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1642 printf("\n+ QuickTime: -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip\n");
1644 printf("\n+ Television: -e x264 -b 1300 -B 160 -R 48 -E faac -f mkv --deinterlace=\"slower\" --denoise=\"weak\" -m -2 -T -x ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip\n");
1646 printf("\n+ Xbox 360: -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -p -x level=40:ref=2:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:no-fast-pskip:filter=-2,-1\n");
1649 /****************************************************************************
1651 ****************************************************************************/
1652 static int ParseOptions( int argc, char ** argv )
1656 static struct option long_options[] =
1658 { "help", no_argument, NULL, 'h' },
1659 { "update", no_argument, NULL, 'u' },
1660 { "verbose", no_argument, NULL, 'v' },
1661 { "cpu", required_argument, NULL, 'C' },
1663 { "format", required_argument, NULL, 'f' },
1664 { "input", required_argument, NULL, 'i' },
1665 { "output", required_argument, NULL, 'o' },
1666 { "large-file", no_argument, NULL, '4' },
1667 { "optimize", no_argument, NULL, 'O' },
1668 { "ipod-atom", no_argument, NULL, 'I' },
1670 { "title", required_argument, NULL, 't' },
1671 { "longest", no_argument, NULL, 'L' },
1672 { "chapters", required_argument, NULL, 'c' },
1673 { "markers", optional_argument, NULL, 'm' },
1674 { "audio", required_argument, NULL, 'a' },
1675 { "mixdown", required_argument, NULL, '6' },
1676 { "drc", required_argument, NULL, 'D' },
1677 { "subtitle", required_argument, NULL, 's' },
1678 { "subtitle-scan", no_argument, NULL, 'U' },
1679 { "subtitle-forced", no_argument, NULL, 'F' },
1680 { "native-language", required_argument, NULL,'N' },
1682 { "encoder", required_argument, NULL, 'e' },
1683 { "aencoder", required_argument, NULL, 'E' },
1684 { "two-pass", no_argument, NULL, '2' },
1685 { "deinterlace", optional_argument, NULL, 'd' },
1686 { "deblock", optional_argument, NULL, '7' },
1687 { "denoise", optional_argument, NULL, '8' },
1688 { "detelecine", optional_argument, NULL, '9' },
1689 { "decomb", optional_argument, NULL, '5' },
1690 { "grayscale", no_argument, NULL, 'g' },
1691 { "pixelratio", no_argument, NULL, 'p' },
1692 { "loosePixelratio", optional_argument, NULL, 'P' },
1693 { "width", required_argument, NULL, 'w' },
1694 { "height", required_argument, NULL, 'l' },
1695 { "crop", required_argument, NULL, 'n' },
1697 { "vb", required_argument, NULL, 'b' },
1698 { "quality", required_argument, NULL, 'q' },
1699 { "size", required_argument, NULL, 'S' },
1700 { "ab", required_argument, NULL, 'B' },
1701 { "rate", required_argument, NULL, 'r' },
1702 { "arate", required_argument, NULL, 'R' },
1703 { "cqp", no_argument, NULL, 'Q' },
1704 { "x264opts", required_argument, NULL, 'x' },
1705 { "turbo", no_argument, NULL, 'T' },
1706 { "maxHeight", required_argument, NULL, 'Y' },
1707 { "maxWidth", required_argument, NULL, 'X' },
1708 { "preset", required_argument, NULL, 'Z' },
1709 { "preset-list", no_argument, NULL, 'z' },
1710 { "vfr", no_argument, NULL, 'V' },
1715 int option_index = 0;
1718 c = getopt_long( argc, argv,
1719 "hvuC:f:4i:Io:t:Lc:m::a:6:s:UFN:e:E:2dD:7895gpOP::w:l:n:b:q:S:B:r:R:Qx:TY:X:VZ:z",
1720 long_options, &option_index );
1735 debug = HB_DEBUG_ALL;
1738 cpu = atoi( optarg );
1743 preset_name = strdup(optarg);
1750 format = strdup( optarg );
1753 input = strdup( optarg );
1755 char *devName = bsd_name_for_path( input );
1756 if( devName == NULL )
1760 if( device_is_dvd( devName ) )
1762 char *newInput = malloc( strlen("/dev/") + strlen( devName ) + 1);
1763 sprintf( newInput, "/dev/%s", devName );
1770 output = strdup( optarg );
1783 titleindex = atoi( optarg );
1791 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1793 chapter_start = start;
1796 else if( sscanf( optarg, "%d", &start ) == 1 )
1798 chapter_start = start;
1799 chapter_end = chapter_start;
1803 fprintf( stderr, "chapters: invalid syntax (%s)\n",
1810 if( optarg != NULL )
1812 marker_file = strdup( optarg );
1814 chapter_markers = 1;
1817 if( optarg != NULL )
1819 atracks = strdup( optarg );
1827 if( optarg != NULL )
1829 mixdowns = strdup( optarg );
1833 if( optarg != NULL )
1835 dynamic_range_compression = strdup( optarg );
1839 sub = atoi( optarg );
1848 native_language = strdup( optarg );
1854 if( optarg != NULL )
1856 if (!( strcmp( optarg, "fast" ) ))
1858 deinterlace_opt = "-1";
1860 else if (!( strcmp( optarg, "slow" ) ))
1862 deinterlace_opt = "2";
1864 else if (!( strcmp( optarg, "slower" ) ))
1866 deinterlace_opt = "0";
1870 deinterlace_opt = strdup( optarg );
1876 if( optarg != NULL )
1878 deblock_opt = strdup( optarg );
1883 if( optarg != NULL )
1885 if (!( strcmp( optarg, "weak" ) ))
1887 denoise_opt = "2:1:2:3";
1889 else if (!( strcmp( optarg, "medium" ) ))
1891 denoise_opt = "3:2:2:3";
1893 else if (!( strcmp( optarg, "strong" ) ))
1895 denoise_opt = "7:7:5:5";
1899 denoise_opt = strdup( optarg );
1905 if( optarg != NULL )
1907 detelecine_opt = strdup( optarg );
1912 if( optarg != NULL )
1914 decomb_opt = strdup( optarg );
1925 loosePixelratio = 1;
1926 if( optarg != NULL )
1928 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
1932 if( !strcasecmp( optarg, "ffmpeg" ) )
1934 vcodec = HB_VCODEC_FFMPEG;
1936 else if( !strcasecmp( optarg, "xvid" ) )
1938 vcodec = HB_VCODEC_XVID;
1940 else if( !strcasecmp( optarg, "x264" ) )
1942 vcodec = HB_VCODEC_X264;
1944 else if( !strcasecmp( optarg, "x264b13" ) )
1946 vcodec = HB_VCODEC_X264;
1949 else if( !strcasecmp( optarg, "x264b30" ) )
1951 vcodec = HB_VCODEC_X264;
1954 else if( !strcasecmp( optarg, "theora" ) )
1956 vcodec = HB_VCODEC_THEORA;
1960 fprintf( stderr, "invalid codec (%s)\n", optarg );
1965 if( optarg != NULL )
1967 acodecs = strdup( optarg );
1971 width = atoi( optarg );
1974 height = atoi( optarg );
1979 char * tmp = optarg;
1980 for( i = 0; i < 4; i++ )
1984 crop[i] = strtol( tmp, &tmp, 0 );
1993 for( i = 0; i < hb_video_rates_count; i++ )
1995 if( !strcmp( optarg, hb_video_rates[i].string ) )
1997 vrate = hb_video_rates[i].rate;
2003 fprintf( stderr, "invalid framerate %s\n", optarg );
2008 if( optarg != NULL )
2010 arates = strdup( optarg );
2014 vbitrate = atoi( optarg );
2017 vquality = atof( optarg );
2020 size = atoi( optarg );
2023 if( optarg != NULL )
2025 abitrates = strdup( optarg );
2032 x264opts = strdup( optarg );
2035 turbo_opts_enabled = 1;
2038 maxHeight = atoi( optarg );
2041 maxWidth = atoi (optarg );
2048 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2056 static int CheckOptions( int argc, char ** argv )
2063 if( input == NULL || *input == '\0' )
2065 fprintf( stderr, "Missing input device. Run %s --help for "
2066 "syntax.\n", argv[0] );
2071 if( titleindex > 0 )
2073 if( output == NULL || *output == '\0' )
2075 fprintf( stderr, "Missing output file name. Run %s --help "
2076 "for syntax.\n", argv[0] );
2082 char * p = strrchr( output, '.' );
2085 if( p && !strcasecmp( p, ".avi" ) )
2088 default_acodec = HB_ACODEC_LAME;
2090 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2091 !strcasecmp( p, ".m4v" ) ) )
2097 default_acodec = HB_ACODEC_FAAC;
2099 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2100 !strcasecmp( p, ".ogg" ) ) )
2103 default_acodec = HB_ACODEC_VORBIS;
2105 else if( p && !strcasecmp(p, ".mkv" ) )
2108 default_acodec = HB_ACODEC_AC3;
2112 fprintf( stderr, "Output format couldn't be guessed "
2113 "from file name, using default.\n" );
2117 else if( !strcasecmp( format, "avi" ) )
2120 default_acodec = HB_ACODEC_LAME;
2122 else if( !strcasecmp( format, "mp4" ) ||
2123 !strcasecmp( format, "m4v" ) )
2129 default_acodec = HB_ACODEC_FAAC;
2131 else if( !strcasecmp( format, "ogm" ) ||
2132 !strcasecmp( format, "ogg" ) )
2135 default_acodec = HB_ACODEC_VORBIS;
2137 else if( !strcasecmp( format, "mkv" ) )
2140 default_acodec = HB_ACODEC_AC3;
2144 fprintf( stderr, "Invalid output format (%s). Possible "
2145 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2153 static int get_acodec_for_string( char *codec )
2155 if( !strcasecmp( codec, "ac3" ) )
2157 return HB_ACODEC_AC3;
2159 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2161 return HB_ACODEC_DCA;
2163 else if( !strcasecmp( codec, "lame" ) )
2165 return HB_ACODEC_LAME;
2167 else if( !strcasecmp( codec, "faac" ) )
2169 return HB_ACODEC_FAAC;
2171 else if( !strcasecmp( codec, "vorbis") )
2173 return HB_ACODEC_VORBIS;
2181 static int is_sample_rate_valid(int rate)
2184 for( i = 0; i < hb_audio_rates_count; i++ )
2186 if (rate == hb_audio_rates[i].rate)
2193 /****************************************************************************
2196 * Returns the BSD device name for the block device that contains the
2197 * passed-in path. Returns NULL on failure.
2198 ****************************************************************************/
2199 static char* bsd_name_for_path(char *path)
2203 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2209 // Get the volume reference number.
2210 FSCatalogInfo catalogInfo;
2211 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2217 FSVolumeRefNum volRefNum = catalogInfo.volume;
2219 // Now let's get the device name
2220 GetVolParmsInfoBuffer volumeParms;
2221 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2227 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the
2228 // vMDeviceID field. It is actually a char * value. This is mentioned in the
2229 // header CoreServices/CarbonCore/Files.h.
2230 return volumeParms.vMDeviceID;
2233 /****************************************************************************
2236 * Returns whether or not the passed in BSD device represents a DVD, or other
2238 ****************************************************************************/
2239 static int device_is_dvd(char *device)
2241 io_service_t service = get_iokit_service(device);
2242 if( service == IO_OBJECT_NULL )
2246 int result = is_dvd_service(service);
2247 IOObjectRelease(service);
2251 /****************************************************************************
2254 * Returns the IOKit service object for the passed in BSD device name.
2255 ****************************************************************************/
2256 static io_service_t get_iokit_service( char *device )
2258 CFMutableDictionaryRef matchingDict;
2259 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2260 if( matchingDict == NULL )
2262 return IO_OBJECT_NULL;
2264 // Fetch the object with the matching BSD node name. There should only be
2265 // one match, so IOServiceGetMatchingService is used instead of
2266 // IOServiceGetMatchingServices to simplify the code.
2267 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2270 /****************************************************************************
2273 * Returns whether or not the service passed in is a DVD.
2275 * Searches for an IOMedia object that represents the entire (whole) media that
2276 * the volume is on. If the volume is on partitioned media, the whole media
2277 * object will be a parent of the volume's media object. If the media is not
2278 * partitioned, the volume's media object will be the whole media object.
2279 ****************************************************************************/
2280 static int is_dvd_service( io_service_t service )
2282 kern_return_t kernResult;
2285 // Create an iterator across all parents of the service object passed in.
2286 kernResult = IORegistryEntryCreateIterator( service,
2288 kIORegistryIterateRecursively | kIORegistryIterateParents,
2290 if( kernResult != KERN_SUCCESS )
2294 if( iter == IO_OBJECT_NULL )
2299 // A reference on the initial service object is released in the do-while
2300 // loop below, so add a reference to balance.
2301 IOObjectRetain( service );
2306 if( is_whole_media_service( service ) &&
2307 IOObjectConformsTo( service, kIODVDMediaClass) )
2311 IOObjectRelease( service );
2312 } while( !result && (service = IOIteratorNext( iter )) );
2313 IOObjectRelease( iter );
2318 /****************************************************************************
2319 * is_whole_media_service
2321 * Returns whether or not the service passed in is an IOMedia service and
2322 * represents the "whole" media instead of just a partition.
2324 * The whole media object is indicated in the IORegistry by the presence of a
2325 * property with the key "Whole" and value "Yes".
2326 ****************************************************************************/
2327 static is_whole_media_service( io_service_t service )
2331 if( IOObjectConformsTo( service, kIOMediaClass ) )
2333 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2334 CFSTR( kIOMediaWholeKey ),
2335 kCFAllocatorDefault,
2341 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2342 CFRelease( wholeMedia );
2347 #endif // __APPLE_CC__