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 surround = 0;
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;
47 static char *x264opts = NULL;
48 static char *x264opts2 = NULL;
49 static int maxHeight = 0;
50 static int maxWidth = 0;
52 /* Exit cleanly on Ctrl-C */
53 static volatile int die = 0;
54 static void SigHandler( int );
57 static void ShowCommands();
58 static void ShowHelp();
59 static int ParseOptions( int argc, char ** argv );
60 static int CheckOptions( int argc, char ** argv );
61 static int HandleEvents( hb_handle_t * h );
63 int main( int argc, char ** argv )
69 /* Parse command line */
70 if( ParseOptions( argc, argv ) ||
71 CheckOptions( argc, argv ) )
77 h = hb_init( debug, update );
80 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
81 hb_get_version( h ), hb_get_build( h ) );
83 /* Check for update */
86 if( ( build = hb_check_update( h, &version ) ) > -1 )
88 fprintf( stderr, "You are using an old version of "
89 "HandBrake.\nLatest is %s (build %d).\n", version,
94 fprintf( stderr, "Your version of HandBrake is up to "
102 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
103 hb_get_cpu_count( h ) > 1 ? "s" : "" );
106 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
107 cpu > 1 ? "s" : "" );
108 hb_set_cpu_count( h, cpu );
111 /* Exit ASAP on Ctrl-C */
112 signal( SIGINT, SigHandler );
114 /* Feed libhb with a DVD to scan */
115 fprintf( stderr, "Opening %s...\n", input );
116 hb_scan( h, input, titleindex );
121 #if !defined(SYS_BEOS)
131 FD_SET( STDIN_FILENO, &fds );
132 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
139 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
141 if( buf[size] == '\n' )
148 if( size >= 256 || buf[size] == '\n' )
177 if( input ) free( input );
178 if( output ) free( output );
179 if( format ) free( format );
180 if( audios ) free( audios );
181 if( x264opts ) free (x264opts );
182 if( x264opts2 ) free (x264opts2 );
184 fprintf( stderr, "HandBrake has exited.\n" );
189 static void ShowCommands()
191 fprintf( stderr, "Commands:\n" );
192 fprintf( stderr, " [h]elp Show this message\n" );
193 fprintf( stderr, " [q]uit Exit HandBrakeCLI\n" );
194 fprintf( stderr, " [p]ause Pause encoding\n" );
195 fprintf( stderr, " [r]esume Resume encoding\n" );
198 static void PrintTitleInfo( hb_title_t * title )
200 hb_chapter_t * chapter;
202 hb_subtitle_t * subtitle;
205 fprintf( stderr, "+ title %d:\n", title->index );
206 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
207 title->vts, title->ttn, title->cell_start, title->cell_end,
208 title->block_count );
209 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
210 title->hours, title->minutes, title->seconds );
211 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
212 title->width, title->height,
213 (float) title->aspect / HB_ASPECT_BASE,
214 (float) title->rate / title->rate_base );
215 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
216 title->crop[1], title->crop[2], title->crop[3] );
217 fprintf( stderr, " + chapters:\n" );
218 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
220 chapter = hb_list_item( title->list_chapter, i );
221 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
222 "%02d:%02d:%02d\n", chapter->index,
223 chapter->cell_start, chapter->cell_end,
224 chapter->block_count, chapter->hours, chapter->minutes,
227 fprintf( stderr, " + audio tracks:\n" );
228 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
230 audio = hb_list_item( title->list_audio, i );
231 if( audio->codec & HB_ACODEC_AC3 )
233 fprintf( stderr, " + %x, %s, %dHz, %dbps\n", audio->id,
234 audio->lang, audio->rate, audio->bitrate );
238 fprintf( stderr, " + %x, %s\n", audio->id, audio->lang );
241 fprintf( stderr, " + subtitle tracks:\n" );
242 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
244 subtitle = hb_list_item( title->list_subtitle, i );
245 fprintf( stderr, " + %x, %s\n", subtitle->id, subtitle->lang );
249 static int HandleEvents( hb_handle_t * h )
252 hb_get_state( h, &s );
259 #define p s.param.scanning
260 case HB_STATE_SCANNING:
261 /* Show what title is currently being scanned */
262 fprintf( stderr, "Scanning title %d", p.title_cur );
264 fprintf( stderr, " of %d", p.title_count );
265 fprintf( stderr, "...\n" );
269 case HB_STATE_SCANDONE:
275 list = hb_get_titles( h );
277 if( !hb_list_count( list ) )
279 /* No valid title, stop right there */
280 fprintf( stderr, "No title found.\n" );
286 /* Scan-only mode, print infos and exit */
288 for( i = 0; i < hb_list_count( list ); i++ )
290 title = hb_list_item( list, i );
291 PrintTitleInfo( title );
297 /* Set job settings */
298 title = hb_list_item( list, 0 );
301 PrintTitleInfo( title );
303 if( chapter_start && chapter_end )
305 job->chapter_start = MAX( job->chapter_start,
307 job->chapter_end = MIN( job->chapter_end,
309 job->chapter_end = MAX( job->chapter_start,
313 if( crop[0] >= 0 && crop[1] >= 0 &&
314 crop[2] >= 0 && crop[3] >= 0 )
316 memcpy( job->crop, crop, 4 * sizeof( int ) );
319 job->deinterlace = deinterlace;
320 job->grayscale = grayscale;
321 job->pixel_ratio = pixelratio;
323 if( width && height )
326 job->height = height;
331 hb_fix_aspect( job, HB_KEEP_WIDTH );
335 job->height = height;
336 hb_fix_aspect( job, HB_KEEP_HEIGHT );
338 else if( !width && !height && !pixelratio )
340 hb_fix_aspect( job, HB_KEEP_WIDTH );
343 if( vquality >= 0.0 && vquality <= 1.0 )
345 job->vquality = vquality;
350 job->vquality = -1.0;
351 job->vbitrate = vbitrate;
355 job->vcodec = vcodec;
359 job->h264_level = 13;
363 job->h264_level = 30;
367 job->vrate = 27000000;
368 job->vrate_base = vrate;
377 if( strcasecmp( audios, "none" ) )
383 if( *tmp < '0' || *tmp > '9' )
385 /* Skip non numeric char */
389 job->audios[audio_count++] =
390 strtol( tmp, &tmp, 0 ) - 1;
392 job->audios[audio_count] = -1;
405 job->abitrate = abitrate;
409 job->acodec = acodec;
414 job->vbitrate = hb_calc_bitrate( job, size );
415 fprintf( stderr, "Calculated bitrate: %d kbps\n",
421 job->subtitle = sub - 1;
428 job->file = strdup( output );
435 if (x264opts != NULL && *x264opts != '\0' )
437 hb_log("Applying the following x264 options: %s", x264opts);
438 job->x264opts = x264opts;
440 else /*avoids a bus error crash when options aren't specified*/
442 job->x264opts = NULL;
445 job->maxWidth = maxWidth;
447 job->maxHeight = maxHeight;
454 job->x264opts = x264opts2;
466 #define p s.param.working
467 case HB_STATE_WORKING:
468 fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
469 p.job_cur, p.job_count, 100.0 * p.progress );
472 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
473 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
474 p.hours, p.minutes, p.seconds );
479 #define p s.param.muxing
480 case HB_STATE_MUXING:
482 fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
487 #define p s.param.workdone
488 case HB_STATE_WORKDONE:
489 /* Print error if any, then exit */
493 fprintf( stderr, "\nRip done!\n" );
495 case HB_ERROR_CANCELED:
496 fprintf( stderr, "\nRip canceled.\n" );
499 fprintf( stderr, "\nRip failed (error %x).\n",
509 /****************************************************************************
511 ****************************************************************************/
512 static volatile int64_t i_die_date = 0;
513 void SigHandler( int i_signal )
518 i_die_date = hb_get_date();
519 fprintf( stderr, "Signal %d received, terminating - do it "
520 "again in case it gets stuck\n", i_signal );
522 else if( i_die_date + 500 < hb_get_date() )
524 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
529 /****************************************************************************
531 ****************************************************************************/
532 static void ShowHelp()
537 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
539 " -h, --help Print help\n"
540 " -u, --update Check for updates and exit\n"
541 " -v, --verbose Be verbose\n"
542 " -C, --cpu Set CPU count (default: autodetected)\n"
544 " -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
545 " autodetected from file name)\n"
546 " -i, --input <string> Set input device\n"
547 " -o, --output <string> Set output file name\n"
549 " -t, --title <number> Select a title to encode (0 to scan only,\n"
551 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
552 " 1 to 3, or \"3\" for chapter 3 only,\n"
553 " default: all chapters)\n"
554 " -a, --audio <string> Select audio channel(s) (\"none\" for no \n"
555 " audio, default: first one)\n"
556 " -6, --surround Export 5.1 surround as 6-channel AAC\n"
558 " -s, --subtitle <number> Select subtitle (default: none)\n"
559 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
560 " x264,x264b13,x264b30 default: ffmpeg)\n"
561 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
562 " meaning passthrough, default: guessed)\n"
563 " -2, --two-pass Use two-pass mode\n"
564 " -d, --deinterlace Deinterlace video\n"
565 " -g, --grayscale Grayscale encoding\n"
566 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
568 " -r, --rate Set video framerate (" );
569 for( i = 0; i < hb_video_rates_count; i++ )
571 fprintf( stderr, hb_video_rates[i].string );
572 if( i != hb_video_rates_count - 1 )
573 fprintf( stderr, "/" );
575 fprintf( stderr, ")\n"
576 " -R, --arate Set audio samplerate (" );
577 for( i = 0; i < hb_audio_rates_count; i++ )
579 fprintf( stderr, hb_audio_rates[i].string );
580 if( i != hb_audio_rates_count - 1 )
581 fprintf( stderr, "/" );
583 fprintf( stderr, " kHz)\n"
584 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
585 " -q, --quality <float> Set video quality (0.0..1.0)\n"
586 " -Q, --crf Use with -q for CRF instead of CQP\n"
587 " -S, --size <MB> Set target size\n"
588 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
589 " -w, --width <number> Set picture width\n"
590 " -l, --height <number> Set picture height\n"
591 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
592 " -Y, --maxHeight <#> Set maximum height\n"
593 " -X, --maxWidth <#> Set maximum width\n"
595 " -x, --x264opts <string> Specify advanced x264 options in the\n"
596 " same style as mencoder:\n"
597 " option1=value1:option2=value2\n" );
600 /****************************************************************************
602 ****************************************************************************/
603 static int ParseOptions( int argc, char ** argv )
607 static struct option long_options[] =
609 { "help", no_argument, NULL, 'h' },
610 { "update", no_argument, NULL, 'u' },
611 { "verbose", no_argument, NULL, 'v' },
612 { "cpu", required_argument, NULL, 'C' },
614 { "format", required_argument, NULL, 'f' },
615 { "input", required_argument, NULL, 'i' },
616 { "output", required_argument, NULL, 'o' },
618 { "title", required_argument, NULL, 't' },
619 { "chapters", required_argument, NULL, 'c' },
620 { "audio", required_argument, NULL, 'a' },
621 { "surround", no_argument, NULL, '6' },
622 { "subtitle", required_argument, NULL, 's' },
624 { "encoder", required_argument, NULL, 'e' },
625 { "aencoder", required_argument, NULL, 'E' },
626 { "two-pass", no_argument, NULL, '2' },
627 { "deinterlace", no_argument, NULL, 'd' },
628 { "grayscale", no_argument, NULL, 'g' },
629 { "pixelratio", no_argument, NULL, 'p' },
630 { "width", required_argument, NULL, 'w' },
631 { "height", required_argument, NULL, 'l' },
632 { "crop", required_argument, NULL, 'n' },
634 { "vb", required_argument, NULL, 'b' },
635 { "quality", required_argument, NULL, 'q' },
636 { "size", required_argument, NULL, 'S' },
637 { "ab", required_argument, NULL, 'B' },
638 { "rate", required_argument, NULL, 'r' },
639 { "arate", required_argument, NULL, 'R' },
640 { "crf", no_argument, NULL, 'Q' },
641 { "x264opts", required_argument, NULL, 'x' },
642 { "maxHeight", required_argument, NULL, 'Y' },
643 { "maxWidth", required_argument, NULL, 'X' },
648 int option_index = 0;
651 c = getopt_long( argc, argv,
652 "hvuC:f:i:o:t:c:a:6s:e:E:2dgpw:l:n:b:q:S:B:r:R:Qx:Y:X:",
653 long_options, &option_index );
668 debug = HB_DEBUG_ALL;
671 cpu = atoi( optarg );
675 format = strdup( optarg );
678 input = strdup( optarg );
681 output = strdup( optarg );
685 titleindex = atoi( optarg );
690 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
692 chapter_start = start;
695 else if( sscanf( optarg, "%d", &start ) == 1 )
697 chapter_start = start;
698 chapter_end = chapter_start;
702 fprintf( stderr, "chapters: invalid syntax (%s)\n",
709 audios = strdup( optarg );
715 sub = atoi( optarg );
731 if( !strcasecmp( optarg, "ffmpeg" ) )
733 vcodec = HB_VCODEC_FFMPEG;
735 else if( !strcasecmp( optarg, "xvid" ) )
737 vcodec = HB_VCODEC_XVID;
739 else if( !strcasecmp( optarg, "x264" ) )
741 vcodec = HB_VCODEC_X264;
743 else if( !strcasecmp( optarg, "x264b13" ) )
745 vcodec = HB_VCODEC_X264;
748 else if( !strcasecmp( optarg, "x264b30" ) )
750 vcodec = HB_VCODEC_X264;
755 fprintf( stderr, "invalid codec (%s)\n", optarg );
760 if( !strcasecmp( optarg, "ac3" ) )
762 acodec = HB_ACODEC_AC3;
764 else if( !strcasecmp( optarg, "lame" ) )
766 acodec = HB_ACODEC_LAME;
770 width = atoi( optarg );
773 height = atoi( optarg );
779 for( i = 0; i < 4; i++ )
783 crop[i] = strtol( tmp, &tmp, 0 );
792 for( i = 0; i < hb_video_rates_count; i++ )
794 if( !strcmp( optarg, hb_video_rates[i].string ) )
796 vrate = hb_video_rates[i].rate;
802 fprintf( stderr, "invalid framerate %s\n", optarg );
810 for( i = 0; i < hb_audio_rates_count; i++ )
812 if( !strcmp( optarg, hb_audio_rates[i].string ) )
814 arate = hb_audio_rates[i].rate;
820 fprintf( stderr, "invalid framerate %s\n", optarg );
825 vbitrate = atoi( optarg );
828 vquality = atof( optarg );
831 size = atoi( optarg );
834 abitrate = atoi( optarg );
840 x264opts = strdup( optarg );
841 x264opts2 = strdup( optarg );
844 maxHeight = atoi( optarg );
847 maxWidth = atoi (optarg );
851 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
859 static int CheckOptions( int argc, char ** argv )
866 if( input == NULL || *input == '\0' )
868 fprintf( stderr, "Missing input device. Run %s --help for "
869 "syntax.\n", argv[0] );
876 if( output == NULL || *output == '\0' )
878 fprintf( stderr, "Missing output file name. Run %s --help "
879 "for syntax.\n", argv[0] );
885 char * p = strrchr( output, '.' );
888 if( p && !strcasecmp( p, ".avi" ) )
892 else if( p && !strcasecmp( p, ".mp4" ) )
899 else if( p && ( !strcasecmp( p, ".ogm" ) ||
900 !strcasecmp( p, ".ogg" ) ) )
906 fprintf( stderr, "Output format couldn't be guessed "
907 "from file name, using default.\n" );
911 else if( !strcasecmp( format, "avi" ) )
915 else if( !strcasecmp( format, "mp4" ) )
922 else if( !strcasecmp( format, "ogm" ) ||
923 !strcasecmp( format, "ogg" ) )
929 fprintf( stderr, "Invalid output format (%s). Possible "
930 "choices are avi, mp4 and ogm\n.", format );
936 if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
938 acodec = HB_ACODEC_FAAC;
940 else if( mux == HB_MUX_AVI )
942 acodec = HB_ACODEC_LAME;
944 else if( mux == HB_MUX_OGM )
946 acodec = HB_ACODEC_VORBIS;
950 if (acodec != HB_ACODEC_FAAC)
952 /* only attempt 5.1 export if exporting to AAC */