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 * arates = NULL;
55 static char * abitrates = NULL;
56 static char * acodecs = NULL;
57 static int default_acodec = HB_ACODEC_FAAC;
58 static int default_arate = 44100;
59 static int default_abitrate = 128;
62 static int height = 0;
63 static int crop[4] = { -1,-1,-1,-1 };
66 static float vquality = -1.0;
67 static int vbitrate = 0;
70 static int pixelratio = 0;
71 static int loosePixelratio = 0;
72 static int modulus = 0;
73 static int par_height = 0;
74 static int par_width = 0;
75 static int chapter_start = 0;
76 static int chapter_end = 0;
77 static int chapter_markers = 0;
78 static char * marker_file = NULL;
80 static char *x264opts = NULL;
81 static char *x264opts2 = NULL;
82 static int maxHeight = 0;
83 static int maxWidth = 0;
84 static int turbo_opts_enabled = 0;
85 static char * turbo_opts = "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0:weightb=0";
86 static int largeFileSize = 0;
87 static int preset = 0;
88 static char * preset_name = 0;
90 static int mp4_optimize = 0;
91 static int ipod_atom = 0;
93 /* Exit cleanly on Ctrl-C */
94 static volatile int die = 0;
95 static void SigHandler( int );
98 static void ShowCommands();
99 static void ShowHelp();
100 static void ShowPresets();
102 static int ParseOptions( int argc, char ** argv );
103 static int CheckOptions( int argc, char ** argv );
104 static int HandleEvents( hb_handle_t * h );
106 static int get_acodec_for_string( char *codec );
107 static int is_sample_rate_valid(int rate);
110 static char* bsd_name_for_path(char *path);
111 static int device_is_dvd(char *device);
112 static io_service_t get_iokit_service( char *device );
113 static int is_dvd_service( io_service_t service );
114 static is_whole_media_service( io_service_t service );
117 /* Only print the "Muxing..." message once */
118 static int show_mux_warning = 1;
120 /****************************************************************************
123 * When using the CLI just display using hb_log as we always did in the past
124 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
125 ****************************************************************************/
126 static void hb_cli_error_handler ( const char *errmsg )
128 fprintf( stderr, "ERROR: %s", errmsg );
131 int main( int argc, char ** argv )
137 audios = hb_list_init();
139 /* Parse command line */
140 if( ParseOptions( argc, argv ) ||
141 CheckOptions( argc, argv ) )
146 /* Register our error handler */
147 hb_register_error_handler(&hb_cli_error_handler);
150 h = hb_init( debug, update );
153 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.fr/\n",
154 hb_get_version( h ), hb_get_build( h ) );
156 /* Check for update */
159 if( ( build = hb_check_update( h, &version ) ) > -1 )
161 fprintf( stderr, "You are using an old version of "
162 "HandBrake.\nLatest is %s (build %d).\n", version,
167 fprintf( stderr, "Your version of HandBrake is up to "
175 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
176 hb_get_cpu_count( h ) > 1 ? "s" : "" );
179 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
180 cpu > 1 ? "s" : "" );
181 hb_set_cpu_count( h, cpu );
184 /* Exit ASAP on Ctrl-C */
185 signal( SIGINT, SigHandler );
187 /* Feed libhb with a DVD to scan */
188 fprintf( stderr, "Opening %s...\n", input );
192 * We need to scan for all the titles in order to find the longest
196 hb_scan( h, input, titleindex );
201 #if !defined(SYS_BEOS)
211 FD_SET( STDIN_FILENO, &fds );
212 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
219 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
221 if( buf[size] == '\n' )
228 if( size >= 256 || buf[size] == '\n' )
233 fprintf( stdout, "\nEncoding Quit by user command\n" );
237 fprintf( stdout, "\nEncoding Paused by user command, 'r' to resume\n" );
259 if( input ) free( input );
260 if( output ) free( output );
261 if( format ) free( format );
264 while( ( audio = hb_list_item( audios, 0 ) ) )
266 hb_list_rem( audios, audio );
269 hb_list_close( &audios );
271 if( mixdowns ) free( mixdowns );
272 if( dynamic_range_compression ) free( dynamic_range_compression );
273 if( arates ) free( arates );
274 if( abitrates ) free( abitrates );
275 if( acodecs ) free( acodecs );
276 if (native_language ) free (native_language );
277 if( x264opts ) free (x264opts );
278 if( x264opts2 ) free (x264opts2 );
279 if (preset_name) free (preset_name);
281 fprintf( stderr, "HandBrake has exited.\n" );
286 static void ShowCommands()
288 fprintf( stdout, "\nCommands:\n" );
289 fprintf( stdout, " [h]elp Show this message\n" );
290 fprintf( stdout, " [q]uit Exit HandBrakeCLI\n" );
291 fprintf( stdout, " [p]ause Pause encoding\n" );
292 fprintf( stdout, " [r]esume Resume encoding\n" );
295 static void PrintTitleInfo( hb_title_t * title )
297 hb_chapter_t * chapter;
298 hb_audio_config_t * audio;
299 hb_subtitle_t * subtitle;
302 fprintf( stderr, "+ title %d:\n", title->index );
303 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
304 title->vts, title->ttn, title->cell_start, title->cell_end,
305 title->block_count );
306 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
307 title->hours, title->minutes, title->seconds );
308 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
309 title->width, title->height,
310 (float) title->aspect / HB_ASPECT_BASE,
311 (float) title->rate / title->rate_base );
312 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
313 title->crop[1], title->crop[2], title->crop[3] );
314 fprintf( stderr, " + chapters:\n" );
315 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
317 chapter = hb_list_item( title->list_chapter, i );
318 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
319 "%02d:%02d:%02d\n", chapter->index,
320 chapter->cell_start, chapter->cell_end,
321 chapter->block_count, chapter->hours, chapter->minutes,
324 fprintf( stderr, " + audio tracks:\n" );
325 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
327 audio = hb_list_audio_config_item( title->list_audio, i );
328 if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
330 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
331 audio->lang.description, audio->in.samplerate, audio->in.bitrate );
335 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang.description );
338 fprintf( stderr, " + subtitle tracks:\n" );
339 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
341 subtitle = hb_list_item( title->list_subtitle, i );
342 fprintf( stderr, " + %d, %s (iso639-2: %s)\n", i + 1, subtitle->lang,
346 if(title->detected_interlacing)
348 /* Interlacing was found in half or more of the preview frames */
349 fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
354 static int HandleEvents( hb_handle_t * h )
357 hb_get_state( h, &s );
364 #define p s.param.scanning
365 case HB_STATE_SCANNING:
366 /* Show what title is currently being scanned */
367 fprintf( stderr, "Scanning title %d", p.title_cur );
369 fprintf( stderr, " of %d", p.title_count );
370 fprintf( stderr, "...\n" );
374 case HB_STATE_SCANDONE:
381 /* Audio argument string parsing variables */
385 int mixdown = HB_AMIXDOWN_DOLBYPLII;
387 /* Audio argument string parsing variables */
389 list = hb_get_titles( h );
391 if( !hb_list_count( list ) )
393 /* No valid title, stop right there */
394 fprintf( stderr, "No title found.\n" );
401 int longest_title_idx=0;
402 int longest_title_pos=-1;
403 int longest_title_time=0;
406 fprintf( stderr, "Searching for longest title...\n" );
408 for( i = 0; i < hb_list_count( list ); i++ )
410 title = hb_list_item( list, i );
411 title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
412 fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
413 i, title->index, title_time );
414 if( longest_title_time < title_time )
416 longest_title_time = title_time;
417 longest_title_pos = i;
418 longest_title_idx = title->index;
421 if( longest_title_pos == -1 )
423 fprintf( stderr, "No longest title found.\n" );
427 titleindex = longest_title_idx;
428 fprintf( stderr, "Found longest title, setting title to %d\n",
431 title = hb_list_item( list, longest_title_pos);
433 title = hb_list_item( list, 0 );
438 /* Scan-only mode, print infos and exit */
440 for( i = 0; i < hb_list_count( list ); i++ )
442 title = hb_list_item( list, i );
443 PrintTitleInfo( title );
449 /* Set job settings */
452 PrintTitleInfo( title );
454 if( chapter_start && chapter_end )
456 job->chapter_start = MAX( job->chapter_start,
458 job->chapter_end = MIN( job->chapter_end,
460 job->chapter_end = MAX( job->chapter_start,
466 fprintf( stderr, "+ Using preset: %s", preset_name);
468 if (!strcmp(preset_name, "Animation"))
471 vcodec = HB_VCODEC_X264;
472 job->vbitrate = 1000;
473 default_abitrate = 160;
474 default_arate = 48000;
475 default_acodec = HB_ACODEC_FAAC;
476 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");
478 deinterlace_opt = "0";
479 job->chapter_markers = 1;
482 turbo_opts_enabled = 1;
485 if (!strcmp(preset_name, "AppleTV"))
489 acodecs = strdup("faac,ac3");
493 job->largeFileSize = 1;
494 vcodec = HB_VCODEC_X264;
495 job->vbitrate = 2500;
496 default_abitrate = 160;
497 default_arate = 48000;
498 default_acodec = HB_ACODEC_FAAC;
499 x264opts = strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=1:cabac=0");
500 job->chapter_markers = 1;
504 if (!strcmp(preset_name, "Bedlam"))
507 vcodec = HB_VCODEC_X264;
508 job->vbitrate = 1800;
509 default_acodec = HB_ACODEC_AC3;
510 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");
511 job->chapter_markers = 1;
514 turbo_opts_enabled = 1;
517 if (!strcmp(preset_name, "Blind"))
521 default_abitrate = 128;
522 default_arate = 48000;
523 default_acodec = HB_ACODEC_FAAC;
525 job->chapter_markers = 1;
528 if (!strcmp(preset_name, "Broke"))
531 vcodec = HB_VCODEC_X264;
533 default_abitrate = 128;
534 default_arate = 48000;
535 default_acodec = HB_ACODEC_FAAC;
537 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");
538 job->chapter_markers = 1;
540 turbo_opts_enabled = 1;
543 if (!strcmp(preset_name, "Classic"))
546 job->vbitrate = 1000;
547 default_abitrate = 160;
548 default_arate = 48000;
549 default_acodec = HB_ACODEC_FAAC;
552 if (!strcmp(preset_name, "Constant Quality Rate"))
555 vcodec = HB_VCODEC_X264;
556 job->vquality = 0.64709997177124023;
558 default_acodec = HB_ACODEC_AC3;
559 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");
560 job->chapter_markers = 1;
564 if (!strcmp(preset_name, "Deux Six Quatre"))
567 vcodec = HB_VCODEC_X264;
568 job->vbitrate = 1600;
569 default_acodec = HB_ACODEC_AC3;
570 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");
571 job->chapter_markers = 1;
574 turbo_opts_enabled = 1;
577 if (!strcmp(preset_name, "Film"))
580 vcodec = HB_VCODEC_X264;
581 job->vbitrate = 1800;
582 default_acodec = HB_ACODEC_AC3;
583 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");
584 job->chapter_markers = 1;
587 turbo_opts_enabled = 1;
590 if (!strcmp(preset_name, "iPhone / iPod Touch"))
594 vcodec = HB_VCODEC_X264;
596 default_abitrate = 128;
597 default_arate = 48000;
598 default_acodec = HB_ACODEC_FAAC;
600 x264opts = strdup("level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
601 job->chapter_markers = 1;
604 if (!strcmp(preset_name, "iPod High-Rez"))
608 vcodec = HB_VCODEC_X264;
609 job->vbitrate = 1500;
610 default_abitrate = 160;
611 default_arate = 48000;
612 default_acodec = HB_ACODEC_FAAC;
614 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");
615 job->chapter_markers = 1;
618 if (!strcmp(preset_name, "iPod Low-Rez"))
622 vcodec = HB_VCODEC_X264;
624 default_abitrate = 160;
625 default_arate = 48000;
626 default_acodec = HB_ACODEC_FAAC;
628 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");
629 job->chapter_markers = 1;
632 if (!strcmp(preset_name, "Normal"))
635 vcodec = HB_VCODEC_X264;
636 job->vbitrate = 1500;
637 default_abitrate = 160;
638 default_arate = 48000;
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_arate = 48000;
654 default_acodec = HB_ACODEC_FAAC;
655 x264opts = strdup("level=41:subme=5:me=umh");
659 if (!strcmp(preset_name, "PSP"))
662 job->vbitrate = 1024;
663 default_abitrate = 128;
664 default_arate = 48000;
665 default_acodec = HB_ACODEC_FAAC;
668 job->chapter_markers = 1;
671 if (!strcmp(preset_name, "QuickTime"))
674 vcodec = HB_VCODEC_X264;
675 job->vbitrate = 2000;
676 default_abitrate = 160;
677 default_arate = 48000;
678 default_acodec = HB_ACODEC_FAAC;
679 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");
680 job->chapter_markers = 1;
683 turbo_opts_enabled = 1;
686 if (!strcmp(preset_name, "Television"))
689 vcodec = HB_VCODEC_X264;
690 job->vbitrate = 1300;
691 default_abitrate = 160;
692 default_arate = 48000;
693 default_acodec = HB_ACODEC_FAAC;
694 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");
696 deinterlace_opt = "0";
698 denoise_opt = "2:1:2:3";
699 job->chapter_markers = 1;
701 turbo_opts_enabled = 1;
704 if (!strcmp(preset_name, "Xbox 360"))
707 vcodec = HB_VCODEC_X264;
708 job->vbitrate = 2000;
709 default_abitrate = 160;
710 default_arate = 48000;
711 default_acodec = HB_ACODEC_FAAC;
712 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");
717 if ( chapter_markers )
719 job->chapter_markers = chapter_markers;
721 if( marker_file != NULL )
723 hb_csv_file_t * file = hb_open_csv_file( marker_file );
724 hb_csv_cell_t * cell;
728 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
732 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
736 /* Parse the cells */
737 while( NULL != ( cell = hb_read_next_cell( file ) ) )
739 /* We have a chapter number */
740 if( cell->cell_col == 0 )
742 row = cell->cell_row;
743 chapter = atoi( cell->cell_text );
746 /* We have a chapter name */
747 if( cell->cell_col == 1 && row == cell->cell_row )
749 /* If we have a valid chapter, copy the string an terminate it */
750 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
752 hb_chapter_t * chapter_s;
754 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
755 strncpy(chapter_s->title, cell->cell_text, 1023);
756 chapter_s->title[1023] = '\0';
761 hb_dispose_cell( cell );
764 hb_close_csv_file( file );
771 int number_of_chapters = hb_list_count(job->title->list_chapter);
774 for(chapter = 0; chapter <= number_of_chapters - 1 ; chapter++)
776 hb_chapter_t * chapter_s;
777 chapter_s = hb_list_item( job->title->list_chapter, chapter);
778 snprintf( chapter_s->title, 1023, "Chapter %i", chapter + 1 );
779 chapter_s->title[1023] = '\0';
784 if( crop[0] >= 0 && crop[1] >= 0 &&
785 crop[2] >= 0 && crop[3] >= 0 )
787 memcpy( job->crop, crop, 4 * sizeof( int ) );
790 job->deinterlace = deinterlace;
791 job->grayscale = grayscale;
794 job->pixel_ratio = 2;
797 job->modulus = modulus;
799 if( par_width && par_height )
801 job->pixel_ratio = 3;
802 job->pixel_aspect_width = par_width;
803 job->pixel_aspect_height = par_height;
808 job->pixel_ratio = pixelratio;
817 /* Add selected filters */
818 job->filters = hb_list_init();
821 hb_filter_detelecine.settings = detelecine_opt;
822 hb_list_add( job->filters, &hb_filter_detelecine );
826 hb_filter_decomb.settings = decomb_opt;
827 hb_list_add( job->filters, &hb_filter_decomb );
831 hb_filter_deinterlace.settings = deinterlace_opt;
832 hb_list_add( job->filters, &hb_filter_deinterlace );
836 hb_filter_deblock.settings = deblock_opt;
837 hb_list_add( job->filters, &hb_filter_deblock );
841 hb_filter_denoise.settings = denoise_opt;
842 hb_list_add( job->filters, &hb_filter_denoise );
845 if( width && height )
848 job->height = height;
853 hb_fix_aspect( job, HB_KEEP_WIDTH );
855 else if( height && !loosePixelratio)
857 job->height = height;
858 hb_fix_aspect( job, HB_KEEP_HEIGHT );
860 else if( !width && !height && !pixelratio && !loosePixelratio )
862 hb_fix_aspect( job, HB_KEEP_WIDTH );
864 else if (!width && loosePixelratio)
866 /* Default to full width when one isn't specified for loose anamorphic */
867 job->width = title->width - job->crop[2] - job->crop[3];
868 /* The height will be thrown away in hb.c but calculate it anyway */
869 hb_fix_aspect( job, HB_KEEP_WIDTH );
872 if( vquality >= 0.0 && ( ( vquality <= 1.0 ) || ( vcodec == HB_VCODEC_X264 ) ) )
874 job->vquality = vquality;
879 job->vquality = -1.0;
880 job->vbitrate = vbitrate;
884 job->vcodec = vcodec;
888 job->h264_level = 13;
892 job->h264_level = 30;
896 job->vrate = 27000000;
897 job->vrate_base = vrate;
900 /* Parse audio tracks */
901 if( hb_list_count(audios) == 0 )
903 /* Create a new audio track with default settings */
904 audio = calloc(1, sizeof(*audio));
905 hb_audio_config_init(audio);
906 /* Add it to our audios */
907 hb_list_add(audios, audio);
910 num_audio_tracks = hb_list_count(audios);
911 for (i = 0; i < num_audio_tracks; i++)
913 audio = hb_list_item(audios, 0);
914 if( (audio == NULL) || (audio->in.track == -1) ||
915 (audio->out.track == -1) || (audio->out.codec == 0) )
921 hb_audio_add( job, audio );
923 hb_list_rem(audios, audio);
933 char * token = strtok(acodecs, ",");
936 while ( token != NULL )
938 if ((acodec = get_acodec_for_string(token)) == -1)
940 fprintf(stderr, "Invalid codec %s, using default for container.\n", token);
941 acodec = default_acodec;
943 if( i < num_audio_tracks )
945 audio = hb_list_audio_config_item(job->list_audio, i);
946 audio->out.codec = acodec;
948 if( i >= num_audio_tracks )
950 int last_track = hb_list_count(job->list_audio);
951 fprintf(stderr, "More audio codecs than audio tracks, copying track %i and using encoder %s\n",
953 hb_audio_config_t * last_audio = hb_list_audio_config_item( job->list_audio, last_track - 1 );
954 audio = calloc(1, sizeof(*audio));
955 hb_audio_config_init(audio);
956 audio->in.track = last_audio->in.track;
957 audio->out.track = num_audio_tracks++;
958 audio->out.codec = acodec;
959 hb_audio_add(job, audio);
962 token = strtok(NULL, ",");
966 if( i < num_audio_tracks )
968 /* We have fewer inputs than audio tracks, use the default codec for
969 * this container for the remaining tracks. Unless we only have one input
970 * then use that codec instead.
973 acodec = default_acodec;
974 for ( ; i < num_audio_tracks; i++)
976 audio = hb_list_audio_config_item(job->list_audio, i);
977 audio->out.codec = acodec;
986 char * token = strtok(arates, ",");
989 while ( token != NULL )
992 audio = hb_list_audio_config_item(job->list_audio, i);
993 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
995 /* Would probably be better to have this handled in libhb. */
996 audio->out.samplerate = audio->in.samplerate;
1000 if (!is_sample_rate_valid(arate))
1002 fprintf(stderr, "Invalid sample rate %d, using default\n", arate);
1003 arate = default_arate;
1007 for( j = 0; j < hb_audio_rates_count; j++ )
1009 if( !strcmp( token, hb_audio_rates[j].string ) )
1011 arate = hb_audio_rates[j].rate;
1016 audio->out.samplerate = arate;
1018 if( (++i) >= num_audio_tracks )
1019 break; /* We have more inputs than audio tracks, oops */
1020 token = strtok(NULL, ",");
1023 if (i < num_audio_tracks)
1025 /* We have fewer inputs than audio tracks, use default sample rate.
1026 * Unless we only have one input, then use that for all tracks.
1029 arate = default_arate;
1030 for ( ; i < num_audio_tracks; i++)
1032 audio = hb_list_audio_config_item(job->list_audio, i);
1033 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1035 audio->out.samplerate = audio->in.samplerate;
1038 audio->out.samplerate = arate;
1047 char * token = strtok(abitrates, ",");
1050 while ( token != NULL )
1052 abitrate = atoi(token);
1053 audio = hb_list_audio_config_item(job->list_audio, i);
1054 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1056 audio->out.bitrate = audio->in.bitrate; /* See note above about arate. */
1060 audio->out.bitrate = abitrate;
1062 if( (++i) >= num_audio_tracks )
1063 break; /* We have more inputs than audio tracks, oops */
1064 token = strtok(NULL, ",");
1067 if (i < num_audio_tracks)
1069 /* We have fewer inputs than audio tracks, use the default bitrate
1070 * for the remaining tracks. Unless we only have one input, then use
1071 * that for all tracks.
1074 abitrate = default_abitrate;
1075 for (; i < num_audio_tracks; i++)
1077 audio = hb_list_audio_config_item(job->list_audio, i);
1078 if (audio->out.codec == HB_ACODEC_AC3 || audio->out.codec == HB_ACODEC_DCA)
1080 audio->out.bitrate = audio->in.bitrate;
1083 audio->out.bitrate = abitrate;
1090 if ( dynamic_range_compression )
1092 char * token = strtok(dynamic_range_compression, ",");
1094 token = dynamic_range_compression;
1095 while ( token != NULL )
1097 d_r_c = atof(token);
1098 audio = hb_list_audio_config_item(job->list_audio, i);
1099 audio->out.dynamic_range_compression = d_r_c;
1100 if( (++i) >= num_audio_tracks )
1101 break; /* We have more inputs than audio tracks, oops */
1102 token = strtok(NULL, ",");
1105 if (i < num_audio_tracks)
1107 /* We have fewer inputs than audio tracks, use no DRC for the remaining
1108 * tracks. Unless we only have one input, then use the same DRC for all
1113 for (; i < num_audio_tracks; i++)
1115 audio = hb_list_audio_config_item(job->list_audio, i);
1116 audio->out.dynamic_range_compression = d_r_c;
1125 char * token = strtok(mixdowns, ",");
1128 while ( token != NULL )
1130 mixdown = hb_mixdown_get_mixdown_from_short_name(token);
1131 audio = hb_list_audio_config_item(job->list_audio, i);
1132 audio->out.mixdown = mixdown;
1133 if( (++i) >= num_audio_tracks )
1134 break; /* We have more inputs than audio tracks, oops */
1135 token = strtok(NULL, ",");
1138 if (i < num_audio_tracks)
1140 /* We have fewer inputs than audio tracks, use DPLII for the rest. Unless
1141 * we only have one input, then use that.
1144 mixdown = HB_AMIXDOWN_DOLBYPLII;
1145 for (; i < num_audio_tracks; i++)
1147 audio = hb_list_audio_config_item(job->list_audio, i);
1148 audio->out.mixdown = mixdown;
1155 job->vbitrate = hb_calc_bitrate( job, size );
1156 fprintf( stderr, "Calculated bitrate: %d kbps\n",
1162 job->subtitle = sub - 1;
1165 if( native_language )
1167 job->native_language = strdup( native_language );
1175 if ( largeFileSize )
1177 job->largeFileSize = 1;
1181 job->mp4_optimize = 1;
1188 job->file = strdup( output );
1195 if( x264opts != NULL && *x264opts != '\0' )
1197 job->x264opts = x264opts;
1199 else /*avoids a bus error crash when options aren't specified*/
1201 job->x264opts = NULL;
1204 job->maxWidth = maxWidth;
1206 job->maxHeight = maxHeight;
1208 if( subtitle_force )
1210 job->subtitle_force = subtitle_force;
1218 * When subtitle scan is enabled do a fast pre-scan job
1219 * which will determine which subtitles to enable, if any.
1223 x264opts_tmp = job->x264opts;
1225 job->x264opts = NULL;
1227 job->indepth_scan = subtitle_scan;
1228 fprintf( stderr, "Subtitle Scan Enabled - enabling "
1229 "subtitles if found for foreign language segments\n");
1230 job->select_subtitle = malloc(sizeof(hb_subtitle_t*));
1231 *(job->select_subtitle) = NULL;
1234 * Add the pre-scan job
1238 job->x264opts = x264opts_tmp;
1244 * If subtitle_scan is enabled then only turn it on
1245 * for the first pass and then off again for the
1248 hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1250 job->select_subtitle = NULL;
1254 job->indepth_scan = 0;
1258 x264opts2 = strdup(x264opts);
1262 * If turbo options have been selected then append them
1263 * to the x264opts now (size includes one ':' and the '\0')
1265 if( turbo_opts_enabled )
1267 int size = (x264opts ? strlen(x264opts) : 0) + strlen(turbo_opts) + 2;
1270 tmp_x264opts = malloc(size * sizeof(char));
1273 snprintf( tmp_x264opts, size, "%s:%s",
1274 x264opts, turbo_opts );
1278 * No x264opts to modify, but apply the turbo options
1279 * anyway as they may be modifying defaults
1281 snprintf( tmp_x264opts, size, "%s",
1284 x264opts = tmp_x264opts;
1286 fprintf( stderr, "Modified x264 options for pass 1 to append turbo options: %s\n",
1289 job->x264opts = x264opts;
1293 job->select_subtitle = subtitle_tmp;
1297 * On the second pass we turn off subtitle scan so that we
1298 * can actually encode using any subtitles that were auto
1299 * selected in the first pass (using the whacky select-subtitle
1300 * attribute of the job).
1302 job->indepth_scan = 0;
1304 job->x264opts = x264opts2;
1311 * Turn on subtitle scan if requested, note that this option
1312 * precludes encoding of any actual subtitles.
1315 job->indepth_scan = 0;
1323 #define p s.param.working
1324 case HB_STATE_WORKING:
1325 fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
1326 p.job_cur, p.job_count, 100.0 * p.progress );
1327 if( p.seconds > -1 )
1329 fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
1330 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
1331 p.hours, p.minutes, p.seconds );
1337 #define p s.param.muxing
1338 case HB_STATE_MUXING:
1340 if (show_mux_warning)
1342 fprintf( stdout, "\rMuxing: this may take awhile..." );
1344 show_mux_warning = 0;
1350 #define p s.param.workdone
1351 case HB_STATE_WORKDONE:
1352 /* Print error if any, then exit */
1356 fprintf( stderr, "\nRip done!\n" );
1358 case HB_ERROR_CANCELED:
1359 fprintf( stderr, "\nRip canceled.\n" );
1362 fprintf( stderr, "\nRip failed (error %x).\n",
1372 /****************************************************************************
1374 ****************************************************************************/
1375 static volatile int64_t i_die_date = 0;
1376 void SigHandler( int i_signal )
1381 i_die_date = hb_get_date();
1382 fprintf( stderr, "Signal %d received, terminating - do it "
1383 "again in case it gets stuck\n", i_signal );
1385 else if( i_die_date + 500 < hb_get_date() )
1387 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
1392 /****************************************************************************
1394 ****************************************************************************/
1395 static void ShowHelp()
1400 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1402 "### General Handbrake Options------------------------------------------------\n\n"
1403 " -h, --help Print help\n"
1404 " -u, --update Check for updates and exit\n"
1405 " -v, --verbose Be verbose\n"
1406 " -C, --cpu Set CPU count (default: autodetected)\n"
1407 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1408 " if the preset name has spaces, surround it with\n"
1409 " double quotation marks\n"
1410 " -z, --preset-list See a list of available built-in presets\n"
1413 "### Source Options-----------------------------------------------------------\n\n"
1414 " -i, --input <string> Set input device\n"
1415 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1417 " -L, --longest Select the longest title\n"
1418 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1419 " 1 to 3, or \"3\" for chapter 3 only,\n"
1420 " default: all chapters)\n"
1423 "### Destination Options------------------------------------------------------\n\n"
1424 " -o, --output <string> Set output file name\n"
1425 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1426 " autodetected from file name)\n"
1427 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1428 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1429 " -O, --optimize Optimize mp4 files for HTTP streaming\n"
1430 " -I, --ipod-atom Mark mp4 files so iPods will accept them\n"
1433 "### Picture Settings---------------------------------------------------------\n\n"
1434 " -w, --width <number> Set picture width\n"
1435 " -l, --height <number> Set picture height\n"
1436 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1437 " -Y, --maxHeight <#> Set maximum height\n"
1438 " -X, --maxWidth <#> Set maximum width\n"
1439 " -s, --subtitle <number> Select subtitle (default: none)\n"
1440 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1441 " the one that's only used 10 percent of the time\n"
1442 " or less. This should locate subtitles for short\n"
1443 " foreign language segments. Best used in conjunction\n"
1444 " with --subtitle-forced.\n"
1445 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1446 " the subtitle has the forced flag set. May be used in\n"
1447 " conjunction with --subtitle-scan to auto-select\n"
1448 " a stream if it contains forced subtitles.\n"
1449 " -N, --native-language Select subtitles with this language if it does not\n"
1450 " <string> match the Audio language. Provide the language's\n"
1451 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1452 " -m, --markers Add chapter markers (mp4 output format only)\n"
1455 "### Video Options------------------------------------------------------------\n\n"
1456 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1457 " x264,theora default: ffmpeg)\n"
1458 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1459 " -Q, --cqp Use with -q for CQP instead of CRF\n"
1460 " -S, --size <MB> Set target size\n"
1461 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1462 " -r, --rate Set video framerate (" );
1463 for( i = 0; i < hb_video_rates_count; i++ )
1465 fprintf( stderr, hb_video_rates[i].string );
1466 if( i != hb_video_rates_count - 1 )
1467 fprintf( stderr, "/" );
1469 fprintf( stderr, ")\n"
1471 " -2, --two-pass Use two-pass mode\n"
1472 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1473 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1475 " <fast/slow/slower>\n"
1476 " -7, --deblock Deblock video with pp7 filter\n"
1477 " <QP:M> (default 0:2)\n"
1478 " -8, --denoise Denoise video with hqdn3d filter\n"
1479 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1481 " <weak/medium/strong>\n"
1482 " -9, --detelecine Detelecine video with pullup filter\n"
1483 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1484 " -5, --decomb Selectively deinterlaces when it detects combing\n"
1485 " <M:EQ:DF:TR:EQ:DF:TR> (default: 4:10:15:9:10:35:9)\n"
1486 " -g, --grayscale Grayscale encoding\n"
1487 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1488 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1489 " <MOD:PARX:PARY> Takes as optional arguments what number you want\n"
1490 " the dimensions to divide cleanly by (default 16)\n"
1491 " and the pixel ratio to use (default autodetected)\n)"
1497 "### Audio Options-----------------------------------------------------------\n\n"
1498 " -a, --audio <string> Select audio channel(s), separated by commas\n"
1499 " More than one output track can be used for one\n"
1501 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1502 " tracks, default: first one)\n"
1503 " -E, --aencoder <string> Audio encoder(s) (faac/lame/vorbis/ac3) \n"
1504 " ac3 meaning passthrough\n"
1505 " Seperated by commas for more than one audio track.\n"
1506 " (default: guessed)\n"
1507 " -B, --ab <kb/s> Set audio bitrate(s) (default: 128)\n"
1508 " Seperated by commas for more than one audio track.\n"
1509 " -6, --mixdown <string> Format(s) for surround sound downmixing\n"
1510 " Seperated by commas for more than one audio track.\n"
1511 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1512 " -R, --arate Set audio samplerate(s) (" );
1513 for( i = 0; i < hb_audio_rates_count; i++ )
1515 fprintf( stderr, hb_audio_rates[i].string );
1516 if( i != hb_audio_rates_count - 1 )
1517 fprintf( stderr, "/" );
1519 fprintf( stderr, " kHz)\n"
1520 " Seperated by commas for more than one audio track.\n"
1521 " -D, --drc <float> Apply extra dynamic range compression to the audio,\n"
1522 " making soft sounds louder. Range is 1.0 to 4.0\n"
1523 " (too loud), with 1.5 - 2.5 being a useful range.\n"
1524 " Seperated by commas for more than one audio track.\n"
1530 "### Advanced Options---------------------------------------------------------\n\n"
1531 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1532 " same style as mencoder:\n"
1533 " option1=value1:option2=value2\n"
1534 " -T, --turbo When using 2-pass use the turbo options\n"
1535 " on the first pass to improve speed\n"
1536 " (only works with x264, affects PSNR by about 0.05dB,\n"
1537 " and increases first pass speed two to four times)\n"
1538 " -V, --vfr Perform variable framerate detelecine on NTSC video\n"
1542 /****************************************************************************
1544 ****************************************************************************/
1545 static void ShowPresets()
1547 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");
1549 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");
1551 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");
1553 printf("\n+ Blind: -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1555 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");
1557 printf("\n+ Classic: -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1559 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");
1561 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");
1563 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");
1565 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");
1567 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");
1569 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");
1571 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");
1573 printf("\n+ PS3: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1575 printf("\n+ PSP: -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1577 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");
1579 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");
1581 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");
1584 /****************************************************************************
1586 ****************************************************************************/
1587 static int ParseOptions( int argc, char ** argv )
1591 static struct option long_options[] =
1593 { "help", no_argument, NULL, 'h' },
1594 { "update", no_argument, NULL, 'u' },
1595 { "verbose", no_argument, NULL, 'v' },
1596 { "cpu", required_argument, NULL, 'C' },
1598 { "format", required_argument, NULL, 'f' },
1599 { "input", required_argument, NULL, 'i' },
1600 { "output", required_argument, NULL, 'o' },
1601 { "large-file", no_argument, NULL, '4' },
1602 { "optimize", no_argument, NULL, 'O' },
1603 { "ipod-atom", no_argument, NULL, 'I' },
1605 { "title", required_argument, NULL, 't' },
1606 { "longest", no_argument, NULL, 'L' },
1607 { "chapters", required_argument, NULL, 'c' },
1608 { "markers", optional_argument, NULL, 'm' },
1609 { "audio", required_argument, NULL, 'a' },
1610 { "mixdown", required_argument, NULL, '6' },
1611 { "drc", required_argument, NULL, 'D' },
1612 { "subtitle", required_argument, NULL, 's' },
1613 { "subtitle-scan", no_argument, NULL, 'U' },
1614 { "subtitle-forced", no_argument, NULL, 'F' },
1615 { "native-language", required_argument, NULL,'N' },
1617 { "encoder", required_argument, NULL, 'e' },
1618 { "aencoder", required_argument, NULL, 'E' },
1619 { "two-pass", no_argument, NULL, '2' },
1620 { "deinterlace", optional_argument, NULL, 'd' },
1621 { "deblock", optional_argument, NULL, '7' },
1622 { "denoise", optional_argument, NULL, '8' },
1623 { "detelecine", optional_argument, NULL, '9' },
1624 { "decomb", optional_argument, NULL, '5' },
1625 { "grayscale", no_argument, NULL, 'g' },
1626 { "pixelratio", no_argument, NULL, 'p' },
1627 { "loosePixelratio", optional_argument, NULL, 'P' },
1628 { "width", required_argument, NULL, 'w' },
1629 { "height", required_argument, NULL, 'l' },
1630 { "crop", required_argument, NULL, 'n' },
1632 { "vb", required_argument, NULL, 'b' },
1633 { "quality", required_argument, NULL, 'q' },
1634 { "size", required_argument, NULL, 'S' },
1635 { "ab", required_argument, NULL, 'B' },
1636 { "rate", required_argument, NULL, 'r' },
1637 { "arate", required_argument, NULL, 'R' },
1638 { "cqp", no_argument, NULL, 'Q' },
1639 { "x264opts", required_argument, NULL, 'x' },
1640 { "turbo", no_argument, NULL, 'T' },
1641 { "maxHeight", required_argument, NULL, 'Y' },
1642 { "maxWidth", required_argument, NULL, 'X' },
1643 { "preset", required_argument, NULL, 'Z' },
1644 { "preset-list", no_argument, NULL, 'z' },
1645 { "vfr", no_argument, NULL, 'V' },
1650 int option_index = 0;
1653 c = getopt_long( argc, argv,
1654 "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",
1655 long_options, &option_index );
1670 debug = HB_DEBUG_ALL;
1673 cpu = atoi( optarg );
1678 preset_name = strdup(optarg);
1685 format = strdup( optarg );
1688 input = strdup( optarg );
1690 char *devName = bsd_name_for_path( input );
1691 if( devName == NULL )
1695 if( device_is_dvd( devName ) )
1697 char *newInput = malloc( strlen("/dev/") + strlen( devName ) + 1);
1698 sprintf( newInput, "/dev/%s", devName );
1705 output = strdup( optarg );
1718 titleindex = atoi( optarg );
1726 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
1728 chapter_start = start;
1731 else if( sscanf( optarg, "%d", &start ) == 1 )
1733 chapter_start = start;
1734 chapter_end = chapter_start;
1738 fprintf( stderr, "chapters: invalid syntax (%s)\n",
1745 if( optarg != NULL )
1747 marker_file = strdup( optarg );
1749 chapter_markers = 1;
1753 char * token = strtok(optarg, ",");
1756 int track_start, track_end;
1757 while( token != NULL )
1759 audio = calloc(1, sizeof(*audio));
1760 hb_audio_config_init(audio);
1761 if (strlen(token) >= 3)
1763 if (sscanf(token, "%d-%d", &track_start, &track_end) == 2)
1766 for (i = track_start - 1; i < track_end; i++)
1768 if (i != track_start - 1)
1770 audio = calloc(1, sizeof(*audio));
1771 hb_audio_config_init(audio);
1773 audio->in.track = i;
1774 audio->out.track = num_audio_tracks++;
1775 hb_list_add(audios, audio);
1778 else if( !strcasecmp(token, "none" ) )
1780 audio->in.track = audio->out.track = -1;
1781 audio->out.codec = 0;
1782 hb_list_add(audios, audio);
1787 fprintf(stderr, "ERROR: Unable to parse audio input \"%s\", skipping.",
1794 audio->in.track = atoi(token) - 1;
1795 audio->out.track = num_audio_tracks++;
1796 hb_list_add(audios, audio);
1798 token = strtok(NULL, ",");
1803 if( optarg != NULL )
1805 mixdowns = strdup( optarg );
1809 if( optarg != NULL )
1811 dynamic_range_compression = strdup( optarg );
1815 sub = atoi( optarg );
1824 native_language = strdup( optarg );
1830 if( optarg != NULL )
1832 if (!( strcmp( optarg, "fast" ) ))
1834 deinterlace_opt = "-1";
1836 else if (!( strcmp( optarg, "slow" ) ))
1838 deinterlace_opt = "2";
1840 else if (!( strcmp( optarg, "slower" ) ))
1842 deinterlace_opt = "0";
1846 deinterlace_opt = strdup( optarg );
1852 if( optarg != NULL )
1854 deblock_opt = strdup( optarg );
1859 if( optarg != NULL )
1861 if (!( strcmp( optarg, "weak" ) ))
1863 denoise_opt = "2:1:2:3";
1865 else if (!( strcmp( optarg, "medium" ) ))
1867 denoise_opt = "3:2:2:3";
1869 else if (!( strcmp( optarg, "strong" ) ))
1871 denoise_opt = "7:7:5:5";
1875 denoise_opt = strdup( optarg );
1881 if( optarg != NULL )
1883 detelecine_opt = strdup( optarg );
1888 if( optarg != NULL )
1890 decomb_opt = strdup( optarg );
1901 loosePixelratio = 1;
1902 if( optarg != NULL )
1904 sscanf( optarg, "%i:%i:%i", &modulus, &par_width, &par_height );
1908 if( !strcasecmp( optarg, "ffmpeg" ) )
1910 vcodec = HB_VCODEC_FFMPEG;
1912 else if( !strcasecmp( optarg, "xvid" ) )
1914 vcodec = HB_VCODEC_XVID;
1916 else if( !strcasecmp( optarg, "x264" ) )
1918 vcodec = HB_VCODEC_X264;
1920 else if( !strcasecmp( optarg, "x264b13" ) )
1922 vcodec = HB_VCODEC_X264;
1925 else if( !strcasecmp( optarg, "x264b30" ) )
1927 vcodec = HB_VCODEC_X264;
1930 else if( !strcasecmp( optarg, "theora" ) )
1932 vcodec = HB_VCODEC_THEORA;
1936 fprintf( stderr, "invalid codec (%s)\n", optarg );
1941 if( optarg != NULL )
1943 acodecs = strdup( optarg );
1947 width = atoi( optarg );
1950 height = atoi( optarg );
1955 char * tmp = optarg;
1956 for( i = 0; i < 4; i++ )
1960 crop[i] = strtol( tmp, &tmp, 0 );
1969 for( i = 0; i < hb_video_rates_count; i++ )
1971 if( !strcmp( optarg, hb_video_rates[i].string ) )
1973 vrate = hb_video_rates[i].rate;
1979 fprintf( stderr, "invalid framerate %s\n", optarg );
1984 if( optarg != NULL )
1986 arates = strdup( optarg );
1990 vbitrate = atoi( optarg );
1993 vquality = atof( optarg );
1996 size = atoi( optarg );
1999 if( optarg != NULL )
2001 abitrates = strdup( optarg );
2008 x264opts = strdup( optarg );
2011 turbo_opts_enabled = 1;
2014 maxHeight = atoi( optarg );
2017 maxWidth = atoi (optarg );
2024 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
2032 static int CheckOptions( int argc, char ** argv )
2039 if( input == NULL || *input == '\0' )
2041 fprintf( stderr, "Missing input device. Run %s --help for "
2042 "syntax.\n", argv[0] );
2047 if( titleindex > 0 )
2049 if( output == NULL || *output == '\0' )
2051 fprintf( stderr, "Missing output file name. Run %s --help "
2052 "for syntax.\n", argv[0] );
2058 char * p = strrchr( output, '.' );
2061 if( p && !strcasecmp( p, ".avi" ) )
2064 default_acodec = HB_ACODEC_LAME;
2066 else if( p && ( !strcasecmp( p, ".mp4" ) ||
2067 !strcasecmp( p, ".m4v" ) ) )
2073 default_acodec = HB_ACODEC_FAAC;
2075 else if( p && ( !strcasecmp( p, ".ogm" ) ||
2076 !strcasecmp( p, ".ogg" ) ) )
2079 default_acodec = HB_ACODEC_VORBIS;
2081 else if( p && !strcasecmp(p, ".mkv" ) )
2084 default_acodec = HB_ACODEC_AC3;
2088 fprintf( stderr, "Output format couldn't be guessed "
2089 "from file name, using default.\n" );
2093 else if( !strcasecmp( format, "avi" ) )
2096 default_acodec = HB_ACODEC_LAME;
2098 else if( !strcasecmp( format, "mp4" ) ||
2099 !strcasecmp( format, "m4v" ) )
2105 default_acodec = HB_ACODEC_FAAC;
2107 else if( !strcasecmp( format, "ogm" ) ||
2108 !strcasecmp( format, "ogg" ) )
2111 default_acodec = HB_ACODEC_VORBIS;
2113 else if( !strcasecmp( format, "mkv" ) )
2116 default_acodec = HB_ACODEC_AC3;
2120 fprintf( stderr, "Invalid output format (%s). Possible "
2121 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format );
2129 static int get_acodec_for_string( char *codec )
2131 if( !strcasecmp( codec, "ac3" ) )
2133 return HB_ACODEC_AC3;
2135 else if( !strcasecmp( codec, "dts" ) || !strcasecmp( codec, "dca" ) )
2137 return HB_ACODEC_DCA;
2139 else if( !strcasecmp( codec, "lame" ) )
2141 return HB_ACODEC_LAME;
2143 else if( !strcasecmp( codec, "faac" ) )
2145 return HB_ACODEC_FAAC;
2147 else if( !strcasecmp( codec, "vorbis") )
2149 return HB_ACODEC_VORBIS;
2157 static int is_sample_rate_valid(int rate)
2160 for( i = 0; i < hb_audio_rates_count; i++ )
2162 if (rate == hb_audio_rates[i].rate)
2169 /****************************************************************************
2172 * Returns the BSD device name for the block device that contains the
2173 * passed-in path. Returns NULL on failure.
2174 ****************************************************************************/
2175 static char* bsd_name_for_path(char *path)
2179 err = FSPathMakeRef( (const UInt8 *) input, &ref, NULL );
2185 // Get the volume reference number.
2186 FSCatalogInfo catalogInfo;
2187 err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
2193 FSVolumeRefNum volRefNum = catalogInfo.volume;
2195 // Now let's get the device name
2196 GetVolParmsInfoBuffer volumeParms;
2197 err = FSGetVolumeParms( volRefNum, &volumeParms, sizeof( volumeParms ) );
2203 // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the
2204 // vMDeviceID field. It is actually a char * value. This is mentioned in the
2205 // header CoreServices/CarbonCore/Files.h.
2206 return volumeParms.vMDeviceID;
2209 /****************************************************************************
2212 * Returns whether or not the passed in BSD device represents a DVD, or other
2214 ****************************************************************************/
2215 static int device_is_dvd(char *device)
2217 io_service_t service = get_iokit_service(device);
2218 if( service == IO_OBJECT_NULL )
2222 int result = is_dvd_service(service);
2223 IOObjectRelease(service);
2227 /****************************************************************************
2230 * Returns the IOKit service object for the passed in BSD device name.
2231 ****************************************************************************/
2232 static io_service_t get_iokit_service( char *device )
2234 CFMutableDictionaryRef matchingDict;
2235 matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, device );
2236 if( matchingDict == NULL )
2238 return IO_OBJECT_NULL;
2240 // Fetch the object with the matching BSD node name. There should only be
2241 // one match, so IOServiceGetMatchingService is used instead of
2242 // IOServiceGetMatchingServices to simplify the code.
2243 return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );
2246 /****************************************************************************
2249 * Returns whether or not the service passed in is a DVD.
2251 * Searches for an IOMedia object that represents the entire (whole) media that
2252 * the volume is on. If the volume is on partitioned media, the whole media
2253 * object will be a parent of the volume's media object. If the media is not
2254 * partitioned, the volume's media object will be the whole media object.
2255 ****************************************************************************/
2256 static int is_dvd_service( io_service_t service )
2258 kern_return_t kernResult;
2261 // Create an iterator across all parents of the service object passed in.
2262 kernResult = IORegistryEntryCreateIterator( service,
2264 kIORegistryIterateRecursively | kIORegistryIterateParents,
2266 if( kernResult != KERN_SUCCESS )
2270 if( iter == IO_OBJECT_NULL )
2275 // A reference on the initial service object is released in the do-while
2276 // loop below, so add a reference to balance.
2277 IOObjectRetain( service );
2282 if( is_whole_media_service( service ) &&
2283 IOObjectConformsTo( service, kIODVDMediaClass) )
2287 IOObjectRelease( service );
2288 } while( !result && (service = IOIteratorNext( iter )) );
2289 IOObjectRelease( iter );
2294 /****************************************************************************
2295 * is_whole_media_service
2297 * Returns whether or not the service passed in is an IOMedia service and
2298 * represents the "whole" media instead of just a partition.
2300 * The whole media object is indicated in the IORegistry by the presence of a
2301 * property with the key "Whole" and value "Yes".
2302 ****************************************************************************/
2303 static is_whole_media_service( io_service_t service )
2307 if( IOObjectConformsTo( service, kIOMediaClass ) )
2309 CFTypeRef wholeMedia = IORegistryEntryCreateCFProperty( service,
2310 CFSTR( kIOMediaWholeKey ),
2311 kCFAllocatorDefault,
2317 result = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
2318 CFRelease( wholeMedia );
2323 #endif // __APPLE_CC__