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. */
17 static int debug = HB_DEBUG_NONE;
18 static int update = 0;
19 static char * input = NULL;
20 static char * output = NULL;
21 static char * format = NULL;
22 static int titleindex = 1;
23 static int twoPass = 0;
24 static int deinterlace = 0;
25 static int grayscale = 0;
26 static int vcodec = HB_VCODEC_FFMPEG;
27 static int h264_13 = 0;
28 static int h264_30 = 0;
29 static char * audios = NULL;
30 static int audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
33 static int height = 0;
34 static int crop[4] = { -1,-1,-1,-1 };
38 static float vquality = -1.0;
39 static int vbitrate = 0;
41 static int abitrate = 0;
43 static int acodec = 0;
44 static int pixelratio = 0;
45 static int chapter_start = 0;
46 static int chapter_end = 0;
47 static int chapter_markers = 0;
48 static char * marker_file = NULL;
50 static char *x264opts = NULL;
51 static char *x264opts2 = NULL;
52 static int maxHeight = 0;
53 static int maxWidth = 0;
55 /* Exit cleanly on Ctrl-C */
56 static volatile int die = 0;
57 static void SigHandler( int );
60 static void ShowCommands();
61 static void ShowHelp();
62 static int ParseOptions( int argc, char ** argv );
63 static int CheckOptions( int argc, char ** argv );
64 static int HandleEvents( hb_handle_t * h );
66 int main( int argc, char ** argv )
72 /* Parse command line */
73 if( ParseOptions( argc, argv ) ||
74 CheckOptions( argc, argv ) )
80 h = hb_init( debug, update );
83 fprintf( stderr, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
84 hb_get_version( h ), hb_get_build( h ) );
86 /* Check for update */
89 if( ( build = hb_check_update( h, &version ) ) > -1 )
91 fprintf( stderr, "You are using an old version of "
92 "HandBrake.\nLatest is %s (build %d).\n", version,
97 fprintf( stderr, "Your version of HandBrake is up to "
105 fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
106 hb_get_cpu_count( h ) > 1 ? "s" : "" );
109 fprintf( stderr, "Forcing %d CPU%s\n", cpu,
110 cpu > 1 ? "s" : "" );
111 hb_set_cpu_count( h, cpu );
114 /* Exit ASAP on Ctrl-C */
115 signal( SIGINT, SigHandler );
117 /* Feed libhb with a DVD to scan */
118 fprintf( stderr, "Opening %s...\n", input );
119 hb_scan( h, input, titleindex );
124 #if !defined(SYS_BEOS)
134 FD_SET( STDIN_FILENO, &fds );
135 ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
142 read( STDIN_FILENO, &buf[size], 1 ) > 0 )
144 if( buf[size] == '\n' )
151 if( size >= 256 || buf[size] == '\n' )
180 if( input ) free( input );
181 if( output ) free( output );
182 if( format ) free( format );
183 if( audios ) free( audios );
184 if( x264opts ) free (x264opts );
185 if( x264opts2 ) free (x264opts2 );
187 fprintf( stderr, "HandBrake has exited.\n" );
192 static void ShowCommands()
194 fprintf( stderr, "Commands:\n" );
195 fprintf( stderr, " [h]elp Show this message\n" );
196 fprintf( stderr, " [q]uit Exit HandBrakeCLI\n" );
197 fprintf( stderr, " [p]ause Pause encoding\n" );
198 fprintf( stderr, " [r]esume Resume encoding\n" );
201 static void PrintTitleInfo( hb_title_t * title )
203 hb_chapter_t * chapter;
205 hb_subtitle_t * subtitle;
208 fprintf( stderr, "+ title %d:\n", title->index );
209 fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
210 title->vts, title->ttn, title->cell_start, title->cell_end,
211 title->block_count );
212 fprintf( stderr, " + duration: %02d:%02d:%02d\n",
213 title->hours, title->minutes, title->seconds );
214 fprintf( stderr, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
215 title->width, title->height,
216 (float) title->aspect / HB_ASPECT_BASE,
217 (float) title->rate / title->rate_base );
218 fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
219 title->crop[1], title->crop[2], title->crop[3] );
220 fprintf( stderr, " + chapters:\n" );
221 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
223 chapter = hb_list_item( title->list_chapter, i );
224 fprintf( stderr, " + %d: cells %d->%d, %d blocks, duration "
225 "%02d:%02d:%02d\n", chapter->index,
226 chapter->cell_start, chapter->cell_end,
227 chapter->block_count, chapter->hours, chapter->minutes,
230 fprintf( stderr, " + audio tracks:\n" );
231 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
233 audio = hb_list_item( title->list_audio, i );
234 if( ( audio->codec & HB_ACODEC_AC3 ) || ( audio->codec & HB_ACODEC_DCA) )
236 fprintf( stderr, " + %d, %s, %dHz, %dbps\n", i + 1,
237 audio->lang, audio->rate, audio->bitrate );
241 fprintf( stderr, " + %d, %s\n", i + 1, audio->lang );
244 fprintf( stderr, " + subtitle tracks:\n" );
245 for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
247 subtitle = hb_list_item( title->list_subtitle, i );
248 fprintf( stderr, " + %d, %s\n", i + 1, subtitle->lang );
252 static int HandleEvents( hb_handle_t * h )
255 hb_get_state( h, &s );
262 #define p s.param.scanning
263 case HB_STATE_SCANNING:
264 /* Show what title is currently being scanned */
265 fprintf( stderr, "Scanning title %d", p.title_cur );
267 fprintf( stderr, " of %d", p.title_count );
268 fprintf( stderr, "...\n" );
272 case HB_STATE_SCANDONE:
278 list = hb_get_titles( h );
280 if( !hb_list_count( list ) )
282 /* No valid title, stop right there */
283 fprintf( stderr, "No title found.\n" );
289 /* Scan-only mode, print infos and exit */
291 for( i = 0; i < hb_list_count( list ); i++ )
293 title = hb_list_item( list, i );
294 PrintTitleInfo( title );
300 /* Set job settings */
301 title = hb_list_item( list, 0 );
304 PrintTitleInfo( title );
306 if( chapter_start && chapter_end )
308 job->chapter_start = MAX( job->chapter_start,
310 job->chapter_end = MIN( job->chapter_end,
312 job->chapter_end = MAX( job->chapter_start,
316 if ( chapter_markers )
318 job->chapter_markers = chapter_markers;
320 if( marker_file != NULL )
322 hb_csv_file_t * file = hb_open_csv_file( marker_file );
323 hb_csv_cell_t * cell;
327 fprintf( stderr, "Reading chapter markers from file %s\n", marker_file );
331 fprintf( stderr, "Cannot open chapter marker file, using defaults\n" );
335 /* Parse the cells */
336 while( NULL != ( cell = hb_read_next_cell( file ) ) )
338 /* We have a chapter number */
339 if( cell->cell_col == 0 )
341 row = cell->cell_row;
342 chapter = atoi( cell->cell_text );
345 /* We have a chapter name */
346 if( cell->cell_col == 1 && row == cell->cell_row )
348 /* If we have a valid chapter, copy the string an terminate it */
349 if( chapter >= job->chapter_start && chapter <= job->chapter_end )
351 hb_chapter_t * chapter_s;
353 chapter_s = hb_list_item( job->title->list_chapter, chapter - 1);
354 strncpy(chapter_s->title, cell->cell_text, 1023);
355 chapter_s->title[1023] = '\0';
360 hb_dispose_cell( cell );
363 hb_close_csv_file( file );
368 if( crop[0] >= 0 && crop[1] >= 0 &&
369 crop[2] >= 0 && crop[3] >= 0 )
371 memcpy( job->crop, crop, 4 * sizeof( int ) );
374 job->deinterlace = deinterlace;
375 job->grayscale = grayscale;
376 job->pixel_ratio = pixelratio;
378 if( width && height )
381 job->height = height;
386 hb_fix_aspect( job, HB_KEEP_WIDTH );
390 job->height = height;
391 hb_fix_aspect( job, HB_KEEP_HEIGHT );
393 else if( !width && !height && !pixelratio )
395 hb_fix_aspect( job, HB_KEEP_WIDTH );
398 if( vquality >= 0.0 && vquality <= 1.0 )
400 job->vquality = vquality;
405 job->vquality = -1.0;
406 job->vbitrate = vbitrate;
410 job->vcodec = vcodec;
414 job->h264_level = 13;
418 job->h264_level = 30;
422 job->vrate = 27000000;
423 job->vrate_base = vrate;
432 if( strcasecmp( audios, "none" ) )
438 if( *tmp < '0' || *tmp > '9' )
440 /* Skip non numeric char */
444 job->audio_mixdowns[audio_count] = audio_mixdown;
445 job->audios[audio_count++] =
446 strtol( tmp, &tmp, 0 ) - 1;
448 job->audios[audio_count] = -1;
457 /* default to the first audio track if none has been specified */
459 job->audio_mixdowns[0] = audio_mixdown;
463 job->abitrate = abitrate;
467 job->acodec = acodec;
472 job->vbitrate = hb_calc_bitrate( job, size );
473 fprintf( stderr, "Calculated bitrate: %d kbps\n",
479 job->subtitle = sub - 1;
486 job->file = strdup( output );
493 if (x264opts != NULL && *x264opts != '\0' )
495 fprintf( stderr, "Applying the following x264 options: %s\n",
497 job->x264opts = x264opts;
499 else /*avoids a bus error crash when options aren't specified*/
501 job->x264opts = NULL;
504 job->maxWidth = maxWidth;
506 job->maxHeight = maxHeight;
513 job->x264opts = x264opts2;
525 #define p s.param.working
526 case HB_STATE_WORKING:
527 fprintf( stderr, "\rEncoding: task %d of %d, %.2f %%",
528 p.job_cur, p.job_count, 100.0 * p.progress );
531 fprintf( stderr, " (%.2f fps, avg %.2f fps, ETA "
532 "%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
533 p.hours, p.minutes, p.seconds );
538 #define p s.param.muxing
539 case HB_STATE_MUXING:
541 fprintf( stderr, "\rMuxing: %.2f %%", 100.0 * p.progress );
546 #define p s.param.workdone
547 case HB_STATE_WORKDONE:
548 /* Print error if any, then exit */
552 fprintf( stderr, "\nRip done!\n" );
554 case HB_ERROR_CANCELED:
555 fprintf( stderr, "\nRip canceled.\n" );
558 fprintf( stderr, "\nRip failed (error %x).\n",
568 /****************************************************************************
570 ****************************************************************************/
571 static volatile int64_t i_die_date = 0;
572 void SigHandler( int i_signal )
577 i_die_date = hb_get_date();
578 fprintf( stderr, "Signal %d received, terminating - do it "
579 "again in case it gets stuck\n", i_signal );
581 else if( i_die_date + 500 < hb_get_date() )
583 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
588 /****************************************************************************
590 ****************************************************************************/
591 static void ShowHelp()
596 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
598 "### General Handbrake Options------------------------------------------------\n\n"
599 " -h, --help Print help\n"
600 " -u, --update Check for updates and exit\n"
601 " -v, --verbose Be verbose\n"
602 " -C, --cpu Set CPU count (default: autodetected)\n"
605 "### Source Options-----------------------------------------------------------\n\n"
606 " -i, --input <string> Set input device\n"
607 " -t, --title <number> Select a title to encode (0 to scan only,\n"
609 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
610 " 1 to 3, or \"3\" for chapter 3 only,\n"
611 " default: all chapters)\n"
614 "### Destination Options------------------------------------------------------\n\n"
615 " -o, --output <string> Set output file name\n"
616 " -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
617 " autodetected from file name)\n"
620 "### Picture Settings---------------------------------------------------------\n\n"
621 " -w, --width <number> Set picture width\n"
622 " -l, --height <number> Set picture height\n"
623 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
624 " -Y, --maxHeight <#> Set maximum height\n"
625 " -X, --maxWidth <#> Set maximum width\n"
626 " -s, --subtitle <number> Select subtitle (default: none)\n"
627 " -m, --markers Add chapter markers (mp4 output format only)\n"
630 "### Video Options------------------------------------------------------------\n\n"
631 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
632 " x264,x264b13,x264b30 default: ffmpeg)\n"
633 " -q, --quality <float> Set video quality (0.0..1.0)\n"
634 " -Q, --crf Use with -q for CRF instead of CQP\n"
635 " -S, --size <MB> Set target size\n"
636 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
637 " -r, --rate Set video framerate (" );
638 for( i = 0; i < hb_video_rates_count; i++ )
640 fprintf( stderr, hb_video_rates[i].string );
641 if( i != hb_video_rates_count - 1 )
642 fprintf( stderr, "/" );
644 fprintf( stderr, ")\n"
646 " -2, --two-pass Use two-pass mode\n"
647 " -d, --deinterlace Deinterlace video\n"
648 " -g, --grayscale Grayscale encoding\n"
649 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
654 "### Audio Options-----------------------------------------------------------\n\n"
655 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
656 " meaning passthrough, default: guessed)\n"
657 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
658 " -a, --audio <string> Select audio channel(s) (\"none\" for no \n"
659 " audio, default: first one)\n"
660 " -6, --mixdown <string> Format for surround sound downmixing\n"
661 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
662 " -R, --arate Set audio samplerate (" );
663 for( i = 0; i < hb_audio_rates_count; i++ )
665 fprintf( stderr, hb_audio_rates[i].string );
666 if( i != hb_audio_rates_count - 1 )
667 fprintf( stderr, "/" );
669 fprintf( stderr, " kHz)\n"
676 "### Advanced H264 Options----------------------------------------------------\n\n"
677 " -x, --x264opts <string> Specify advanced x264 options in the\n"
678 " same style as mencoder:\n"
679 " option1=value1:option2=value2\n" );
682 /****************************************************************************
684 ****************************************************************************/
685 static int ParseOptions( int argc, char ** argv )
689 static struct option long_options[] =
691 { "help", no_argument, NULL, 'h' },
692 { "update", no_argument, NULL, 'u' },
693 { "verbose", no_argument, NULL, 'v' },
694 { "cpu", required_argument, NULL, 'C' },
696 { "format", required_argument, NULL, 'f' },
697 { "input", required_argument, NULL, 'i' },
698 { "output", required_argument, NULL, 'o' },
700 { "title", required_argument, NULL, 't' },
701 { "chapters", required_argument, NULL, 'c' },
702 { "markers", optional_argument, NULL, 'm' },
703 { "audio", required_argument, NULL, 'a' },
704 { "mixdown", required_argument, NULL, '6' },
705 { "subtitle", required_argument, NULL, 's' },
707 { "encoder", required_argument, NULL, 'e' },
708 { "aencoder", required_argument, NULL, 'E' },
709 { "two-pass", no_argument, NULL, '2' },
710 { "deinterlace", no_argument, NULL, 'd' },
711 { "grayscale", no_argument, NULL, 'g' },
712 { "pixelratio", no_argument, NULL, 'p' },
713 { "width", required_argument, NULL, 'w' },
714 { "height", required_argument, NULL, 'l' },
715 { "crop", required_argument, NULL, 'n' },
717 { "vb", required_argument, NULL, 'b' },
718 { "quality", required_argument, NULL, 'q' },
719 { "size", required_argument, NULL, 'S' },
720 { "ab", required_argument, NULL, 'B' },
721 { "rate", required_argument, NULL, 'r' },
722 { "arate", required_argument, NULL, 'R' },
723 { "crf", no_argument, NULL, 'Q' },
724 { "x264opts", required_argument, NULL, 'x' },
725 { "maxHeight", required_argument, NULL, 'Y' },
726 { "maxWidth", required_argument, NULL, 'X' },
731 int option_index = 0;
734 c = getopt_long( argc, argv,
735 "hvuC:f:i:o:t:c:ma:6:s:e:E:2dgpw:l:n:b:q:S:B:r:R:Qx:Y:X:",
736 long_options, &option_index );
751 debug = HB_DEBUG_ALL;
754 cpu = atoi( optarg );
758 format = strdup( optarg );
761 input = strdup( optarg );
764 output = strdup( optarg );
768 titleindex = atoi( optarg );
773 if( sscanf( optarg, "%d-%d", &start, &end ) == 2 )
775 chapter_start = start;
778 else if( sscanf( optarg, "%d", &start ) == 1 )
780 chapter_start = start;
781 chapter_end = chapter_start;
785 fprintf( stderr, "chapters: invalid syntax (%s)\n",
794 marker_file = strdup( optarg );
799 audios = strdup( optarg );
802 if( !strcasecmp( optarg, "mono" ) )
804 audio_mixdown = HB_AMIXDOWN_MONO;
806 else if( !strcasecmp( optarg, "stereo" ) )
808 audio_mixdown = HB_AMIXDOWN_STEREO;
810 else if( !strcasecmp( optarg, "dpl1" ) )
812 audio_mixdown = HB_AMIXDOWN_DOLBY;
814 else if( !strcasecmp( optarg, "dpl2" ) )
816 audio_mixdown = HB_AMIXDOWN_DOLBYPLII;
818 else if( !strcasecmp( optarg, "6ch" ) )
820 audio_mixdown = HB_AMIXDOWN_6CH;
824 sub = atoi( optarg );
840 if( !strcasecmp( optarg, "ffmpeg" ) )
842 vcodec = HB_VCODEC_FFMPEG;
844 else if( !strcasecmp( optarg, "xvid" ) )
846 vcodec = HB_VCODEC_XVID;
848 else if( !strcasecmp( optarg, "x264" ) )
850 vcodec = HB_VCODEC_X264;
852 else if( !strcasecmp( optarg, "x264b13" ) )
854 vcodec = HB_VCODEC_X264;
857 else if( !strcasecmp( optarg, "x264b30" ) )
859 vcodec = HB_VCODEC_X264;
864 fprintf( stderr, "invalid codec (%s)\n", optarg );
869 if( !strcasecmp( optarg, "ac3" ) )
871 acodec = HB_ACODEC_AC3;
873 else if( !strcasecmp( optarg, "lame" ) )
875 acodec = HB_ACODEC_LAME;
879 width = atoi( optarg );
882 height = atoi( optarg );
888 for( i = 0; i < 4; i++ )
892 crop[i] = strtol( tmp, &tmp, 0 );
901 for( i = 0; i < hb_video_rates_count; i++ )
903 if( !strcmp( optarg, hb_video_rates[i].string ) )
905 vrate = hb_video_rates[i].rate;
911 fprintf( stderr, "invalid framerate %s\n", optarg );
919 for( i = 0; i < hb_audio_rates_count; i++ )
921 if( !strcmp( optarg, hb_audio_rates[i].string ) )
923 arate = hb_audio_rates[i].rate;
929 fprintf( stderr, "invalid framerate %s\n", optarg );
934 vbitrate = atoi( optarg );
937 vquality = atof( optarg );
940 size = atoi( optarg );
943 abitrate = atoi( optarg );
949 x264opts = strdup( optarg );
950 x264opts2 = strdup( optarg );
953 maxHeight = atoi( optarg );
956 maxWidth = atoi (optarg );
960 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
968 static int CheckOptions( int argc, char ** argv )
975 if( input == NULL || *input == '\0' )
977 fprintf( stderr, "Missing input device. Run %s --help for "
978 "syntax.\n", argv[0] );
985 if( output == NULL || *output == '\0' )
987 fprintf( stderr, "Missing output file name. Run %s --help "
988 "for syntax.\n", argv[0] );
994 char * p = strrchr( output, '.' );
997 if( p && !strcasecmp( p, ".avi" ) )
1001 else if( p && ( !strcasecmp( p, ".mp4" ) ||
1002 !strcasecmp( p, ".m4v" ) ) )
1009 else if( p && ( !strcasecmp( p, ".ogm" ) ||
1010 !strcasecmp( p, ".ogg" ) ) )
1016 fprintf( stderr, "Output format couldn't be guessed "
1017 "from file name, using default.\n" );
1021 else if( !strcasecmp( format, "avi" ) )
1025 else if( !strcasecmp( format, "mp4" ) )
1032 else if( !strcasecmp( format, "ogm" ) ||
1033 !strcasecmp( format, "ogg" ) )
1039 fprintf( stderr, "Invalid output format (%s). Possible "
1040 "choices are avi, mp4 and ogm\n.", format );
1046 if( mux == HB_MUX_MP4 || mux == HB_MUX_IPOD )
1048 acodec = HB_ACODEC_FAAC;
1050 else if( mux == HB_MUX_AVI )
1052 acodec = HB_ACODEC_LAME;
1054 else if( mux == HB_MUX_OGM )
1056 acodec = HB_ACODEC_VORBIS;