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.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
16 static int debug = HB_DEBUG_NONE;
17 static int update = 0;
18 static char * input = NULL;
19 static char * output = NULL;
20 static char * format = NULL;
21 static int titleindex = 1;
22 static int twoPass = 0;
23 static int deinterlace = 0;
24 static int grayscale = 0;
25 static int vcodec = HB_VCODEC_FFMPEG;
26 static int h264_13 = 0;
27 static int h264_30 = 0;
28 static char * audios = NULL;
29 static int audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
32 static int height = 0;
33 static int crop[4] = { -1,-1,-1,-1 };
37 static float vquality = -1.0;
38 static int vbitrate = 0;
40 static int abitrate = 0;
42 static int acodec = 0;
43 static int pixelratio = 0;
44 static int chapter_start = 0;
45 static int chapter_end = 0;
46 static int chapter_markers = 0;
48 static char *x264opts = NULL;
49 static char *x264opts2 = NULL;
50 static int maxHeight = 0;
51 static int maxWidth = 0;
53 /* Exit cleanly on Ctrl-C */
54 static volatile int die = 0;
55 static void SigHandler( int );
58 static void ShowCommands();
59 static void ShowHelp();
60 static int ParseOptions( int argc, char ** argv );
61 static int CheckOptions( int argc, char ** argv );
62 static int HandleEvents( hb_handle_t * h );
64 int main( int argc, char ** argv )
70 /* Parse command line */
71 if( ParseOptions( argc, argv ) ||
72 CheckOptions( argc, argv ) )
78 h = hb_init( debug, update );
81 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
82 hb_get_version( h ), hb_get_build( h ) );
84 /* Check for update */
87 if( ( build = hb_check_update( h, &version ) ) > -1 )
89 fprintf( stderr, "You are using an old version of "
90 "HandBrake.\nLatest is %s (build %d).\n", version,
95 fprintf( stderr, "Your version of HandBrake is up to "
103 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
104 hb_get_cpu_count( h ) > 1 ? "s" : "" );
107 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
108 cpu > 1 ? "s" : "" );
109 hb_set_cpu_count( h, cpu );
112 /* Exit ASAP on Ctrl-C */
113 signal( SIGINT, SigHandler );
115 /* Feed libhb with a DVD to scan */
116 fprintf( stderr, "Opening %s...\n", input );
117 hb_scan( h, input, titleindex );
122 #if !defined(SYS_BEOS)
132 FD_SET( STDIN_FILENO, &fds );
133 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
140 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
142 if( buf[size] == '\n' )
149 if( size >= 256 || buf[size] == '\n' )
178 if( input ) free( input );
179 if( output ) free( output );
180 if( format ) free( format );
181 if( audios ) free( audios );
182 if( x264opts ) free (x264opts );
183 if( x264opts2 ) free (x264opts2 );
185 fprintf( stderr, "HandBrake has exited.\n" );
190 static void ShowCommands()
192 fprintf( stderr, "Commands:\n" );
193 fprintf( stderr, " [h]elp Show this message\n" );
194 fprintf( stderr, " [q]uit Exit HandBrakeCLI\n" );
195 fprintf( stderr, " [p]ause Pause encoding\n" );
196 fprintf( stderr, " [r]esume Resume encoding\n" );
199 static void PrintTitleInfo( hb_title_t * title )
201 hb_chapter_t * chapter;
203 hb_subtitle_t * subtitle;
206 fprintf( stderr, "+ title %d:\n", title->index );
207 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
208 title->vts, title->ttn, title->cell_start, title->cell_end,
209 title->block_count );
210 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
211 title->hours, title->minutes, title->seconds );
212 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
213 title->width, title->height,
214 (float) title->aspect / HB_ASPECT_BASE,
215 (float) title->rate / title->rate_base );
216 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
217 title->crop[1], title->crop[2], title->crop[3] );
218 fprintf( stderr, " + chapters:\n" );
219 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
221 chapter = hb_list_item( title->list_chapter, i );
222 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
223 "%02d:%02d:%02d\n", chapter->index,
224 chapter->cell_start, chapter->cell_end,
225 chapter->block_count, chapter->hours, chapter->minutes,
228 fprintf( stderr, " + audio tracks:\n" );
229 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
231 audio = hb_list_item( title->list_audio, i );
232 if( audio->codec & HB_ACODEC_AC3 )
234 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
235 audio->lang, audio->rate, audio->bitrate );
239 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang );
242 fprintf( stderr, " + subtitle tracks:\n" );
243 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
245 subtitle = hb_list_item( title->list_subtitle, i );
246 fprintf( stderr, " + %d, %s\n", i + 1, subtitle->lang );
250 static int HandleEvents( hb_handle_t * h )
253 hb_get_state( h, &s );
260 #define p s.param.scanning
261 case HB_STATE_SCANNING:
262 /* Show what title is currently being scanned */
263 fprintf( stderr, "Scanning title %d", p.title_cur );
265 fprintf( stderr, " of %d", p.title_count );
266 fprintf( stderr, "...\n" );
270 case HB_STATE_SCANDONE:
276 list = hb_get_titles( h );
278 if( !hb_list_count( list ) )
280 /* No valid title, stop right there */
281 fprintf( stderr, "No title found.\n" );
287 /* Scan-only mode, print infos and exit */
289 for( i = 0; i < hb_list_count( list ); i++ )
291 title = hb_list_item( list, i );
292 PrintTitleInfo( title );
298 /* Set job settings */
299 title = hb_list_item( list, 0 );
302 PrintTitleInfo( title );
304 if( chapter_start && chapter_end )
306 job->chapter_start = MAX( job->chapter_start,
308 job->chapter_end = MIN( job->chapter_end,
310 job->chapter_end = MAX( job->chapter_start,
314 if ( chapter_markers )
316 job->chapter_markers = chapter_markers;
319 if( crop[0] >= 0 && crop[1] >= 0 &&
320 crop[2] >= 0 && crop[3] >= 0 )
322 memcpy( job->crop, crop, 4 * sizeof( int ) );
325 job->deinterlace = deinterlace;
326 job->grayscale = grayscale;
327 job->pixel_ratio = pixelratio;
329 if( width && height )
332 job->height = height;
337 hb_fix_aspect( job, HB_KEEP_WIDTH );
341 job->height = height;
342 hb_fix_aspect( job, HB_KEEP_HEIGHT );
344 else if( !width && !height && !pixelratio )
346 hb_fix_aspect( job, HB_KEEP_WIDTH );
349 if( vquality >= 0.0 && vquality <= 1.0 )
351 job->vquality = vquality;
356 job->vquality = -1.0;
357 job->vbitrate = vbitrate;
361 job->vcodec = vcodec;
365 job->h264_level = 13;
369 job->h264_level = 30;
373 job->vrate = 27000000;
374 job->vrate_base = vrate;
383 if( strcasecmp( audios, "none" ) )
389 if( *tmp < '0' || *tmp > '9' )
391 /* Skip non numeric char */
395 job->audio_mixdowns[audio_count] = audio_mixdown;
396 job->audios[audio_count++] =
397 strtol( tmp, &tmp, 0 ) - 1;
399 job->audios[audio_count] = -1;
408 /* default to the first audio track if none has been specified */
410 job->audio_mixdowns[0] = audio_mixdown;
414 job->abitrate = abitrate;
418 job->acodec = acodec;
423 job->vbitrate = hb_calc_bitrate( job, size );
424 fprintf( stderr, "Calculated bitrate: %d kbps\n",
430 job->subtitle = sub - 1;
437 job->file = strdup( output );
444 if (x264opts != NULL && *x264opts != '\0' )
446 hb_log("Applying the following x264 options: %s", x264opts);
447 job->x264opts = x264opts;
449 else /*avoids a bus error crash when options aren't specified*/
451 job->x264opts = NULL;
454 job->maxWidth = maxWidth;
456 job->maxHeight = maxHeight;
463 job->x264opts = x264opts2;
475 #define p s.param.working
476 case HB_STATE_WORKING:
477 fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
478 p.job_cur, p.job_count, 100.0 * p.progress );
481 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
482 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
483 p.hours, p.minutes, p.seconds );
488 #define p s.param.muxing
489 case HB_STATE_MUXING:
491 fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
496 #define p s.param.workdone
497 case HB_STATE_WORKDONE:
498 /* Print error if any, then exit */
502 fprintf( stderr, "\nRip done!\n" );
504 case HB_ERROR_CANCELED:
505 fprintf( stderr, "\nRip canceled.\n" );
508 fprintf( stderr, "\nRip failed (error %x).\n",
518 /****************************************************************************
520 ****************************************************************************/
521 static volatile int64_t i_die_date = 0;
522 void SigHandler( int i_signal )
527 i_die_date = hb_get_date();
528 fprintf( stderr, "Signal %d received, terminating - do it "
529 "again in case it gets stuck\n", i_signal );
531 else if( i_die_date + 500 < hb_get_date() )
533 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
538 /****************************************************************************
540 ****************************************************************************/
541 static void ShowHelp()
546 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
548 "### General Handbrake Options------------------------------------------------\n\n"
549 " -h, --help Print help\n"
550 " -u, --update Check for updates and exit\n"
551 " -v, --verbose Be verbose\n"
552 " -C, --cpu Set CPU count (default: autodetected)\n"
555 "### Source Options-----------------------------------------------------------\n\n"
556 " -i, --input <string> Set input device\n"
557 " -t, --title <number> Select a title to encode (0 to scan only,\n"
559 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
560 " 1 to 3, or \"3\" for chapter 3 only,\n"
561 " default: all chapters)\n"
564 "### Destination Options------------------------------------------------------\n\n"
565 " -o, --output <string> Set output file name\n"
566 " -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
567 " autodetected from file name)\n"
570 "### Picture Settings---------------------------------------------------------\n\n"
571 " -w, --width <number> Set picture width\n"
572 " -l, --height <number> Set picture height\n"
573 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
574 " -Y, --maxHeight <#> Set maximum height\n"
575 " -X, --maxWidth <#> Set maximum width\n"
576 " -s, --subtitle <number> Select subtitle (default: none)\n"
577 " -m, --markers Add chapter markers (mp4 output format only)\n"
580 "### Video Options------------------------------------------------------------\n\n"
581 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
582 " x264,x264b13,x264b30 default: ffmpeg)\n"
583 " -q, --quality <float> Set video quality (0.0..1.0)\n"
584 " -Q, --crf Use with -q for CRF instead of CQP\n"
585 " -S, --size <MB> Set target size\n"
586 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
587 " -r, --rate Set video framerate (" );
588 for( i = 0; i < hb_video_rates_count; i++ )
590 fprintf( stderr, hb_video_rates[i].string );
591 if( i != hb_video_rates_count - 1 )
592 fprintf( stderr, "/" );
594 fprintf( stderr, ")\n"
596 " -2, --two-pass Use two-pass mode\n"
597 " -d, --deinterlace Deinterlace video\n"
598 " -g, --grayscale Grayscale encoding\n"
599 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
604 "### Audio Options-----------------------------------------------------------\n\n"
605 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
606 " meaning passthrough, default: guessed)\n"
607 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
608 " -a, --audio <string> Select audio channel(s) (\"none\" for no \n"
609 " audio, default: first one)\n"
610 " -6, --mixdown <string> Format for surround sound downmixing\n"
611 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
612 " -R, --arate Set audio samplerate (" );
613 for( i = 0; i < hb_audio_rates_count; i++ )
615 fprintf( stderr, hb_audio_rates[i].string );
616 if( i != hb_audio_rates_count - 1 )
617 fprintf( stderr, "/" );
619 fprintf( stderr, " kHz)\n"
626 "### Advanced H264 Options----------------------------------------------------\n\n"
627 " -x, --x264opts <string> Specify advanced x264 options in the\n"
628 " same style as mencoder:\n"
629 " option1=value1:option2=value2\n" );
632 /****************************************************************************
634 ****************************************************************************/
635 static int ParseOptions( int argc, char ** argv )
639 static struct option long_options[] =
641 { "help", no_argument, NULL, 'h' },
642 { "update", no_argument, NULL, 'u' },
643 { "verbose", no_argument, NULL, 'v' },
644 { "cpu", required_argument, NULL, 'C' },
646 { "format", required_argument, NULL, 'f' },
647 { "input", required_argument, NULL, 'i' },
648 { "output", required_argument, NULL, 'o' },
650 { "title", required_argument, NULL, 't' },
651 { "chapters", required_argument, NULL, 'c' },
652 { "markers", no_argument, NULL, 'm' },
653 { "audio", required_argument, NULL, 'a' },
654 { "mixdown", required_argument, NULL, '6' },
655 { "subtitle", required_argument, NULL, 's' },
657 { "encoder", required_argument, NULL, 'e' },
658 { "aencoder", required_argument, NULL, 'E' },
659 { "two-pass", no_argument, NULL, '2' },
660 { "deinterlace", no_argument, NULL, 'd' },
661 { "grayscale", no_argument, NULL, 'g' },
662 { "pixelratio", no_argument, NULL, 'p' },
663 { "width", required_argument, NULL, 'w' },
664 { "height", required_argument, NULL, 'l' },
665 { "crop", required_argument, NULL, 'n' },
667 { "vb", required_argument, NULL, 'b' },
668 { "quality", required_argument, NULL, 'q' },
669 { "size", required_argument, NULL, 'S' },
670 { "ab", required_argument, NULL, 'B' },
671 { "rate", required_argument, NULL, 'r' },
672 { "arate", required_argument, NULL, 'R' },
673 { "crf", no_argument, NULL, 'Q' },
674 { "x264opts", required_argument, NULL, 'x' },
675 { "maxHeight", required_argument, NULL, 'Y' },
676 { "maxWidth", required_argument, NULL, 'X' },
681 int option_index = 0;
684 c = getopt_long( argc, argv,
685 "hvuC:f:i:o:t:c:ma:6:s:e:E:2dgpw:l:n:b:q:S:B:r:R:Qx:Y:X:",
686 long_options, &option_index );
701 debug = HB_DEBUG_ALL;
704 cpu = atoi( optarg );
708 format = strdup( optarg );
711 input = strdup( optarg );
714 output = strdup( optarg );
718 titleindex = atoi( optarg );
723 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
725 chapter_start = start;
728 else if( sscanf( optarg, "%d", &start ) == 1 )
730 chapter_start = start;
731 chapter_end = chapter_start;
735 fprintf( stderr, "chapters: invalid syntax (%s)\n",
745 audios = strdup( optarg );
748 if( !strcasecmp( optarg, "mono" ) )
750 audio_mixdown = HB_AMIXDOWN_MONO;
752 else if( !strcasecmp( optarg, "stereo" ) )
754 audio_mixdown = HB_AMIXDOWN_STEREO;
756 else if( !strcasecmp( optarg, "dpl1" ) )
758 audio_mixdown = HB_AMIXDOWN_DOLBY;
760 else if( !strcasecmp( optarg, "dpl2" ) )
762 audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
764 else if( !strcasecmp( optarg, "6ch" ) )
766 audio_mixdown = HB_AMIXDOWN_6CH;
770 sub = atoi( optarg );
786 if( !strcasecmp( optarg, "ffmpeg" ) )
788 vcodec = HB_VCODEC_FFMPEG;
790 else if( !strcasecmp( optarg, "xvid" ) )
792 vcodec = HB_VCODEC_XVID;
794 else if( !strcasecmp( optarg, "x264" ) )
796 vcodec = HB_VCODEC_X264;
798 else if( !strcasecmp( optarg, "x264b13" ) )
800 vcodec = HB_VCODEC_X264;
803 else if( !strcasecmp( optarg, "x264b30" ) )
805 vcodec = HB_VCODEC_X264;
810 fprintf( stderr, "invalid codec (%s)\n", optarg );
815 if( !strcasecmp( optarg, "ac3" ) )
817 acodec = HB_ACODEC_AC3;
819 else if( !strcasecmp( optarg, "lame" ) )
821 acodec = HB_ACODEC_LAME;
825 width = atoi( optarg );
828 height = atoi( optarg );
834 for( i = 0; i < 4; i++ )
838 crop[i] = strtol( tmp, &tmp, 0 );
847 for( i = 0; i < hb_video_rates_count; i++ )
849 if( !strcmp( optarg, hb_video_rates[i].string ) )
851 vrate = hb_video_rates[i].rate;
857 fprintf( stderr, "invalid framerate %s\n", optarg );
865 for( i = 0; i < hb_audio_rates_count; i++ )
867 if( !strcmp( optarg, hb_audio_rates[i].string ) )
869 arate = hb_audio_rates[i].rate;
875 fprintf( stderr, "invalid framerate %s\n", optarg );
880 vbitrate = atoi( optarg );
883 vquality = atof( optarg );
886 size = atoi( optarg );
889 abitrate = atoi( optarg );
895 x264opts = strdup( optarg );
896 x264opts2 = strdup( optarg );
899 maxHeight = atoi( optarg );
902 maxWidth = atoi (optarg );
906 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
914 static int CheckOptions( int argc, char ** argv )
921 if( input == NULL || *input == '\0' )
923 fprintf( stderr, "Missing input device. Run %s --help for "
924 "syntax.\n", argv[0] );
931 if( output == NULL || *output == '\0' )
933 fprintf( stderr, "Missing output file name. Run %s --help "
934 "for syntax.\n", argv[0] );
940 char * p = strrchr( output, '.' );
943 if( p && !strcasecmp( p, ".avi" ) )
947 else if( p && ( !strcasecmp( p, ".mp4" ) ||
948 !strcasecmp( p, ".m4v" ) ) )
955 else if( p && ( !strcasecmp( p, ".ogm" ) ||
956 !strcasecmp( p, ".ogg" ) ) )
962 fprintf( stderr, "Output format couldn't be guessed "
963 "from file name, using default.\n" );
967 else if( !strcasecmp( format, "avi" ) )
971 else if( !strcasecmp( format, "mp4" ) )
978 else if( !strcasecmp( format, "ogm" ) ||
979 !strcasecmp( format, "ogg" ) )
985 fprintf( stderr, "Invalid output format (%s). Possible "
986 "choices are avi, mp4 and ogm\n.", format );
992 if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
994 acodec = HB_ACODEC_FAAC;
996 else if( mux == HB_MUX_AVI )
998 acodec = HB_ACODEC_LAME;
1000 else if( mux == HB_MUX_OGM )
1002 acodec = HB_ACODEC_VORBIS;