summaryrefslogtreecommitdiffstats
path: root/test/test.c
blob: 67b9450a9ef45476895ea1a0aad43f68ad7cac44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* $Id: test.c,v 1.28 2004/03/22 19:18:57 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.m0k.org/>.
   It may be used under the terms of the GNU General Public License. */

#include <signal.h>
#include <getopt.h>

#include "HandBrake.h"

/* Options */
static int    verbose     = 0;
static char * input       = NULL;
static char * output      = NULL;
static char * format      = NULL;
static int    titleindex  = 1;
static int    twoPass     = 0;
static int    deinterlace = 0;
static int    vcodec      = HB_CODEC_FFMPEG;
static char * audios      = NULL;
static int    width       = 0;
static int    top         = 0;
static int    bottom      = 0;
static int    left        = 0;
static int    right       = 0;
static int    autocrop    = 0;
static int    cpu         = 0;
static int    vbitrate    = 1024;
static int    size        = 0;
static int    abitrate    = 128;
static int    mux         = 0;
static int    acodec      = 0;

/* Exit cleanly on Ctrl-C */
static volatile int die = 0;
static void SigHandler( int );

/* Utils */
static void ShowHelp();
static int  ParseOptions( int argc, char ** argv );
static int  CheckOptions( int argc, char ** argv );

/* libhb callbacks */
static void Scanning( void * data, int title, int titleCount );
static void ScanDone( void * data, HBList * titleList );
static void Encoding( void * data, float position, int pass,
                      int passCount, float frameRate,
                      float avgFrameRate, int remainingTime );
static void RipDone( void * data, int result );

int main( int argc, char ** argv )
{
    HBHandle    * h;
    HBCallbacks   callbacks;

    fprintf( stderr, "HandBrake " HB_VERSION
             " - http://handbrake.m0k.org/\n" );

    if( ParseOptions( argc, argv ) ||
        CheckOptions( argc, argv ) )
    {
        return 1;
    }

    /* Exit ASAP on Ctrl-C */
    signal( SIGINT, SigHandler );

    /* Init libhb */
    h = HBInit( verbose, cpu );

    /* Set libhb callbacks */
    callbacks.data     = h;
    callbacks.scanning = Scanning;
    callbacks.scanDone = ScanDone;
    callbacks.encoding = Encoding;
    callbacks.ripDone  = RipDone;
    HBSetCallbacks( h, callbacks );

    /* Feed libhb with a DVD to scan */
    fprintf( stderr, "Opening %s...\n", input );
    HBScanDVD( h, input, titleindex );

    /* Wait... */
    while( !die )
    {
        HBSnooze( 500000 );
    }

    /* Clean up */
    HBClose( &h );

    fprintf( stderr, "HandBrake has exited cleanly.\n" );

    return 0;
}

/****************************************************************************
 * SigHandler:
 ****************************************************************************/
static volatile int64_t i_die_date = 0;
void SigHandler( int i_signal )
{
    if( die == 0 )
    {
        i_die_date = HBGetDate();
        fprintf( stderr, "Signal %d received, terminating - do it "
                 "again in case it gets stuck\n", i_signal );
    }
    else if( i_die_date + 500000 < HBGetDate() )
    {
        fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
        exit( 1 );
    }
    die = 1;
}

/****************************************************************************
 * ShowHelp:
 ****************************************************************************/
static void ShowHelp()
{
    fprintf( stderr,
    "Syntax: HBTest [options] -i <device> -o <file>\n"
    "\n"
    "    -h, --help            Print help\n"
    "    -v, --verbose         Be verbose\n"
    "    -C, --cpu             Set CPU count (default: autodetected)\n"
    "\n"
    "    -f, --format <string> Set output format (avi/mp4/ogm, default:\n"
    "                          autodetected)\n"
    "    -i, --input <string>  Set input device\n"
    "    -o, --output <string> Set output file name\n"
    "\n"
    "        --scan            Only scan the device\n"
    "    -t, --title <number>  Select a title to encode (0 to scan only,\n"
    "                          default: 1)\n"
    "    -a, --audio <string>  Select audio channel(s) (none for no audio,\n"
    "                          default: first one)\n"
    "        --noaudio         Disable audio\n"
    "\n"
    "    -c, --codec <string>  Set video library encoder (ffmpeg/xvid/x264,\n"
    "                          default: ffmpeg)\n"
    "    -2, --two-pass        Use two-pass mode\n"
    "    -d, --deinterlace     Deinterlace video\n"
    "\n"
    "    -b, --vb <kb/s>       Set video bitrate (default: 1024)\n"
    "    -s, --size <MB>       Set target size instead of bitrate\n"
    "    -B, --ab <kb/s>       Set audio bitrate (default: 128)\n"
    "    -w, --width <number>  Set picture width\n"
    "        --crop <T:B:L:R>  Set cropping values\n"
    "        --autocrop        Use autodetected cropping values\n" );
}


/****************************************************************************
 * ParseOptions:
 ****************************************************************************/
static int ParseOptions( int argc, char ** argv )
{
    for( ;; )
    {
        static struct option long_options[] =
          {
            { "help",        no_argument,       NULL,    'h' },
            { "verbose",     no_argument,       NULL,    'v' },
            { "cpu",         required_argument, NULL,    'C' },

            { "format",      required_argument, NULL,    'f' },
            { "input",       required_argument, NULL,    'i' },
            { "output",      required_argument, NULL,    'o' },

            { "scan",        no_argument,       NULL,    'S' },
            { "title",       required_argument, NULL,    't' },
            { "audio",       required_argument, NULL,    'a' },
            { "noaudio",     no_argument,       NULL,    'a' },

            { "codec",       required_argument, NULL,    'c' },
            { "two-pass",    no_argument,       NULL,    '2' },
            { "deinterlace", no_argument,       NULL,    'd' },
            { "width",       required_argument, NULL,    'w' },
            { "crop",        required_argument, NULL,    'n' },
            { "autocrop",    no_argument,       NULL,    'z' },

            { "vb",          required_argument, NULL,    'b' },
            { "size",        required_argument, NULL,    's' },
            { "ab",          required_argument, NULL,    'B' },

            { 0, 0, 0, 0 }
          };

        int option_index = 0;
        int c;

        c = getopt_long( argc, argv, "hvC:f:i:o:St:a:c:2dw:n:zb:s:B:",
                         long_options, &option_index );
        if( c < 0 )
        {
            break;
        }

        switch( c )
        {
            case 'h':
                ShowHelp();
                exit( 0 );
            case 'v':
                verbose = 1;
                break;
            case 'C':
                cpu = atoi( optarg );
                break;

            case 'f':
                format = strdup( optarg );
                break;
            case 'i':
                input = strdup( optarg );
                break;
            case 'o':
                output = strdup( optarg );
                break;

            case 'S':
                titleindex = 0;
                break;
            case 't':
                titleindex = atoi( optarg );
                break;
            case 'a':
                audios = strdup( optarg ? optarg : "none" );
                break;

            case '2':
                twoPass = 1;
                break;
            case 'd':
                deinterlace = 1;
                break;
            case 'c':
                if( !strcasecmp( optarg, "ffmpeg" ) )
                {
                    vcodec = HB_CODEC_FFMPEG;
                }
                else if( !strcasecmp( optarg, "xvid" ) )
                {
                    vcodec = HB_CODEC_XVID;
                }
                else if( !strcasecmp( optarg, "x264" ) )
                {
                    vcodec = HB_CODEC_X264;
                }
                else
                {
                    fprintf( stderr, "invalid codec (%s)\n", optarg );
                    return -1;
                }
                break;
            case 'w':
                width = atoi( optarg );
                break;
            case 'n':
            {
                char * crop = strdup( optarg );
                char * _2be3 = crop;

                if( *crop )
                {
                    top = strtol( crop, &crop, 0 ); crop++;
                }
                if( *crop )
                {
                    bottom = strtol( crop, &crop, 0 ); crop++;
                }
                if( *crop )
                {
                    left = strtol( crop, &crop, 0 ); crop++;
                }
                if( *crop )
                {
                    right = strtol( crop, &crop, 0 ); crop++;
                }

                free( _2be3 );
                break;
            }
            case 'z':
               autocrop = 1;
               break;

            case 'b':
                vbitrate = atoi( optarg );
                break;
            case 's':
                size = atoi( optarg );
                break;
            case 'B':
                abitrate = atoi( optarg );
                break;

            default:
                fprintf( stderr, "unknown option (%s)\n", argv[optind] );
                return -1;
        }
    }

    return 0;
}

static int CheckOptions( int argc, char ** argv )
{
    if( input == NULL || *input == '\0' )
    {
        fprintf( stderr, "Missing input device. Run %s --help for "
                 "syntax.\n", argv[0] );
        return 1;
    }

    /* Parse format */
    if( titleindex > 0 )
    {
        if( output == NULL || *output == '\0' )
        {
            fprintf( stderr, "Missing output file name. Run %s --help "
                     "for syntax.\n", argv[0] );
            return 1;
        }

        if( !format )
        {
            char *p = strrchr( output, '.' );
            /* autodetect */
            if( p && !strcasecmp( p, ".avi" ) )
            {
                mux = HB_MUX_AVI;
            }
            else if( p && !strcasecmp( p, ".mp4" ) )
            {
                mux = HB_MUX_MP4;
            }
            else if( p && ( !strcasecmp( p, ".ogm" ) ||
                            !strcasecmp( p, ".ogg" ) ) )
            {
                mux = HB_MUX_OGM;
            }

            else
            {
                fprintf( stderr, "Output format couldn't be guessed "
                         "from file name, please use --format.\n" );
                return 1;
            }
        }
        else if( !strcasecmp( format, "avi" ) )
        {
            mux = HB_MUX_AVI;
        }
        else if( !strcasecmp( format, "mp4" ) )
        {
            mux = HB_MUX_MP4;
        }
        else if( !strcasecmp( format, "ogm" ) ||
                 !strcasecmp( format, "ogg" ) )
        {
            mux = HB_MUX_OGM;
        }
        else
        {
            fprintf( stderr, "Invalid output format (%s). Possible "
                     "choices are avi, mp4 and ogm\n.", format );
            return 1;
        }
        if( mux == HB_MUX_MP4 )
        {
            acodec = HB_CODEC_AAC;
        }
        else if( mux == HB_MUX_AVI )
        {
            acodec = HB_CODEC_MP3;
        }
        else if( mux == HB_MUX_OGM )
        {
            acodec = HB_CODEC_VORBIS;
        }
    }

    return 0;
}

static void Scanning( void * data, int title, int titleCount )
{
    if( titleindex )
    {
        fprintf( stderr, "Scanning title %d...\n", title );
    }
    else
    {
        fprintf( stderr, "Scanning title %d/%d...\n", title, titleCount );
    }
}

static void ScanDone( void * data, HBList * titleList )
{
    HBHandle * h = (HBHandle*) data;
    HBAudio  * audio;
    HBTitle  * title;

    if( !titleList )
    {
        fprintf( stderr, "No title found. Invalid device?\n" );
        die = 1;
        return;
    }
    if( !titleindex )
    {
        die = 1;
        return;
    }

    title = HBListItemAt( titleList, 0 );
    title->file = strdup( output );
    title->twoPass = twoPass;
    title->deinterlace = deinterlace;
    if( width )
    {
        title->outWidth = width;
    }
    if( autocrop )
    {
        title->topCrop    = title->autoTopCrop;
        title->bottomCrop = title->autoBottomCrop;
        title->leftCrop   = title->autoLeftCrop;
        title->rightCrop  = title->autoRightCrop;
    }
    else
    {
        title->topCrop    = top;
        title->bottomCrop = bottom;
        title->leftCrop   = left;
        title->rightCrop  = right;
    }
    fprintf( stderr, "Cropping: T=%d,B=%d,L=%d,R=%d\n",
             title->topCrop, title->bottomCrop,
             title->leftCrop, title->rightCrop );
    title->bitrate = vbitrate;
    title->codec = vcodec;
    title->mux = mux;

    if( audios == NULL )
    {
        audio = HBListItemAt( title->audioList, 0 );
        audio->outBitrate = abitrate;
        audio->outCodec = acodec;
        HBListAdd( title->ripAudioList, audio );
    }
    else if( strcasecmp( audios, "none" ) )
    {
        char *tmp = audios;

        while( *tmp )
        {
            int i;

            if( *tmp < '0' || *tmp > '9' )
            {
                /* Skip non numeric char */
                tmp++;
                continue;
            }

            i = strtol( tmp, &tmp, 0 );
            audio = HBListItemAt( title->audioList, i - 1 );
            audio->outBitrate = abitrate;
            audio->outCodec = acodec;
            HBListAdd( title->ripAudioList, audio );
        }
    }
    if( size )
    {
        title->bitrate = HBGetBitrateForSize( title, size, title->mux,
                HBListCount( title->ripAudioList ), abitrate );
        fprintf( stderr, "Calculated bitrate: %d kbps\n", title->bitrate );
    }

    HBStartRip( h, title );
}

static void Encoding( void * data, float position, int pass,
                      int passCount, float frameRate,
                      float avgFrameRate, int remainingTime )
{
    fprintf( stderr, "%6.2f %% (pass: %d/%d, cur/avg speed: "
             "%5.2f/%5.2f fps, %02d:%02d:%02d remaining)\n",
             100.0 * position, pass, passCount, frameRate, avgFrameRate,
             remainingTime / 3600, ( remainingTime / 60 ) % 60,
             remainingTime % 60 );
}

static void RipDone( void * data, int result )
{
    switch( result )
    {
        case HB_SUCCESS:
            fprintf( stderr, "Rip done!\n" );
            break;
        case HB_CANCELED:
            fprintf( stderr, "Rip canceled.\n" );
            break;
        default:
            fprintf( stderr, "Rip failed (error %x).\n", result );
    }
    die = 1;
}