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 grayscale = 0;
44 static int vcodec = HB_VCODEC_FFMPEG;
45 static int h264_13 = 0;
46 static int h264_30 = 0;
47 static hb_list_t * audios = NULL;
48 static hb_audio_config_t * audio = NULL;
49 static int num_audio_tracks = 0;
50 static char * mixdowns = NULL;
51 static char * dynamic_range_compression = NULL;
52 static char * arates = NULL;
53 static char * abitrates = NULL;
54 static char * acodecs = NULL;
55 static int default_acodec = HB_ACODEC_FAAC;
56 static int default_arate = 44100;
57 static int default_abitrate = 128;
60 static int height = 0;
61 static int crop[4] = { -1,-1,-1,-1 };
64 static float vquality = -1.0;
65 static int vbitrate = 0;
68 static int pixelratio = 0;
69 static int loosePixelratio = 0;
70 static int modulus = 0;
71 static int par_height = 0;
72 static int par_width = 0;
73 static int chapter_start = 0;
74 static int chapter_end = 0;
75 static int chapter_markers = 0;
76 static char * marker_file = NULL;
78 static char *x264opts = NULL;
79 static char *x264opts2 = NULL;
80 static int maxHeight = 0;
81 static int maxWidth = 0;
82 static int turbo_opts_enabled = 0;
83 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
84 static int largeFileSize = 0;
85 static int preset = 0;
86 static char * preset_name = 0;
88 static int mp4_optimize = 0;
89 static int ipod_atom = 0;
91 /* Exit cleanly on Ctrl-C */
92 static volatile int die = 0;
93 static void SigHandler( int );
96 static void ShowCommands();
97 static void ShowHelp();
98 static void ShowPresets();
100 static int ParseOptions( int argc, char ** argv );
101 static int CheckOptions( int argc, char ** argv );
102 static int HandleEvents( hb_handle_t * h );
104 static int get_acodec_for_string( char *codec );
105 static int is_sample_rate_valid(int rate);
108 static char* bsd_name_for_path(char *path);
109 static int device_is_dvd(char *device);
110 static io_service_t get_iokit_service( char *device );
111 static int is_dvd_service( io_service_t service );
112 static is_whole_media_service( io_service_t service );
115 /* Only print the "Muxing..." message once */
116 static int show_mux_warning = 1;
118 /****************************************************************************
121 * When using the CLI just display using hb_log as we always did in the past
122 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
123 ****************************************************************************/
124 static void hb_cli_error_handler ( const char *errmsg )
126 fprintf( stderr, "ERROR: %s", errmsg );
129 int main( int argc, char ** argv )
135 audios = hb_list_init();
137 /* Parse command line */
138 if( ParseOptions( argc, argv ) ||
139 CheckOptions( argc, argv ) )
144 /* Register our error handler */
145 hb_register_error_handler(&hb_cli_error_handler);
148 h = hb_init( debug, update );
151 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
152 hb_get_version( h ), hb_get_build( h ) );
154 /* Check for update */
157 if( ( build = hb_check_update( h, &version ) ) > -1 )
159 fprintf( stderr, "You are using an old version of "
160 "HandBrake.\nLatest is %s (build %d).\n", version,
165 fprintf( stderr, "Your version of HandBrake is up to "
173 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
174 hb_get_cpu_count( h ) > 1 ? "s" : "" );
177 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
178 cpu > 1 ? "s" : "" );
179 hb_set_cpu_count( h, cpu );
182 /* Exit ASAP on Ctrl-C */
183 signal( SIGINT, SigHandler );
185 /* Feed libhb with a DVD to scan */
186 fprintf( stderr, "Opening %s...\n", input );
190 * We need to scan for all the titles in order to find the longest
194 hb_scan( h, input, titleindex );
199 #if !defined(SYS_BEOS)
209 FD_SET( STDIN_FILENO, &fds );
210 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
217 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
219 if( buf[size] == '\n' )
226 if( size >= 256 || buf[size] == '\n' )
231 fprintf( stdout, "\nEncoding Quit by user command\n" );
235 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
257 if( input ) free( input );
258 if( output ) free( output );
259 if( format ) free( format );
262 while( ( audio = hb_list_item( audios, 0 ) ) )
264 hb_list_rem( audios, audio );
267 hb_list_close( &audios );
269 if( mixdowns ) free( mixdowns );
270 if( dynamic_range_compression ) free( dynamic_range_compression );
271 if( arates ) free( arates );
272 if( abitrates ) free( abitrates );
273 if( acodecs ) free( acodecs );
274 if (native_language ) free (native_language );
275 if( x264opts ) free (x264opts );
276 if( x264opts2 ) free (x264opts2 );
277 if (preset_name) free (preset_name);
279 fprintf( stderr, "HandBrake has exited.\n" );
284 static void ShowCommands()
286 fprintf( stdout, "\nCommands:\n" );
287 fprintf( stdout, " [h]elp Show this message\n" );
288 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
289 fprintf( stdout, " [p]ause Pause encoding\n" );
290 fprintf( stdout, " [r]esume Resume encoding\n" );
293 static void PrintTitleInfo( hb_title_t * title )
295 hb_chapter_t * chapter;
296 hb_audio_config_t * audio;
297 hb_subtitle_t * subtitle;
300 fprintf( stderr, "+ title %d:\n", title->index );
301 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
302 title->vts, title->ttn, title->cell_start, title->cell_end,
303 title->block_count );
304 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
305 title->hours, title->minutes, title->seconds );
306 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
307 title->width, title->height,
308 (float) title->aspect / HB_ASPECT_BASE,
309 (float) title->rate / title->rate_base );
310 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
311 title->crop[1], title->crop[2], title->crop[3] );
312 fprintf( stderr, " + chapters:\n" );
313 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
315 chapter = hb_list_item( title->list_chapter, i );
316 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
317 "%02d:%02d:%02d\n", chapter->index,
318 chapter->cell_start, chapter->cell_end,
319 chapter->block_count, chapter->hours, chapter->minutes,
322 fprintf( stderr, " + audio tracks:\n" );
323 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
325 audio = hb_list_audio_config_item( title->list_audio, i );
326 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
328 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
329 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
333 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
336 fprintf( stderr, " + subtitle tracks:\n" );
337 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
339 subtitle = hb_list_item( title->list_subtitle, i );
340 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
344 if(title->detected_interlacing)
346 /* Interlacing was found in half or more of the preview frames */
347 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
352 static int HandleEvents( hb_handle_t * h )
355 hb_get_state( h, &s );
362 #define p s.param.scanning
363 case HB_STATE_SCANNING:
364 /* Show what title is currently being scanned */
365 fprintf( stderr, "Scanning title %d", p.title_cur );
367 fprintf( stderr, " of %d", p.title_count );
368 fprintf( stderr, "...\n" );
372 case HB_STATE_SCANDONE:
379 /* Audio argument string parsing variables */
383 int mixdown = HB_AMIXDOWN_DOLBYPLII;
385 /* Audio argument string parsing variables */
387 list = hb_get_titles( h );
389 if( !hb_list_count( list ) )
391 /* No valid title, stop right there */
392 fprintf( stderr, "No title found.\n" );
399 int longest_title_idx=0;
400 int longest_title_pos=-1;
401 int longest_title_time=0;
404 fprintf( stderr, "Searching for longest title...\n" );
406 for( i = 0; i < hb_list_count( list ); i++ )
408 title = hb_list_item( list, i );
409 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
410 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
411 i, title->index, title_time );
412 if( longest_title_time < title_time )
414 longest_title_time = title_time;
415 longest_title_pos = i;
416 longest_title_idx = title->index;
419 if( longest_title_pos == -1 )
421 fprintf( stderr, "No longest title found.\n" );
425 titleindex = longest_title_idx;
426 fprintf( stderr, "Found longest title, setting title to %d\n",
429 title = hb_list_item( list, longest_title_pos);
431 title = hb_list_item( list, 0 );
436 /* Scan-only mode, print infos and exit */
438 for( i = 0; i < hb_list_count( list ); i++ )
440 title = hb_list_item( list, i );
441 PrintTitleInfo( title );
447 /* Set job settings */
450 PrintTitleInfo( title );
452 if( chapter_start && chapter_end )
454 job->chapter_start = MAX( job->chapter_start,
456 job->chapter_end = MIN( job->chapter_end,
458 job->chapter_end = MAX( job->chapter_start,
464 fprintf( stderr, "+ Using preset: %s", preset_name);
466 if (!strcmp(preset_name, "Animation"))
469 vcodec = HB_VCODEC_X264;
470 job->vbitrate = 1000;
471 default_abitrate = 160;
472 default_arate = 48000;
473 default_acodec = HB_ACODEC_FAAC;
474 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");
476 deinterlace_opt = "0";
477 job->chapter_markers = 1;
480 turbo_opts_enabled = 1;
483 if (!strcmp(preset_name, "AppleTV"))
487 acodecs = strdup("faac,ac3");
491 job->largeFileSize = 1;
492 vcodec = HB_VCODEC_X264;
493 job->vbitrate = 2500;
494 default_abitrate = 160;
495 default_arate = 48000;
496 default_acodec = HB_ACODEC_FAAC;
497 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
498 job->chapter_markers = 1;
502 if (!strcmp(preset_name, "Bedlam"))
505 vcodec = HB_VCODEC_X264;
506 job->vbitrate = 1800;
507 default_acodec = HB_ACODEC_AC3;
508 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");
509 job->chapter_markers = 1;
512 turbo_opts_enabled = 1;
515 if (!strcmp(preset_name, "Blind"))
519 default_abitrate = 128;
520 default_arate = 48000;
521 default_acodec = HB_ACODEC_FAAC;
523 job->chapter_markers = 1;
526 if (!strcmp(preset_name, "Broke"))
529 vcodec = HB_VCODEC_X264;
531 default_abitrate = 128;
532 default_arate = 48000;
533 default_acodec = HB_ACODEC_FAAC;
535 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");
536 job->chapter_markers = 1;
538 turbo_opts_enabled = 1;
541 if (!strcmp(preset_name, "Classic"))
544 job->vbitrate = 1000;
545 default_abitrate = 160;
546 default_arate = 48000;
547 default_acodec = HB_ACODEC_FAAC;
550 if (!strcmp(preset_name, "Constant Quality Rate"))
553 vcodec = HB_VCODEC_X264;
554 job->vquality = 0.64709997177124023;
556 default_acodec = HB_ACODEC_AC3;
557 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");
558 job->chapter_markers = 1;
562 if (!strcmp(preset_name, "Deux Six Quatre"))
565 vcodec = HB_VCODEC_X264;
566 job->vbitrate = 1600;
567 default_acodec = HB_ACODEC_AC3;
568 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");
569 job->chapter_markers = 1;
572 turbo_opts_enabled = 1;
575 if (!strcmp(preset_name, "Film"))
578 vcodec = HB_VCODEC_X264;
579 job->vbitrate = 1800;
580 default_acodec = HB_ACODEC_AC3;
581 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");
582 job->chapter_markers = 1;
585 turbo_opts_enabled = 1;
588 if (!strcmp(preset_name, "iPhone / iPod Touch"))
592 vcodec = HB_VCODEC_X264;
594 default_abitrate = 128;
595 default_arate = 48000;
596 default_acodec = HB_ACODEC_FAAC;
598 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
599 job->chapter_markers = 1;
602 if (!strcmp(preset_name, "iPod High-Rez"))
606 vcodec = HB_VCODEC_X264;
607 job->vbitrate = 1500;
608 default_abitrate = 160;
609 default_arate = 48000;
610 default_acodec = HB_ACODEC_FAAC;
612 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");
613 job->chapter_markers = 1;
616 if (!strcmp(preset_name, "iPod Low-Rez"))
620 vcodec = HB_VCODEC_X264;
622 default_abitrate = 160;
623 default_arate = 48000;
624 default_acodec = HB_ACODEC_FAAC;
626 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");
627 job->chapter_markers = 1;
630 if (!strcmp(preset_name, "Normal"))
633 vcodec = HB_VCODEC_X264;
634 job->vbitrate = 1500;
635 default_abitrate = 160;
636 default_arate = 48000;
637 default_acodec = HB_ACODEC_FAAC;
638 x264opts = strdup("ref=2:bframes=2:subme=5:me=umh");
639 job->chapter_markers = 1;
642 turbo_opts_enabled = 1;
645 if (!strcmp(preset_name, "PS3"))
648 vcodec = HB_VCODEC_X264;
649 job->vbitrate = 2500;
650 default_abitrate = 160;
651 default_arate = 48000;
652 default_acodec = HB_ACODEC_FAAC;
653 x264opts = strdup("level=41:subme=5:me=umh");
657 if (!strcmp(preset_name, "PSP"))
660 job->vbitrate = 1024;
661 default_abitrate = 128;
662 default_arate = 48000;
663 default_acodec = HB_ACODEC_FAAC;
666 job->chapter_markers = 1;
669 if (!strcmp(preset_name, "QuickTime"))
672 vcodec = HB_VCODEC_X264;
673 job->vbitrate = 2000;
674 default_abitrate = 160;
675 default_arate = 48000;
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_arate = 48000;
691 default_acodec = HB_ACODEC_FAAC;
692 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");
694 deinterlace_opt = "0";
696 denoise_opt = "2:1:2:3";
697 job->chapter_markers = 1;
699 turbo_opts_enabled = 1;
702 if (!strcmp(preset_name, "Xbox 360"))
705 vcodec = HB_VCODEC_X264;
706 job->vbitrate = 2000;
707 default_abitrate = 160;
708 default_arate = 48000;
709 default_acodec = HB_ACODEC_FAAC;
710 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");
715 if ( chapter_markers )
717 job->chapter_markers = chapter_markers;
719 if( marker_file != NULL )
721 hb_csv_file_t * file = hb_open_csv_file( marker_file );
722 hb_csv_cell_t * cell;
726 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
730 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
734 /* Parse the cells */
735 while( NULL != ( cell = hb_read_next_cell( file ) ) )
737 /* We have a chapter number */
738 if( cell->cell_col == 0 )
740 row = cell->cell_row;
741 chapter = atoi( cell->cell_text );
744 /* We have a chapter name */
745 if( cell->cell_col == 1 && row == cell->cell_row )
747 /* If we have a valid chapter, copy the string an terminate it */
748 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
750 hb_chapter_t * chapter_s;
752 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
753 strncpy(chapter_s->title, cell->cell_text, 1023);
754 chapter_s->title[1023] = '\0';
759 hb_dispose_cell( cell );
762 hb_close_csv_file( file );
769 int number_of_chapters = hb_list_count(job->title->list_chapter);
772 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
774 hb_chapter_t * chapter_s;
775 chapter_s = hb_list_item( job->title->list_chapter, chapter);
776 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
777 chapter_s->title[1023] = '\0';
782 if( crop[0] >= 0 && crop[1] >= 0 &&
783 crop[2] >= 0 && crop[3] >= 0 )
785 memcpy( job->crop, crop, 4 * sizeof( int ) );
788 job->deinterlace = deinterlace;
789 job->grayscale = grayscale;
792 job->pixel_ratio = 2;
795 job->modulus = modulus;
797 if( par_width && par_height )
799 job->pixel_ratio = 3;
800 job->pixel_aspect_width = par_width;
801 job->pixel_aspect_height = par_height;
806 job->pixel_ratio = pixelratio;
815 /* Add selected filters */
816 job->filters = hb_list_init();
819 hb_filter_detelecine.settings = detelecine_opt;
820 hb_list_add( job->filters, &hb_filter_detelecine );
824 hb_filter_deinterlace.settings = deinterlace_opt;
825 hb_list_add( job->filters, &hb_filter_deinterlace );
829 hb_filter_deblock.settings = deblock_opt;
830 hb_list_add( job->filters, &hb_filter_deblock );
834 hb_filter_denoise.settings = denoise_opt;
835 hb_list_add( job->filters, &hb_filter_denoise );
838 if( width && height )
841 job->height = height;
846 hb_fix_aspect( job, HB_KEEP_WIDTH );
848 else if( height && !loosePixelratio)
850 job->height = height;
851 hb_fix_aspect( job, HB_KEEP_HEIGHT );
853 else if( !width && !height && !pixelratio && !loosePixelratio )
855 hb_fix_aspect( job, HB_KEEP_WIDTH );
857 else if (!width && loosePixelratio)
859 /* Default to full width when one isn't specified for loose anamorphic */
860 job->width = title->width - job->crop[2] - job->crop[3];
861 /* The height will be thrown away in hb.c but calculate it anyway */
862 hb_fix_aspect( job, HB_KEEP_WIDTH );
865 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) ) )
867 job->vquality = vquality;
872 job->vquality = -1.0;
873 job->vbitrate = vbitrate;
877 job->vcodec = vcodec;
881 job->h264_level = 13;
885 job->h264_level = 30;
889 job->vrate = 27000000;
890 job->vrate_base = vrate;
893 /* Parse audio tracks */
894 if( hb_list_count(audios) == 0 )
896 /* Create a new audio track with default settings */
897 audio = calloc(1, sizeof(*audio));
898 hb_audio_config_init(audio);
899 /* Add it to our audios */
900 hb_list_add(audios, audio);
903 num_audio_tracks = hb_list_count(audios);
904 for (i = 0; i < num_audio_tracks; i++)
906 audio = hb_list_item(audios, 0);
907 if( (audio == NULL) || (audio->in.track == -1) ||
908 (audio->out.track == -1) || (audio->out.codec == 0) )
914 hb_audio_add( job, audio );
916 hb_list_rem(audios, audio);
926 char * token = strtok(acodecs, ",");
929 while ( token != NULL )
931 if ((acodec = get_acodec_for_string(token)) == -1)
933 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
934 acodec = default_acodec;
936 if( i < num_audio_tracks )
938 audio = hb_list_audio_config_item(job->list_audio, i);
939 audio->out.codec = acodec;
941 if( i >= num_audio_tracks )
943 int last_track = hb_list_count(job->list_audio);
944 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
946 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, last_track - 1 );
947 audio = calloc(1, sizeof(*audio));
948 hb_audio_config_init(audio);
949 audio->in.track = last_audio->in.track;
950 audio->out.track = num_audio_tracks++;
951 audio->out.codec = acodec;
952 hb_audio_add(job, audio);
955 token = strtok(NULL, ",");
959 if( i < num_audio_tracks )
961 /* We have fewer inputs than audio tracks, use the default codec for
962 * this container for the remaining tracks. Unless we only have one input
963 * then use that codec instead.
966 acodec = default_acodec;
967 for ( ; i < num_audio_tracks; i++)
969 audio = hb_list_audio_config_item(job->list_audio, i);
970 audio->out.codec = acodec;
979 char * token = strtok(arates, ",");
982 while ( token != NULL )
985 audio = hb_list_audio_config_item(job->list_audio, i);
986 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
988 /* Would probably be better to have this handled in libhb. */
989 audio->out.samplerate = audio->in.samplerate;
993 if (!is_sample_rate_valid(arate))
995 fprintf(stderr, "Invalid sample rate %d, using default\n", arate);
996 arate = default_arate;
1000 for( j = 0; j < hb_audio_rates_count; j++ )
1002 if( !strcmp( token, hb_audio_rates[j].string ) )
1004 arate = hb_audio_rates[j].rate;
1009 audio->out.samplerate = arate;
1011 if( (++i) >= num_audio_tracks )
1012 break; /* We have more inputs than audio tracks, oops */
1013 token = strtok(NULL, ",");
1016 if (i < num_audio_tracks)
1018 /* We have fewer inputs than audio tracks, use default sample rate.
1019 * Unless we only have one input, then use that for all tracks.
1022 arate = default_arate;
1023 for ( ; i < num_audio_tracks; i++)
1025 audio = hb_list_audio_config_item(job->list_audio, i);
1026 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1028 audio->out.samplerate = audio->in.samplerate;
1031 audio->out.samplerate = arate;
1040 char * token = strtok(abitrates, ",");
1043 while ( token != NULL )
1045 abitrate = atoi(token);
1046 audio = hb_list_audio_config_item(job->list_audio, i);
1047 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1049 audio->out.bitrate = audio->in.bitrate; /* See note above about arate. */
1053 audio->out.bitrate = abitrate;
1055 if( (++i) >= num_audio_tracks )
1056 break; /* We have more inputs than audio tracks, oops */
1057 token = strtok(NULL, ",");
1060 if (i < num_audio_tracks)
1062 /* We have fewer inputs than audio tracks, use the default bitrate
1063 * for the remaining tracks. Unless we only have one input, then use
1064 * that for all tracks.
1067 abitrate = default_abitrate;
1068 for (; i < num_audio_tracks; i++)
1070 audio = hb_list_audio_config_item(job->list_audio, i);
1071 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1073 audio->out.bitrate = audio->in.bitrate;
1076 audio->out.bitrate = abitrate;
1083 if ( dynamic_range_compression )
1085 char * token = strtok(dynamic_range_compression, ",");
1087 token = dynamic_range_compression;
1088 while ( token != NULL )
1090 d_r_c = atof(token);
1091 audio = hb_list_audio_config_item(job->list_audio, i);
1092 audio->out.dynamic_range_compression = d_r_c;
1093 if( (++i) >= num_audio_tracks )
1094 break; /* We have more inputs than audio tracks, oops */
1095 token = strtok(NULL, ",");
1098 if (i < num_audio_tracks)
1100 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1101 * tracks. Unless we only have one input, then use the same DRC for all
1106 for (; i < num_audio_tracks; i++)
1108 audio = hb_list_audio_config_item(job->list_audio, i);
1109 audio->out.dynamic_range_compression = d_r_c;
1118 char * token = strtok(mixdowns, ",");
1121 while ( token != NULL )
1123 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1124 audio = hb_list_audio_config_item(job->list_audio, i);
1125 audio->out.mixdown = mixdown;
1126 if( (++i) >= num_audio_tracks )
1127 break; /* We have more inputs than audio tracks, oops */
1128 token = strtok(NULL, ",");
1131 if (i < num_audio_tracks)
1133 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1134 * we only have one input, then use that.
1137 mixdown = HB_AMIXDOWN_DOLBYPLII;
1138 for (; i < num_audio_tracks; i++)
1140 audio = hb_list_audio_config_item(job->list_audio, i);
1141 audio->out.mixdown = mixdown;
1148 job->vbitrate = hb_calc_bitrate( job, size );
1149 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1155 job->subtitle = sub - 1;
1158 if( native_language )
1160 job->native_language = strdup( native_language );
1168 if ( largeFileSize )
1170 job->largeFileSize = 1;
1174 job->mp4_optimize = 1;
1181 job->file = strdup( output );
1188 if( x264opts != NULL && *x264opts != '\0' )
1190 job->x264opts = x264opts;
1192 else /*avoids a bus error crash when options aren't specified*/
1194 job->x264opts = NULL;
1197 job->maxWidth = maxWidth;
1199 job->maxHeight = maxHeight;
1201 if( subtitle_force )
1203 job->subtitle_force = subtitle_force;
1211 * When subtitle scan is enabled do a fast pre-scan job
1212 * which will determine which subtitles to enable, if any.
1216 x264opts_tmp = job->x264opts;
1218 job->x264opts = NULL;
1220 job->indepth_scan = subtitle_scan;
1221 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1222 "subtitles if found for foreign language segments\n");
1223 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1224 *(job->select_subtitle) = NULL;
1227 * Add the pre-scan job
1231 job->x264opts = x264opts_tmp;
1237 * If subtitle_scan is enabled then only turn it on
1238 * for the first pass and then off again for the
1241 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1243 job->select_subtitle = NULL;
1247 job->indepth_scan = 0;
1251 x264opts2 = strdup(x264opts);
1255 * If turbo options have been selected then append them
1256 * to the x264opts now (size includes one ':' and the '\0')
1258 if( turbo_opts_enabled )
1260 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1263 tmp_x264opts = malloc(size * sizeof(char));
1266 snprintf( tmp_x264opts, size, "%s:%s",
1267 x264opts, turbo_opts );
1271 * No x264opts to modify, but apply the turbo options
1272 * anyway as they may be modifying defaults
1274 snprintf( tmp_x264opts, size, "%s",
1277 x264opts = tmp_x264opts;
1279 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1282 job->x264opts = x264opts;
1286 job->select_subtitle = subtitle_tmp;
1290 * On the second pass we turn off subtitle scan so that we
1291 * can actually encode using any subtitles that were auto
1292 * selected in the first pass (using the whacky select-subtitle
1293 * attribute of the job).
1295 job->indepth_scan = 0;
1297 job->x264opts = x264opts2;
1304 * Turn on subtitle scan if requested, note that this option
1305 * precludes encoding of any actual subtitles.
1308 job->indepth_scan = 0;
1316 #define p s.param.working
1317 case HB_STATE_WORKING:
1318 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1319 p.job_cur, p.job_count, 100.0 * p.progress );
1320 if( p.seconds > -1 )
1322 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1323 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1324 p.hours, p.minutes, p.seconds );
1330 #define p s.param.muxing
1331 case HB_STATE_MUXING:
1333 if (show_mux_warning)
1335 fprintf( stdout, "\rMuxing: this may take awhile..." );
1337 show_mux_warning = 0;
1343 #define p s.param.workdone
1344 case HB_STATE_WORKDONE:
1345 /* Print error if any, then exit */
1349 fprintf( stderr, "\nRip done!\n" );
1351 case HB_ERROR_CANCELED:
1352 fprintf( stderr, "\nRip canceled.\n" );
1355 fprintf( stderr, "\nRip failed (error %x).\n",
1365 /****************************************************************************
1367 ****************************************************************************/
1368 static volatile int64_t i_die_date = 0;
1369 void SigHandler( int i_signal )
1374 i_die_date = hb_get_date();
1375 fprintf( stderr, "Signal %d received, terminating - do it "
1376 "again in case it gets stuck\n", i_signal );
1378 else if( i_die_date + 500 < hb_get_date() )
1380 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1385 /****************************************************************************
1387 ****************************************************************************/
1388 static void ShowHelp()
1393 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1395 "### General Handbrake Options------------------------------------------------\n\n"
1396 " -h, --help Print help\n"
1397 " -u, --update Check for updates and exit\n"
1398 " -v, --verbose Be verbose\n"
1399 " -C, --cpu Set CPU count (default: autodetected)\n"
1400 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1401 " if the preset name has spaces, surround it with\n"
1402 " double quotation marks\n"
1403 " -z, --preset-list See a list of available built-in presets\n"
1406 "### Source Options-----------------------------------------------------------\n\n"
1407 " -i, --input <string> Set input device\n"
1408 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1410 " -L, --longest Select the longest title\n"
1411 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1412 " 1 to 3, or \"3\" for chapter 3 only,\n"
1413 " default: all chapters)\n"
1416 "### Destination Options------------------------------------------------------\n\n"
1417 " -o, --output <string> Set output file name\n"
1418 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1419 " autodetected from file name)\n"
1420 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1421 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1422 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1423 " -I, --ipod-atom Mark mp4 files so iPods will accept them\n"
1426 "### Picture Settings---------------------------------------------------------\n\n"
1427 " -w, --width <number> Set picture width\n"
1428 " -l, --height <number> Set picture height\n"
1429 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1430 " -Y, --maxHeight <#> Set maximum height\n"
1431 " -X, --maxWidth <#> Set maximum width\n"
1432 " -s, --subtitle <number> Select subtitle (default: none)\n"
1433 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1434 " the one that's only used 10 percent of the time\n"
1435 " or less. This should locate subtitles for short\n"
1436 " foreign language segments. Best used in conjunction\n"
1437 " with --subtitle-forced.\n"
1438 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1439 " the subtitle has the forced flag set. May be used in\n"
1440 " conjunction with --subtitle-scan to auto-select\n"
1441 " a stream if it contains forced subtitles.\n"
1442 " -N, --native-language Select subtitles with this language if it does not\n"
1443 " <string> match the Audio language. Provide the language's\n"
1444 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1445 " -m, --markers Add chapter markers (mp4 output format only)\n"
1448 "### Video Options------------------------------------------------------------\n\n"
1449 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1450 " x264,theora default: ffmpeg)\n"
1451 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1452 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1453 " -S, --size <MB> Set target size\n"
1454 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1455 " -r, --rate Set video framerate (" );
1456 for( i = 0; i < hb_video_rates_count; i++ )
1458 fprintf( stderr, hb_video_rates[i].string );
1459 if( i != hb_video_rates_count - 1 )
1460 fprintf( stderr, "/" );
1462 fprintf( stderr, ")\n"
1464 " -2, --two-pass Use two-pass mode\n"
1465 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1466 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1468 " <fast/slow/slower>\n"
1469 " -7, --deblock Deblock video with pp7 filter\n"
1470 " <QP:M> (default 0:2)\n"
1471 " -8, --denoise Denoise video with hqdn3d filter\n"
1472 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1474 " <weak/medium/strong>\n"
1475 " -9, --detelecine Detelecine video with pullup filter\n"
1476 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1477 " -g, --grayscale Grayscale encoding\n"
1478 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1479 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1480 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
1481 " the dimensions to divide cleanly by (default 16)\n"
1482 " and the pixel ratio to use (default autodetected)\n)"
1488 "### Audio Options-----------------------------------------------------------\n\n"
1489 " -a, --audio <string> Select audio channel(s), separated by commas\n"
1490 " More than one output track can be used for one\n"
1492 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1493 " tracks, default: first one)\n"
1494 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1495 " ac3 meaning passthrough\n"
1496 " Seperated by commas for more than one audio track.\n"
1497 " (default: guessed)\n"
1498 " -B, --ab <kb/s> Set audio bitrate(s) (default: 128)\n"
1499 " Seperated by commas for more than one audio track.\n"
1500 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1501 " Seperated by commas for more than one audio track.\n"
1502 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1503 " -R, --arate Set audio samplerate(s) (" );
1504 for( i = 0; i < hb_audio_rates_count; i++ )
1506 fprintf( stderr, hb_audio_rates[i].string );
1507 if( i != hb_audio_rates_count - 1 )
1508 fprintf( stderr, "/" );
1510 fprintf( stderr, " kHz)\n"
1511 " Seperated by commas for more than one audio track.\n"
1512 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1513 " making soft sounds louder. Range is 1.0 to 4.0\n"
1514 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1515 " Seperated by commas for more than one audio track.\n"
1521 "### Advanced Options---------------------------------------------------------\n\n"
1522 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1523 " same style as mencoder:\n"
1524 " option1=value1:option2=value2\n"
1525 " -T, --turbo When using 2-pass use the turbo options\n"
1526 " on the first pass to improve speed\n"
1527 " (only works with x264, affects PSNR by about 0.05dB,\n"
1528 " and increases first pass speed two to four times)\n"
1529 " -V, --vfr Perform variable framerate detelecine on NTSC video\n"
1533 /****************************************************************************
1535 ****************************************************************************/
1536 static void ShowPresets()
1538 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");
1540 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");
1542 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");
1544 printf("\n+ Blind: -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1546 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");
1548 printf("\n+ Classic: -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1550 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");
1552 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");
1554 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");
1556 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");
1558 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");
1560 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");
1562 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");
1564 printf("\n+ PS3: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1566 printf("\n+ PSP: -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1568 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");
1570 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");
1572 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");
1575 /****************************************************************************
1577 ****************************************************************************/
1578 static int ParseOptions( int argc, char ** argv )
1582 static struct option long_options[] =
1584 { "help", no_argument, NULL, 'h' },
1585 { "update", no_argument, NULL, 'u' },
1586 { "verbose", no_argument, NULL, 'v' },
1587 { "cpu", required_argument, NULL, 'C' },
1589 { "format", required_argument, NULL, 'f' },
1590 { "input", required_argument, NULL, 'i' },
1591 { "output", required_argument, NULL, 'o' },
1592 { "large-file", no_argument, NULL, '4' },
1593 { "optimize", no_argument, NULL, 'O' },
1594 { "ipod-atom", no_argument, NULL, 'I' },
1596 { "title", required_argument, NULL, 't' },
1597 { "longest", no_argument, NULL, 'L' },
1598 { "chapters", required_argument, NULL, 'c' },
1599 { "markers", optional_argument, NULL, 'm' },
1600 { "audio", required_argument, NULL, 'a' },
1601 { "mixdown", required_argument, NULL, '6' },
1602 { "drc", required_argument, NULL, 'D' },
1603 { "subtitle", required_argument, NULL, 's' },
1604 { "subtitle-scan", no_argument, NULL, 'U' },
1605 { "subtitle-forced", no_argument, NULL, 'F' },
1606 { "native-language", required_argument, NULL,'N' },
1608 { "encoder", required_argument, NULL, 'e' },
1609 { "aencoder", required_argument, NULL, 'E' },
1610 { "two-pass", no_argument, NULL, '2' },
1611 { "deinterlace", optional_argument, NULL, 'd' },
1612 { "deblock", optional_argument, NULL, '7' },
1613 { "denoise", optional_argument, NULL, '8' },
1614 { "detelecine", optional_argument, NULL, '9' },
1615 { "grayscale", no_argument, NULL, 'g' },
1616 { "pixelratio", no_argument, NULL, 'p' },
1617 { "loosePixelratio", optional_argument, NULL, 'P' },
1618 { "width", required_argument, NULL, 'w' },
1619 { "height", required_argument, NULL, 'l' },
1620 { "crop", required_argument, NULL, 'n' },
1622 { "vb", required_argument, NULL, 'b' },
1623 { "quality", required_argument, NULL, 'q' },
1624 { "size", required_argument, NULL, 'S' },
1625 { "ab", required_argument, NULL, 'B' },
1626 { "rate", required_argument, NULL, 'r' },
1627 { "arate", required_argument, NULL, 'R' },
1628 { "cqp", no_argument, NULL, 'Q' },
1629 { "x264opts", required_argument, NULL, 'x' },
1630 { "turbo", no_argument, NULL, 'T' },
1631 { "maxHeight", required_argument, NULL, 'Y' },
1632 { "maxWidth", required_argument, NULL, 'X' },
1633 { "preset", required_argument, NULL, 'Z' },
1634 { "preset-list", no_argument, NULL, 'z' },
1635 { "vfr", no_argument, NULL, 'V' },
1640 int option_index = 0;
1643 c = getopt_long( argc, argv,
1644 "hvuC:f:4i:Io:t:Lc:m::a:6:s:UFN:e:E:2dD:789gpOP::w:l:n:b:q:S:B:r:R:Qx:TY:X:VZ:z",
1645 long_options, &option_index );
1660 debug = HB_DEBUG_ALL;
1663 cpu = atoi( optarg );
1668 preset_name = strdup(optarg);
1675 format = strdup( optarg );
1678 input = strdup( optarg );
1680 char *devName = bsd_name_for_path( input );
1681 if( devName == NULL )
1685 if( device_is_dvd( devName ) )
1687 char *newInput = malloc( strlen("/dev/") + strlen( devName ) + 1);
1688 sprintf( newInput, "/dev/%s", devName );
1695 output = strdup( optarg );
1708 titleindex = atoi( optarg );
1716 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1718 chapter_start = start;
1721 else if( sscanf( optarg, "%d", &start ) == 1 )
1723 chapter_start = start;
1724 chapter_end = chapter_start;
1728 fprintf( stderr, "chapters: invalid syntax (%s)\n",
1735 if( optarg != NULL )
1737 marker_file = strdup( optarg );
1739 chapter_markers = 1;
1743 char * token = strtok(optarg, ",");
1746 int track_start, track_end;
1747 while( token != NULL )
1749 audio = calloc(1, sizeof(*audio));
1750 hb_audio_config_init(audio);
1751 if (strlen(token) >= 3)
1753 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1756 for (i = track_start - 1; i < track_end; i++)
1758 if (i != track_start - 1)
1760 audio = calloc(1, sizeof(*audio));
1761 hb_audio_config_init(audio);
1763 audio->in.track = i;
1764 audio->out.track = num_audio_tracks++;
1765 hb_list_add(audios, audio);
1768 else if( !strcasecmp(token, "none" ) )
1770 audio->in.track = audio->out.track = -1;
1771 audio->out.codec = 0;
1772 hb_list_add(audios, audio);
1777 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1784 audio->in.track = atoi(token) - 1;
1785 audio->out.track = num_audio_tracks++;
1786 hb_list_add(audios, audio);
1788 token = strtok(NULL, ",");
1793 if( optarg != NULL )
1795 mixdowns = strdup( optarg );
1799 if( optarg != NULL )
1801 dynamic_range_compression = strdup( optarg );
1805 sub = atoi( optarg );
1814 native_language = strdup( optarg );
1820 if( optarg != NULL )
1822 if (!( strcmp( optarg, "fast" ) ))
1824 deinterlace_opt = "-1";
1826 else if (!( strcmp( optarg, "slow" ) ))
1828 deinterlace_opt = "2";
1830 else if (!( strcmp( optarg, "slower" ) ))
1832 deinterlace_opt = "0";
1836 deinterlace_opt = strdup( optarg );
1842 if( optarg != NULL )
1844 deblock_opt = strdup( optarg );
1849 if( optarg != NULL )
1851 if (!( strcmp( optarg, "weak" ) ))
1853 denoise_opt = "2:1:2:3";
1855 else if (!( strcmp( optarg, "medium" ) ))
1857 denoise_opt = "3:2:2:3";
1859 else if (!( strcmp( optarg, "strong" ) ))
1861 denoise_opt = "7:7:5:5";
1865 denoise_opt = strdup( optarg );
1871 if( optarg != NULL )
1873 detelecine_opt = strdup( optarg );
1884 loosePixelratio = 1;
1885 if( optarg != NULL )
1887 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
1891 if( !strcasecmp( optarg, "ffmpeg" ) )
1893 vcodec = HB_VCODEC_FFMPEG;
1895 else if( !strcasecmp( optarg, "xvid" ) )
1897 vcodec = HB_VCODEC_XVID;
1899 else if( !strcasecmp( optarg, "x264" ) )
1901 vcodec = HB_VCODEC_X264;
1903 else if( !strcasecmp( optarg, "x264b13" ) )
1905 vcodec = HB_VCODEC_X264;
1908 else if( !strcasecmp( optarg, "x264b30" ) )
1910 vcodec = HB_VCODEC_X264;
1913 else if( !strcasecmp( optarg, "theora" ) )
1915 vcodec = HB_VCODEC_THEORA;
1919 fprintf( stderr, "invalid codec (%s)\n", optarg );
1924 if( optarg != NULL )
1926 acodecs = strdup( optarg );
1930 width = atoi( optarg );
1933 height = atoi( optarg );
1938 char * tmp = optarg;
1939 for( i = 0; i < 4; i++ )
1943 crop[i] = strtol( tmp, &tmp, 0 );
1952 for( i = 0; i < hb_video_rates_count; i++ )
1954 if( !strcmp( optarg, hb_video_rates[i].string ) )
1956 vrate = hb_video_rates[i].rate;
1962 fprintf( stderr, "invalid framerate %s\n", optarg );
1967 if( optarg != NULL )
1969 arates = strdup( optarg );
1973 vbitrate = atoi( optarg );
1976 vquality = atof( optarg );
1979 size = atoi( optarg );
1982 if( optarg != NULL )
1984 abitrates = strdup( optarg );
1991 x264opts = strdup( optarg );
1994 turbo_opts_enabled = 1;
1997 maxHeight = atoi( optarg );
2000 maxWidth = atoi (optarg );
2007 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2015 static int CheckOptions( int argc, char ** argv )
2022 if( input == NULL || *input == '\0' )
2024 fprintf( stderr, "Missing input device. Run %s --help for "
2025 "syntax.\n", argv[0] );
2030 if( titleindex > 0 )
2032 if( output == NULL || *output == '\0' )
2034 fprintf( stderr, "Missing output file name. Run %s --help "
2035 "for syntax.\n", argv[0] );
2041 char * p = strrchr( output, '.' );
2044 if( p && !strcasecmp( p, ".avi" ) )
2047 default_acodec = HB_ACODEC_LAME;
2049 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2050 !strcasecmp( p, ".m4v" ) ) )
2056 default_acodec = HB_ACODEC_FAAC;
2058 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2059 !strcasecmp( p, ".ogg" ) ) )
2062 default_acodec = HB_ACODEC_VORBIS;
2064 else if( p && !strcasecmp(p, ".mkv" ) )
2067 default_acodec = HB_ACODEC_AC3;
2071 fprintf( stderr, "Output format couldn't be guessed "
2072 "from file name, using default.\n" );
2076 else if( !strcasecmp( format, "avi" ) )
2079 default_acodec = HB_ACODEC_LAME;
2081 else if( !strcasecmp( format, "mp4" ) ||
2082 !strcasecmp( format, "m4v" ) )
2088 default_acodec = HB_ACODEC_FAAC;
2090 else if( !strcasecmp( format, "ogm" ) ||
2091 !strcasecmp( format, "ogg" ) )
2094 default_acodec = HB_ACODEC_VORBIS;
2096 else if( !strcasecmp( format, "mkv" ) )
2099 default_acodec = HB_ACODEC_AC3;
2103 fprintf( stderr, "Invalid output format (%s). Possible "
2104 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2112 static int get_acodec_for_string( char *codec )
2114 if( !strcasecmp( codec, "ac3" ) )
2116 return HB_ACODEC_AC3;
2118 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2120 return HB_ACODEC_DCA;
2122 else if( !strcasecmp( codec, "lame" ) )
2124 return HB_ACODEC_LAME;
2126 else if( !strcasecmp( codec, "faac" ) )
2128 return HB_ACODEC_FAAC;
2130 else if( !strcasecmp( codec, "vorbis") )
2132 return HB_ACODEC_VORBIS;
2140 static int is_sample_rate_valid(int rate)
2143 for( i = 0; i < hb_audio_rates_count; i++ )
2145 if (rate == hb_audio_rates[i].rate)
2152 /****************************************************************************
2155 * Returns the BSD device name for the block device that contains the
2156 * passed-in path. Returns NULL on failure.
2157 ****************************************************************************/
2158 static char* bsd_name_for_path(char *path)
2162 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2168 // Get the volume reference number.
2169 FSCatalogInfo catalogInfo;
2170 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2176 FSVolumeRefNum volRefNum = catalogInfo.volume;
2178 // Now let's get the device name
2179 GetVolParmsInfoBuffer volumeParms;
2180 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2186 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the
2187 // vMDeviceID field. It is actually a char * value. This is mentioned in the
2188 // header CoreServices/CarbonCore/Files.h.
2189 return volumeParms.vMDeviceID;
2192 /****************************************************************************
2195 * Returns whether or not the passed in BSD device represents a DVD, or other
2197 ****************************************************************************/
2198 static int device_is_dvd(char *device)
2200 io_service_t service = get_iokit_service(device);
2201 if( service == IO_OBJECT_NULL )
2205 int result = is_dvd_service(service);
2206 IOObjectRelease(service);
2210 /****************************************************************************
2213 * Returns the IOKit service object for the passed in BSD device name.
2214 ****************************************************************************/
2215 static io_service_t get_iokit_service( char *device )
2217 CFMutableDictionaryRef matchingDict;
2218 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2219 if( matchingDict == NULL )
2221 return IO_OBJECT_NULL;
2223 // Fetch the object with the matching BSD node name. There should only be
2224 // one match, so IOServiceGetMatchingService is used instead of
2225 // IOServiceGetMatchingServices to simplify the code.
2226 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2229 /****************************************************************************
2232 * Returns whether or not the service passed in is a DVD.
2234 * Searches for an IOMedia object that represents the entire (whole) media that
2235 * the volume is on. If the volume is on partitioned media, the whole media
2236 * object will be a parent of the volume's media object. If the media is not
2237 * partitioned, the volume's media object will be the whole media object.
2238 ****************************************************************************/
2239 static int is_dvd_service( io_service_t service )
2241 kern_return_t kernResult;
2244 // Create an iterator across all parents of the service object passed in.
2245 kernResult = IORegistryEntryCreateIterator( service,
2247 kIORegistryIterateRecursively | kIORegistryIterateParents,
2249 if( kernResult != KERN_SUCCESS )
2253 if( iter == IO_OBJECT_NULL )
2258 // A reference on the initial service object is released in the do-while
2259 // loop below, so add a reference to balance.
2260 IOObjectRetain( service );
2265 if( is_whole_media_service( service ) &&
2266 IOObjectConformsTo( service, kIODVDMediaClass) )
2270 IOObjectRelease( service );
2271 } while( !result && (service = IOIteratorNext( iter )) );
2272 IOObjectRelease( iter );
2277 /****************************************************************************
2278 * is_whole_media_service
2280 * Returns whether or not the service passed in is an IOMedia service and
2281 * represents the "whole" media instead of just a partition.
2283 * The whole media object is indicated in the IORegistry by the presence of a
2284 * property with the key "Whole" and value "Yes".
2285 ****************************************************************************/
2286 static is_whole_media_service( io_service_t service )
2290 if( IOObjectConformsTo( service, kIOMediaClass ) )
2292 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2293 CFSTR( kIOMediaWholeKey ),
2294 kCFAllocatorDefault,
2300 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2301 CFRelease( wholeMedia );
2306 #endif // __APPLE_CC__