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;
31 static int height = 0;
32 static int crop[4] = { -1,-1,-1,-1 };
36 static float vquality = -1.0;
37 static int vbitrate = 0;
39 static int abitrate = 0;
41 static int acodec = 0;
42 static int chapter_start = 0;
43 static int chapter_end = 0;
45 /* Exit cleanly on Ctrl-C */
46 static volatile int die = 0;
47 static void SigHandler( int );
50 static void ShowCommands();
51 static void ShowHelp();
52 static int ParseOptions( int argc, char ** argv );
53 static int CheckOptions( int argc, char ** argv );
54 static int HandleEvents( hb_handle_t * h );
56 int main( int argc, char ** argv )
62 /* Parse command line */
63 if( ParseOptions( argc, argv ) ||
64 CheckOptions( argc, argv ) )
70 h = hb_init( debug, update );
73 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
74 hb_get_version( h ), hb_get_build( h ) );
76 /* Check for update */
79 if( ( build = hb_check_update( h, &version ) ) > -1 )
81 fprintf( stderr, "You are using an old version of "
82 "HandBrake.\nLatest is %s (build %d).\n", version,
87 fprintf( stderr, "Your version of HandBrake is up to "
95 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
96 hb_get_cpu_count( h ) > 1 ? "s" : "" );
99 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
100 cpu > 1 ? "s" : "" );
101 hb_set_cpu_count( h, cpu );
104 /* Exit ASAP on Ctrl-C */
105 signal( SIGINT, SigHandler );
107 /* Feed libhb with a DVD to scan */
108 fprintf( stderr, "Opening %s...\n", input );
109 hb_scan( h, input, titleindex );
114 #if !defined(SYS_BEOS)
124 FD_SET( STDIN_FILENO, &fds );
125 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
132 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
134 if( buf[size] == '\n' )
141 if( size >= 256 || buf[size] == '\n' )
169 if( input ) free( input );
170 if( output ) free( output );
171 if( format ) free( format );
172 if( audios ) free( audios );
174 fprintf( stderr, "HandBrake has exited.\n" );
179 static void ShowCommands()
181 fprintf( stderr, "Commands:\n" );
182 fprintf( stderr, " [h]elp Show this message\n" );
183 fprintf( stderr, " [q]uit Exit HBTest\n" );
184 fprintf( stderr, " [p]ause Pause encoding\n" );
185 fprintf( stderr, " [r]esume Resume encoding\n" );
188 static void PrintTitleInfo( hb_title_t * title )
190 hb_chapter_t * chapter;
192 hb_subtitle_t * subtitle;
195 fprintf( stderr, "+ title %d:\n", title->index );
196 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
197 title->vts, title->ttn, title->cell_start, title->cell_end,
198 title->block_count );
199 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
200 title->hours, title->minutes, title->seconds );
201 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
202 title->width, title->height,
203 (float) title->aspect / HB_ASPECT_BASE,
204 (float) title->rate / title->rate_base );
205 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
206 title->crop[1], title->crop[2], title->crop[3] );
207 fprintf( stderr, " + chapters:\n" );
208 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
210 chapter = hb_list_item( title->list_chapter, i );
211 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
212 "%02d:%02d:%02d\n", chapter->index,
213 chapter->cell_start, chapter->cell_end,
214 chapter->block_count, chapter->hours, chapter->minutes,
217 fprintf( stderr, " + audio tracks:\n" );
218 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
220 audio = hb_list_item( title->list_audio, i );
221 if( audio->codec & HB_ACODEC_AC3 )
223 fprintf( stderr, " + %x, %s, %dHz, %dbps\n", audio->id,
224 audio->lang, audio->rate, audio->bitrate );
228 fprintf( stderr, " + %x, %s\n", audio->id, audio->lang );
231 fprintf( stderr, " + subtitle tracks:\n" );
232 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
234 subtitle = hb_list_item( title->list_subtitle, i );
235 fprintf( stderr, " + %x, %s\n", subtitle->id, subtitle->lang );
239 static int HandleEvents( hb_handle_t * h )
242 hb_get_state( h, &s );
249 #define p s.param.scanning
250 case HB_STATE_SCANNING:
251 /* Show what title is currently being scanned */
252 fprintf( stderr, "Scanning title %d", p.title_cur );
254 fprintf( stderr, " of %d", p.title_count );
255 fprintf( stderr, "...\n" );
259 case HB_STATE_SCANDONE:
265 list = hb_get_titles( h );
267 if( !hb_list_count( list ) )
269 /* No valid title, stop right there */
270 fprintf( stderr, "No title found.\n" );
276 /* Scan-only mode, print infos and exit */
278 for( i = 0; i < hb_list_count( list ); i++ )
280 title = hb_list_item( list, i );
281 PrintTitleInfo( title );
287 /* Set job settings */
288 title = hb_list_item( list, 0 );
291 PrintTitleInfo( title );
293 if( chapter_start && chapter_end )
295 job->chapter_start = MAX( job->chapter_start,
297 job->chapter_end = MIN( job->chapter_end,
299 job->chapter_end = MAX( job->chapter_start,
303 if( crop[0] >= 0 && crop[1] >= 0 &&
304 crop[2] >= 0 && crop[3] >= 0 )
306 memcpy( job->crop, crop, 4 * sizeof( int ) );
309 job->deinterlace = deinterlace;
310 job->grayscale = grayscale;
312 if( width && height )
315 job->height = height;
320 hb_fix_aspect( job, HB_KEEP_WIDTH );
324 job->height = height;
325 hb_fix_aspect( job, HB_KEEP_HEIGHT );
329 hb_fix_aspect( job, HB_KEEP_WIDTH );
332 if( vquality >= 0.0 && vquality <= 1.0 )
334 job->vquality = vquality;
339 job->vquality = -1.0;
340 job->vbitrate = vbitrate;
344 job->vcodec = vcodec;
348 job->h264_level = 13;
352 job->h264_level = 30;
356 job->vrate = 27000000;
357 job->vrate_base = vrate;
366 if( strcasecmp( audios, "none" ) )
372 if( *tmp < '0' || *tmp > '9' )
374 /* Skip non numeric char */
378 job->audios[audio_count++] =
379 strtol( tmp, &tmp, 0 ) - 1;
381 job->audios[audio_count] = -1;
390 job->abitrate = abitrate;
394 job->acodec = acodec;
399 job->vbitrate = hb_calc_bitrate( job, size );
400 fprintf( stderr, "Calculated bitrate: %d kbps\n",
406 job->subtitle = sub - 1;
413 job->file = strdup( output );
431 #define p s.param.working
432 case HB_STATE_WORKING:
433 fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
434 p.job_cur, p.job_count, 100.0 * p.progress );
437 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
438 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
439 p.hours, p.minutes, p.seconds );
444 #define p s.param.workdone
445 case HB_STATE_WORKDONE:
446 /* Print error if any, then exit */
450 fprintf( stderr, "\nRip done!\n" );
452 case HB_ERROR_CANCELED:
453 fprintf( stderr, "\nRip canceled.\n" );
456 fprintf( stderr, "\nRip failed (error %x).\n",
466 /****************************************************************************
468 ****************************************************************************/
469 static volatile int64_t i_die_date = 0;
470 void SigHandler( int i_signal )
475 i_die_date = hb_get_date();
476 fprintf( stderr, "Signal %d received, terminating - do it "
477 "again in case it gets stuck\n", i_signal );
479 else if( i_die_date + 500 < hb_get_date() )
481 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
486 /****************************************************************************
488 ****************************************************************************/
489 static void ShowHelp()
494 "Syntax: HBTest [options] -i <device> -o <file>\n"
496 " -h, --help Print help\n"
497 " -u, --update Check for updates and exit\n"
498 " -v, --verbose Be verbose\n"
499 " -C, --cpu Set CPU count (default: autodetected)\n"
501 " -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
502 " autodetected from file name)\n"
503 " -i, --input <string> Set input device\n"
504 " -o, --output <string> Set output file name\n"
506 " -t, --title <number> Select a title to encode (0 to scan only,\n"
508 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
509 " 1 to 3, or \"3\" for chapter 3 only,\n"
510 " default: all chapters)\n"
511 " -a, --audio <string> Select audio channel(s) (\"none\" for no \n"
512 " audio, default: first one)\n"
514 " -s, --subtitle <number> Select subtitle (default: none)\n"
515 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
516 " x264,x264b13,x264b30 default: ffmpeg)\n"
517 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
518 " meaning passthrough, default: guessed)\n"
519 " -2, --two-pass Use two-pass mode\n"
520 " -d, --deinterlace Deinterlace video\n"
521 " -g, --grayscale Grayscale encoding\n"
523 " -r, --rate Set video framerate (" );
524 for( i = 0; i < hb_video_rates_count; i++ )
526 fprintf( stderr, hb_video_rates[i].string );
527 if( i != hb_video_rates_count - 1 )
528 fprintf( stderr, "/" );
530 fprintf( stderr, ")\n"
531 " -R, --arate Set audio samplerate (" );
532 for( i = 0; i < hb_audio_rates_count; i++ )
534 fprintf( stderr, hb_audio_rates[i].string );
535 if( i != hb_audio_rates_count - 1 )
536 fprintf( stderr, "/" );
538 fprintf( stderr, " kHz)\n"
539 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
540 " -q, --quality <float> Set video quality (0.0..1.0)\n"
541 " -S, --size <MB> Set target size\n"
542 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
543 " -w, --width <number> Set picture width\n"
544 " -l, --height <number> Set picture height\n"
545 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n" );
548 /****************************************************************************
550 ****************************************************************************/
551 static int ParseOptions( int argc, char ** argv )
555 static struct option long_options[] =
557 { "help", no_argument, NULL, 'h' },
558 { "update", no_argument, NULL, 'u' },
559 { "verbose", no_argument, NULL, 'v' },
560 { "cpu", required_argument, NULL, 'C' },
562 { "format", required_argument, NULL, 'f' },
563 { "input", required_argument, NULL, 'i' },
564 { "output", required_argument, NULL, 'o' },
566 { "title", required_argument, NULL, 't' },
567 { "chapters", required_argument, NULL, 'c' },
568 { "audio", required_argument, NULL, 'a' },
569 { "subtitle", required_argument, NULL, 's' },
571 { "encoder", required_argument, NULL, 'e' },
572 { "aencoder", required_argument, NULL, 'E' },
573 { "two-pass", no_argument, NULL, '2' },
574 { "deinterlace", no_argument, NULL, 'd' },
575 { "grayscale", no_argument, NULL, 'g' },
576 { "width", required_argument, NULL, 'w' },
577 { "height", required_argument, NULL, 'l' },
578 { "crop", required_argument, NULL, 'n' },
580 { "vb", required_argument, NULL, 'b' },
581 { "quality", required_argument, NULL, 'q' },
582 { "size", required_argument, NULL, 'S' },
583 { "ab", required_argument, NULL, 'B' },
584 { "rate", required_argument, NULL, 'r' },
585 { "arate", required_argument, NULL, 'R' },
590 int option_index = 0;
593 c = getopt_long( argc, argv,
594 "hvuC:f:i:o:t:c:a:s:e:E:2dgw:l:n:b:q:S:B:r:R:",
595 long_options, &option_index );
610 debug = HB_DEBUG_ALL;
613 cpu = atoi( optarg );
617 format = strdup( optarg );
620 input = strdup( optarg );
623 output = strdup( optarg );
627 titleindex = atoi( optarg );
632 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
634 chapter_start = start;
637 else if( sscanf( optarg, "%d", &start ) == 1 )
639 chapter_start = start;
640 chapter_end = chapter_start;
644 fprintf( stderr, "chapters: invalid syntax (%s)\n",
651 audios = strdup( optarg );
654 sub = atoi( optarg );
667 if( !strcasecmp( optarg, "ffmpeg" ) )
669 vcodec = HB_VCODEC_FFMPEG;
671 else if( !strcasecmp( optarg, "xvid" ) )
673 vcodec = HB_VCODEC_XVID;
675 else if( !strcasecmp( optarg, "x264" ) )
677 vcodec = HB_VCODEC_X264;
679 else if( !strcasecmp( optarg, "x264b13" ) )
681 vcodec = HB_VCODEC_X264;
684 else if( !strcasecmp( optarg, "x264b30" ) )
686 vcodec = HB_VCODEC_X264;
691 fprintf( stderr, "invalid codec (%s)\n", optarg );
696 if( !strcasecmp( optarg, "ac3" ) )
698 acodec = HB_ACODEC_AC3;
700 else if( !strcasecmp( optarg, "lame" ) )
702 acodec = HB_ACODEC_LAME;
706 width = atoi( optarg );
709 height = atoi( optarg );
715 for( i = 0; i < 4; i++ )
719 crop[i] = strtol( tmp, &tmp, 0 );
728 for( i = 0; i < hb_video_rates_count; i++ )
730 if( !strcmp( optarg, hb_video_rates[i].string ) )
732 vrate = hb_video_rates[i].rate;
738 fprintf( stderr, "invalid framerate %s\n", optarg );
746 for( i = 0; i < hb_audio_rates_count; i++ )
748 if( !strcmp( optarg, hb_audio_rates[i].string ) )
750 arate = hb_audio_rates[i].rate;
756 fprintf( stderr, "invalid framerate %s\n", optarg );
761 vbitrate = atoi( optarg );
764 vquality = atof( optarg );
767 size = atoi( optarg );
770 abitrate = atoi( optarg );
774 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
782 static int CheckOptions( int argc, char ** argv )
789 if( input == NULL || *input == '\0' )
791 fprintf( stderr, "Missing input device. Run %s --help for "
792 "syntax.\n", argv[0] );
799 if( output == NULL || *output == '\0' )
801 fprintf( stderr, "Missing output file name. Run %s --help "
802 "for syntax.\n", argv[0] );
808 char * p = strrchr( output, '.' );
811 if( p && !strcasecmp( p, ".avi" ) )
815 else if( p && !strcasecmp( p, ".mp4" ) )
822 else if( p && ( !strcasecmp( p, ".ogm" ) ||
823 !strcasecmp( p, ".ogg" ) ) )
829 fprintf( stderr, "Output format couldn't be guessed "
830 "from file name, using default.\n" );
834 else if( !strcasecmp( format, "avi" ) )
838 else if( !strcasecmp( format, "mp4" ) )
845 else if( !strcasecmp( format, "ogm" ) ||
846 !strcasecmp( format, "ogg" ) )
852 fprintf( stderr, "Invalid output format (%s). Possible "
853 "choices are avi, mp4 and ogm\n.", format );
859 if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
861 acodec = HB_ACODEC_FAAC;
863 else if( mux == HB_MUX_AVI )
865 acodec = HB_ACODEC_LAME;
867 else if( mux == HB_MUX_OGM )
869 acodec = HB_ACODEC_VORBIS;