1 /* $Id: test.c,v 1.26 2004/01/22 19:36:55 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. */
10 #include "HandBrake.h"
13 static int verbose = 0;
14 static char * input = NULL;
15 static char * output = NULL;
16 static char * format = NULL;
17 static int titleindex = 1;
18 static int twoPass = 0;
19 static int deinterlace = 0;
20 static int vcodec = HB_CODEC_FFMPEG;
21 static char * audios = NULL;
24 static int bottom = 0;
27 static int autocrop = 0;
29 static int vbitrate = 1024;
31 static int abitrate = 128;
33 static int acodec = 0;
35 /* Exit cleanly on Ctrl-C */
36 static volatile int die = 0;
37 static void SigHandler( int );
40 static void ShowHelp();
41 static int ParseOptions( int argc, char ** argv );
42 static int CheckOptions( int argc, char ** argv );
45 static void Scanning( void * data, int title, int titleCount );
46 static void ScanDone( void * data, HBList * titleList );
47 static void Encoding( void * data, float position, int pass,
48 int passCount, float frameRate,
49 float avgFrameRate, int remainingTime );
50 static void RipDone( void * data, int result );
52 int main( int argc, char ** argv )
55 HBCallbacks callbacks;
57 fprintf( stderr, "HandBrake " HB_VERSION
58 " - http://handbrake.m0k.org/\n" );
60 if( ParseOptions( argc, argv ) ||
61 CheckOptions( argc, argv ) )
66 /* Exit ASAP on Ctrl-C */
67 signal( SIGINT, SigHandler );
70 h = HBInit( verbose, cpu );
72 /* Set libhb callbacks */
74 callbacks.scanning = Scanning;
75 callbacks.scanDone = ScanDone;
76 callbacks.encoding = Encoding;
77 callbacks.ripDone = RipDone;
78 HBSetCallbacks( h, callbacks );
80 /* Feed libhb with a DVD to scan */
81 fprintf( stderr, "Opening %s...\n", input );
82 HBScanDVD( h, input, titleindex );
93 fprintf( stderr, "HandBrake has exited cleanly.\n" );
98 /****************************************************************************
100 ****************************************************************************/
101 static volatile int64_t i_die_date = 0;
102 void SigHandler( int i_signal )
106 i_die_date = HBGetDate();
107 fprintf( stderr, "Signal %d received, terminating - do it "
108 "again in case it gets stuck\n", i_signal );
110 else if( i_die_date + 500000 < HBGetDate() )
112 fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
118 /****************************************************************************
120 ****************************************************************************/
121 static void ShowHelp()
124 "Syntax: HBTest [options] -i <device> -o <file>\n"
126 " -h, --help Print help\n"
127 " -v, --verbose Be verbose\n"
128 " -C, --cpu Set CPU count (default: autodetected)\n"
130 " -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
132 " -i, --input <string> Set input device\n"
133 " -o, --output <string> Set output file name\n"
135 " --scan Only scan the device\n"
136 " -t, --title <number> Select a title to encode (0 to scan only,\n"
138 " -a, --audio <string> Select audio channel(s) (none for no audio,\n"
139 " default: first one)\n"
140 " --noaudio Disable audio\n"
142 " -c, --codec <string> Set video library encoder (ffmpeg/xvid/x264,\n"
143 " default: ffmpeg)\n"
144 " -2, --two-pass Use two-pass mode\n"
145 " -d, --deinterlace Deinterlace video\n"
147 " -b, --vb <kb/s> Set video bitrate (default: 1024)\n"
148 " -s, --size <MB> Set target size instead of bitrate\n"
149 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
150 " -w, --width <number> Set picture width\n"
151 " --crop <T:B:L:R> Set cropping values\n"
152 " --autocrop Use autodetected cropping values\n" );
156 /****************************************************************************
158 ****************************************************************************/
159 static int ParseOptions( int argc, char ** argv )
163 static struct option long_options[] =
165 { "help", no_argument, NULL, 'h' },
166 { "verbose", no_argument, NULL, 'v' },
167 { "cpu", required_argument, NULL, 'C' },
169 { "format", required_argument, NULL, 'f' },
170 { "input", required_argument, NULL, 'i' },
171 { "output", required_argument, NULL, 'o' },
173 { "scan", no_argument, NULL, 'S' },
174 { "title", required_argument, NULL, 't' },
175 { "audio", required_argument, NULL, 'a' },
176 { "noaudio", no_argument, NULL, 'a' },
178 { "codec", required_argument, NULL, 'c' },
179 { "two-pass", no_argument, NULL, '2' },
180 { "deinterlace", no_argument, NULL, 'd' },
181 { "width", required_argument, NULL, 'w' },
182 { "crop", required_argument, NULL, 'n' },
183 { "autocrop", no_argument, NULL, 'z' },
185 { "vb", required_argument, NULL, 'b' },
186 { "size", required_argument, NULL, 's' },
187 { "ab", required_argument, NULL, 'B' },
192 int option_index = 0;
195 c = getopt_long( argc, argv, "hvC:f:i:o:St:a:c:2dw:n:zb:s:B:",
196 long_options, &option_index );
211 cpu = atoi( optarg );
215 format = strdup( optarg );
218 input = strdup( optarg );
221 output = strdup( optarg );
228 titleindex = atoi( optarg );
231 audios = strdup( optarg ? optarg : "none" );
241 if( !strcasecmp( optarg, "ffmpeg" ) )
243 vcodec = HB_CODEC_FFMPEG;
245 else if( !strcasecmp( optarg, "xvid" ) )
247 vcodec = HB_CODEC_XVID;
249 else if( !strcasecmp( optarg, "x264" ) )
251 vcodec = HB_CODEC_X264;
255 fprintf( stderr, "invalid codec (%s)\n", optarg );
260 width = atoi( optarg );
264 char * crop = strdup( optarg );
269 top = strtol( crop, &crop, 0 ); crop++;
273 bottom = strtol( crop, &crop, 0 ); crop++;
277 left = strtol( crop, &crop, 0 ); crop++;
281 right = strtol( crop, &crop, 0 ); crop++;
292 vbitrate = atoi( optarg );
295 size = atoi( optarg );
298 abitrate = atoi( optarg );
302 fprintf( stderr, "unknown option (%s)\n", argv[optind] );
310 static int CheckOptions( int argc, char ** argv )
312 if( input == NULL || *input == '\0' )
314 fprintf( stderr, "Missing input device. Run %s --help for "
315 "syntax.\n", argv[0] );
322 if( output == NULL || *output == '\0' )
324 fprintf( stderr, "Missing output file name. Run %s --help "
325 "for syntax.\n", argv[0] );
331 char *p = strrchr( output, '.' );
333 if( p && !strcasecmp( p, ".avi" ) )
337 else if( p && !strcasecmp( p, ".mp4" ) )
341 else if( p && ( !strcasecmp( p, ".ogm" ) ||
342 !strcasecmp( p, ".ogg" ) ) )
349 fprintf( stderr, "Output format couldn't be guessed "
350 "from file name, please use --format.\n" );
354 else if( !strcasecmp( format, "avi" ) )
358 else if( !strcasecmp( format, "mp4" ) )
362 else if( !strcasecmp( format, "ogm" ) ||
363 !strcasecmp( format, "ogg" ) )
369 fprintf( stderr, "Invalid output format (%s). Possible "
370 "choices are avi, mp4 and ogm\n.", format );
373 if( mux == HB_MUX_MP4 )
375 acodec = HB_CODEC_AAC;
377 else if( mux == HB_MUX_AVI )
379 acodec = HB_CODEC_MP3;
381 else if( mux == HB_MUX_OGM )
383 acodec = HB_CODEC_VORBIS;
390 static void Scanning( void * data, int title, int titleCount )
394 fprintf( stderr, "Scanning title %d...\n", title );
398 fprintf( stderr, "Scanning title %d/%d...\n", title, titleCount );
402 static void ScanDone( void * data, HBList * titleList )
404 HBHandle * h = (HBHandle*) data;
410 fprintf( stderr, "No title found. Invalid device?\n" );
420 title = HBListItemAt( titleList, 0 );
421 title->file = strdup( output );
422 title->twoPass = twoPass;
423 title->deinterlace = deinterlace;
426 title->outWidth = width;
430 title->topCrop = title->autoTopCrop;
431 title->bottomCrop = title->autoBottomCrop;
432 title->leftCrop = title->autoLeftCrop;
433 title->rightCrop = title->autoRightCrop;
437 title->topCrop = top;
438 title->bottomCrop = bottom;
439 title->leftCrop = left;
440 title->rightCrop = right;
442 fprintf( stderr, "Cropping: T=%d,B=%d,L=%d,R=%d\n",
443 title->topCrop, title->bottomCrop,
444 title->leftCrop, title->rightCrop );
445 title->bitrate = vbitrate;
446 title->codec = vcodec;
451 audio = HBListItemAt( title->audioList, 0 );
452 audio->outBitrate = abitrate;
453 audio->codec = acodec;
454 HBListAdd( title->ripAudioList, audio );
456 else if( strcasecmp( audios, "none" ) )
464 if( *tmp < '0' || *tmp > '9' )
466 /* Skip non numeric char */
471 i = strtol( tmp, &tmp, 0 );
472 audio = HBListItemAt( title->audioList, i - 1 );
473 audio->outBitrate = abitrate;
474 audio->codec = acodec;
475 HBListAdd( title->ripAudioList, audio );
482 title->bitrate = HBGetBitrateForSize( title, size, title->mux,
483 HBListCount( title->ripAudioList ), abitrate );
484 fprintf( stderr, "Calculated bitrate: %d kbps\n", title->bitrate );
487 HBStartRip( h, title );
490 static void Encoding( void * data, float position, int pass,
491 int passCount, float frameRate,
492 float avgFrameRate, int remainingTime )
494 fprintf( stderr, "%6.2f %% (pass: %d/%d, cur/avg speed: "
495 "%5.2f/%5.2f fps, %02d:%02d:%02d remaining)\n",
496 100.0 * position, pass, passCount, frameRate, avgFrameRate,
497 remainingTime / 3600, ( remainingTime / 60 ) % 60,
498 remainingTime % 60 );
501 static void RipDone( void * data, int result )
506 fprintf( stderr, "Rip done!\n" );
509 fprintf( stderr, "Rip canceled.\n" );
512 fprintf( stderr, "Rip failed (error %x).\n", result );