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 longest_title
= 0;
24 static int subtitle_scan
= 0;
25 static int subtitle_force
= 0;
26 static char * native_language
= NULL
;
27 static int twoPass
= 0;
28 static int deinterlace
= 0;
29 static char * deinterlace_opt
= 0;
30 static int deblock
= 0;
31 static char * deblock_opt
= 0;
32 static int denoise
= 0;
33 static char * denoise_opt
= 0;
34 static int detelecine
= 0;
35 static char * detelecine_opt
= 0;
36 static int grayscale
= 0;
37 static int vcodec
= HB_VCODEC_FFMPEG
;
38 static int h264_13
= 0;
39 static int h264_30
= 0;
40 static char * audios
= NULL
;
41 static int audio_mixdown
= HB_AMIXDOWN_DOLBYPLII
;
44 static int height
= 0;
45 static int crop
[4] = { -1,-1,-1,-1 };
49 static float vquality
= -1.0;
50 static int vbitrate
= 0;
52 static int abitrate
= 0;
54 static int acodec
= 0;
55 static int pixelratio
= 0;
56 static int loosePixelratio
= 0;
57 static int modulus
= 0;
58 static int chapter_start
= 0;
59 static int chapter_end
= 0;
60 static int chapter_markers
= 0;
61 static char * marker_file
= NULL
;
63 static char *x264opts
= NULL
;
64 static char *x264opts2
= NULL
;
65 static int maxHeight
= 0;
66 static int maxWidth
= 0;
67 static int turbo_opts_enabled
= 0;
68 static char * turbo_opts
= "ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
69 static int largeFileSize
= 0;
70 static int preset
= 0;
71 static char * preset_name
= 0;
74 /* Exit cleanly on Ctrl-C */
75 static volatile int die
= 0;
76 static void SigHandler( int );
79 static void ShowCommands();
80 static void ShowHelp();
81 static void ShowPresets();
83 static int ParseOptions( int argc
, char ** argv
);
84 static int CheckOptions( int argc
, char ** argv
);
85 static int HandleEvents( hb_handle_t
* h
);
87 /****************************************************************************
90 * When using the CLI just display using hb_log as we always did in the past
91 * make sure that we prefix with a nice ERROR message to catch peoples eyes.
92 ****************************************************************************/
93 static void hb_cli_error_handler ( const char *errmsg
)
95 fprintf( stderr
, "ERROR: %s", errmsg
);
98 int main( int argc
, char ** argv
)
104 /* Parse command line */
105 if( ParseOptions( argc
, argv
) ||
106 CheckOptions( argc
, argv
) )
111 /* Register our error handler */
112 hb_register_error_handler(&hb_cli_error_handler
);
115 h
= hb_init( debug
, update
);
118 fprintf( stderr
, "HandBrake %s (%d) - http://handbrake.m0k.org/\n",
119 hb_get_version( h
), hb_get_build( h
) );
121 /* Check for update */
124 if( ( build
= hb_check_update( h
, &version
) ) > -1 )
126 fprintf( stderr
, "You are using an old version of "
127 "HandBrake.\nLatest is %s (build %d).\n", version
,
132 fprintf( stderr
, "Your version of HandBrake is up to "
140 fprintf( stderr
, "%d CPU%s detected\n", hb_get_cpu_count(),
141 hb_get_cpu_count( h
) > 1 ? "s" : "" );
144 fprintf( stderr
, "Forcing %d CPU%s\n", cpu
,
145 cpu
> 1 ? "s" : "" );
146 hb_set_cpu_count( h
, cpu
);
149 /* Exit ASAP on Ctrl-C */
150 signal( SIGINT
, SigHandler
);
152 /* Feed libhb with a DVD to scan */
153 fprintf( stderr
, "Opening %s...\n", input
);
157 * We need to scan for all the titles in order to find the longest
161 hb_scan( h
, input
, titleindex
);
166 #if !defined(SYS_BEOS)
176 FD_SET( STDIN_FILENO
, &fds
);
177 ret
= select( STDIN_FILENO
+ 1, &fds
, NULL
, NULL
, &tv
);
184 read( STDIN_FILENO
, &buf
[size
], 1 ) > 0 )
186 if( buf
[size
] == '\n' )
193 if( size
>= 256 || buf
[size
] == '\n' )
198 fprintf( stdout
, "\nEncoding Quit by user command\n" );
202 fprintf( stdout
, "\nEncoding Paused by user command, 'r' to resume\n" );
224 if( input
) free( input
);
225 if( output
) free( output
);
226 if( format
) free( format
);
227 if( audios
) free( audios
);
228 if (native_language
) free (native_language
);
229 if( x264opts
) free (x264opts
);
230 if( x264opts2
) free (x264opts2
);
231 if (preset_name
) free (preset_name
);
233 fprintf( stderr
, "HandBrake has exited.\n" );
238 static void ShowCommands()
240 fprintf( stdout
, "\nCommands:\n" );
241 fprintf( stdout
, " [h]elp Show this message\n" );
242 fprintf( stdout
, " [q]uit Exit HandBrakeCLI\n" );
243 fprintf( stdout
, " [p]ause Pause encoding\n" );
244 fprintf( stdout
, " [r]esume Resume encoding\n" );
247 static void PrintTitleInfo( hb_title_t
* title
)
249 hb_chapter_t
* chapter
;
251 hb_subtitle_t
* subtitle
;
254 fprintf( stderr
, "+ title %d:\n", title
->index
);
255 fprintf( stderr
, " + vts %d, ttn %d, cells %d->%d (%d blocks)\n",
256 title
->vts
, title
->ttn
, title
->cell_start
, title
->cell_end
,
257 title
->block_count
);
258 fprintf( stderr
, " + duration: %02d:%02d:%02d\n",
259 title
->hours
, title
->minutes
, title
->seconds
);
260 fprintf( stderr
, " + size: %dx%d, aspect: %.2f, %.3f fps\n",
261 title
->width
, title
->height
,
262 (float) title
->aspect
/ HB_ASPECT_BASE
,
263 (float) title
->rate
/ title
->rate_base
);
264 fprintf( stderr
, " + autocrop: %d/%d/%d/%d\n", title
->crop
[0],
265 title
->crop
[1], title
->crop
[2], title
->crop
[3] );
266 fprintf( stderr
, " + chapters:\n" );
267 for( i
= 0; i
< hb_list_count( title
->list_chapter
); i
++ )
269 chapter
= hb_list_item( title
->list_chapter
, i
);
270 fprintf( stderr
, " + %d: cells %d->%d, %d blocks, duration "
271 "%02d:%02d:%02d\n", chapter
->index
,
272 chapter
->cell_start
, chapter
->cell_end
,
273 chapter
->block_count
, chapter
->hours
, chapter
->minutes
,
276 fprintf( stderr
, " + audio tracks:\n" );
277 for( i
= 0; i
< hb_list_count( title
->list_audio
); i
++ )
279 audio
= hb_list_item( title
->list_audio
, i
);
280 if( ( audio
->codec
& HB_ACODEC_AC3
) || ( audio
->codec
& HB_ACODEC_DCA
) )
282 fprintf( stderr
, " + %d, %s, %dHz, %dbps\n", i
+ 1,
283 audio
->lang
, audio
->rate
, audio
->bitrate
);
287 fprintf( stderr
, " + %d, %s\n", i
+ 1, audio
->lang
);
290 fprintf( stderr
, " + subtitle tracks:\n" );
291 for( i
= 0; i
< hb_list_count( title
->list_subtitle
); i
++ )
293 subtitle
= hb_list_item( title
->list_subtitle
, i
);
294 fprintf( stderr
, " + %d, %s (iso639-2: %s)\n", i
+ 1, subtitle
->lang
,
299 static int HandleEvents( hb_handle_t
* h
)
302 hb_get_state( h
, &s
);
309 #define p s.param.scanning
310 case HB_STATE_SCANNING
:
311 /* Show what title is currently being scanned */
312 fprintf( stderr
, "Scanning title %d", p
.title_cur
);
314 fprintf( stderr
, " of %d", p
.title_count
);
315 fprintf( stderr
, "...\n" );
319 case HB_STATE_SCANDONE
:
325 list
= hb_get_titles( h
);
327 if( !hb_list_count( list
) )
329 /* No valid title, stop right there */
330 fprintf( stderr
, "No title found.\n" );
337 int longest_title_idx
=0;
338 int longest_title_pos
=-1;
339 int longest_title_time
=0;
342 fprintf( stderr
, "Searching for longest title...\n" );
344 for( i
= 0; i
< hb_list_count( list
); i
++ )
346 title
= hb_list_item( list
, i
);
347 title_time
= (title
->hours
*60*60 ) + (title
->minutes
*60) + (title
->seconds
);
348 fprintf( stderr
, " + Title (%d) index %d has length %dsec\n",
349 i
, title
->index
, title_time
);
350 if( longest_title_time
< title_time
)
352 longest_title_time
= title_time
;
353 longest_title_pos
= i
;
354 longest_title_idx
= title
->index
;
357 if( longest_title_pos
== -1 )
359 fprintf( stderr
, "No longest title found.\n" );
363 titleindex
= longest_title_idx
;
364 fprintf( stderr
, "Found longest title, setting title to %d\n",
367 title
= hb_list_item( list
, longest_title_pos
);
369 title
= hb_list_item( list
, 0 );
374 /* Scan-only mode, print infos and exit */
376 for( i
= 0; i
< hb_list_count( list
); i
++ )
378 title
= hb_list_item( list
, i
);
379 PrintTitleInfo( title
);
385 /* Set job settings */
388 PrintTitleInfo( title
);
390 if( chapter_start
&& chapter_end
)
392 job
->chapter_start
= MAX( job
->chapter_start
,
394 job
->chapter_end
= MIN( job
->chapter_end
,
396 job
->chapter_end
= MAX( job
->chapter_start
,
402 hb_log("+ Using preset: %s", preset_name
);
404 if (!strcmp(preset_name
, "Animation"))
407 vcodec
= HB_VCODEC_X264
;
408 job
->vbitrate
= 1000;
411 acodec
= HB_ACODEC_FAAC
;
412 x264opts
= strdup("ref=5:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2");
413 job
->chapter_markers
= 1;
414 job
->deinterlace
= 1;
417 turbo_opts_enabled
= 1;
420 if (!strcmp(preset_name
, "AppleTV"))
423 vcodec
= HB_VCODEC_X264
;
424 job
->vbitrate
= 2500;
427 acodec
= HB_ACODEC_FAAC
;
428 x264opts
= strdup("bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2:cabac=0");
429 job
->chapter_markers
= 1;
433 if (!strcmp(preset_name
, "Bedlam"))
436 vcodec
= HB_VCODEC_X264
;
437 job
->vbitrate
= 1800;
438 acodec
= HB_ACODEC_AC3
;
439 x264opts
= strdup("ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1");
440 job
->chapter_markers
= 1;
443 turbo_opts_enabled
= 1;
446 if (!strcmp(preset_name
, "Blind"))
452 acodec
= HB_ACODEC_FAAC
;
454 job
->chapter_markers
= 1;
457 if (!strcmp(preset_name
, "Broke"))
460 vcodec
= HB_VCODEC_X264
;
464 acodec
= HB_ACODEC_FAAC
;
466 x264opts
= strdup("ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip");
467 job
->chapter_markers
= 1;
469 turbo_opts_enabled
= 1;
472 if (!strcmp(preset_name
, "Classic"))
475 job
->vbitrate
= 1000;
478 acodec
= HB_ACODEC_FAAC
;
481 if (!strcmp(preset_name
, "Constant Quality Rate"))
484 vcodec
= HB_VCODEC_X264
;
485 job
->vquality
= 0.64709997177124023;
487 acodec
= HB_ACODEC_AC3
;
488 x264opts
= strdup("ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh");
489 job
->chapter_markers
= 1;
493 if (!strcmp(preset_name
, "Deux Six Quatre"))
496 vcodec
= HB_VCODEC_X264
;
497 job
->vbitrate
= 1600;
498 acodec
= HB_ACODEC_AC3
;
499 x264opts
= strdup("ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip");
500 job
->chapter_markers
= 1;
503 turbo_opts_enabled
= 1;
506 if (!strcmp(preset_name
, "Film"))
509 vcodec
= HB_VCODEC_X264
;
510 job
->vbitrate
= 1800;
511 acodec
= HB_ACODEC_AC3
;
512 x264opts
= strdup("ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip");
513 job
->chapter_markers
= 1;
516 turbo_opts_enabled
= 1;
519 if (!strcmp(preset_name
, "iPhone / iPod Touch"))
522 vcodec
= HB_VCODEC_X264
;
523 job
->h264_level
= 30;
527 acodec
= HB_ACODEC_FAAC
;
529 x264opts
= strdup("cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1");
530 job
->chapter_markers
= 1;
533 if (!strcmp(preset_name
, "iPod High-Rez"))
536 vcodec
= HB_VCODEC_X264
;
537 job
->h264_level
= 30;
538 job
->vbitrate
= 1500;
541 acodec
= HB_ACODEC_FAAC
;
543 x264opts
= strdup("keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
544 job
->chapter_markers
= 1;
547 if (!strcmp(preset_name
, "iPod Low-Rez"))
550 vcodec
= HB_VCODEC_X264
;
551 job
->h264_level
= 30;
555 acodec
= HB_ACODEC_FAAC
;
557 x264opts
= strdup("keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1");
558 job
->chapter_markers
= 1;
561 if (!strcmp(preset_name
, "Normal"))
564 vcodec
= HB_VCODEC_X264
;
565 job
->vbitrate
= 1500;
568 acodec
= HB_ACODEC_FAAC
;
569 x264opts
= strdup("ref=2:bframes=2:subme=5:me=umh");
570 job
->chapter_markers
= 1;
573 turbo_opts_enabled
= 1;
576 if (!strcmp(preset_name
, "PS3"))
579 vcodec
= HB_VCODEC_X264
;
580 job
->vbitrate
= 2500;
583 acodec
= HB_ACODEC_FAAC
;
584 x264opts
= strdup("level=41:subme=5:me=umh");
588 if (!strcmp(preset_name
, "PSP"))
591 job
->vbitrate
= 1024;
594 acodec
= HB_ACODEC_FAAC
;
597 job
->chapter_markers
= 1;
600 if (!strcmp(preset_name
, "QuickTime"))
603 vcodec
= HB_VCODEC_X264
;
604 job
->vbitrate
= 2000;
607 acodec
= HB_ACODEC_FAAC
;
608 x264opts
= strdup("ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip");
609 job
->chapter_markers
= 1;
612 turbo_opts_enabled
= 1;
615 if (!strcmp(preset_name
, "Television"))
618 vcodec
= HB_VCODEC_X264
;
619 job
->vbitrate
= 1300;
622 acodec
= HB_ACODEC_FAAC
;
623 x264opts
= strdup("ref=3:mixed-refs:bframes=16:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip");
624 job
->chapter_markers
= 1;
625 job
->deinterlace
= 1;
627 turbo_opts_enabled
= 1;
631 if ( chapter_markers
)
633 job
->chapter_markers
= chapter_markers
;
635 if( marker_file
!= NULL
)
637 hb_csv_file_t
* file
= hb_open_csv_file( marker_file
);
638 hb_csv_cell_t
* cell
;
642 fprintf( stderr
, "Reading chapter markers from file %s\n", marker_file
);
646 fprintf( stderr
, "Cannot open chapter marker file, using defaults\n" );
650 /* Parse the cells */
651 while( NULL
!= ( cell
= hb_read_next_cell( file
) ) )
653 /* We have a chapter number */
654 if( cell
->cell_col
== 0 )
656 row
= cell
->cell_row
;
657 chapter
= atoi( cell
->cell_text
);
660 /* We have a chapter name */
661 if( cell
->cell_col
== 1 && row
== cell
->cell_row
)
663 /* If we have a valid chapter, copy the string an terminate it */
664 if( chapter
>= job
->chapter_start
&& chapter
<= job
->chapter_end
)
666 hb_chapter_t
* chapter_s
;
668 chapter_s
= hb_list_item( job
->title
->list_chapter
, chapter
- 1);
669 strncpy(chapter_s
->title
, cell
->cell_text
, 1023);
670 chapter_s
->title
[1023] = '\0';
675 hb_dispose_cell( cell
);
678 hb_close_csv_file( file
);
683 if( crop
[0] >= 0 && crop
[1] >= 0 &&
684 crop
[2] >= 0 && crop
[3] >= 0 )
686 memcpy( job
->crop
, crop
, 4 * sizeof( int ) );
689 job
->deinterlace
= deinterlace
;
690 job
->grayscale
= grayscale
;
693 job
->pixel_ratio
= 2;
696 job
->modulus
= modulus
;
701 job
->pixel_ratio
= pixelratio
;
703 /* Add selected filters */
704 job
->filters
= hb_list_init();
707 hb_filter_detelecine
.settings
= detelecine_opt
;
708 hb_list_add( job
->filters
, &hb_filter_detelecine
);
712 hb_filter_deinterlace
.settings
= deinterlace_opt
;
713 hb_list_add( job
->filters
, &hb_filter_deinterlace
);
717 hb_filter_deblock
.settings
= deblock_opt
;
718 hb_list_add( job
->filters
, &hb_filter_deblock
);
722 hb_filter_denoise
.settings
= denoise_opt
;
723 hb_list_add( job
->filters
, &hb_filter_denoise
);
726 if( width
&& height
)
729 job
->height
= height
;
734 hb_fix_aspect( job
, HB_KEEP_WIDTH
);
736 else if( height
&& !loosePixelratio
)
738 job
->height
= height
;
739 hb_fix_aspect( job
, HB_KEEP_HEIGHT
);
741 else if( !width
&& !height
&& !pixelratio
&& !loosePixelratio
)
743 hb_fix_aspect( job
, HB_KEEP_WIDTH
);
745 else if (!width
&& loosePixelratio
)
747 /* Default to full width when one isn't specified for loose anamorphic */
748 job
->width
= title
->width
- job
->crop
[2] - job
->crop
[3];
749 /* The height will be thrown away in hb.c but calculate it anyway */
750 hb_fix_aspect( job
, HB_KEEP_WIDTH
);
753 if( vquality
>= 0.0 && vquality
<= 1.0 )
755 job
->vquality
= vquality
;
760 job
->vquality
= -1.0;
761 job
->vbitrate
= vbitrate
;
765 job
->vcodec
= vcodec
;
769 job
->h264_level
= 13;
773 job
->h264_level
= 30;
777 job
->vrate
= 27000000;
778 job
->vrate_base
= vrate
;
787 if( strcasecmp( audios
, "none" ) )
793 if( *tmp
< '0' || *tmp
> '9' )
795 /* Skip non numeric char */
799 job
->audio_mixdowns
[audio_count
] = audio_mixdown
;
800 job
->audios
[audio_count
++] =
801 strtol( tmp
, &tmp
, 0 ) - 1;
803 job
->audios
[audio_count
] = -1;
812 /* default to the first audio track if none has been specified */
814 job
->audio_mixdowns
[0] = audio_mixdown
;
818 job
->abitrate
= abitrate
;
822 job
->acodec
= acodec
;
827 job
->vbitrate
= hb_calc_bitrate( job
, size
);
828 fprintf( stderr
, "Calculated bitrate: %d kbps\n",
834 job
->subtitle
= sub
- 1;
837 if( native_language
)
839 job
->native_language
= strdup( native_language
);
849 job
->largeFileSize
= 1;
852 job
->file
= strdup( output
);
859 if( x264opts
!= NULL
&& *x264opts
!= '\0' )
861 job
->x264opts
= x264opts
;
863 else /*avoids a bus error crash when options aren't specified*/
865 job
->x264opts
= NULL
;
868 job
->maxWidth
= maxWidth
;
870 job
->maxHeight
= maxHeight
;
877 job
->subtitle_force
= subtitle_force
;
885 * When subtitle scan is enabled do a fast pre-scan job
886 * which will determine which subtitles to enable, if any.
890 x264opts_tmp
= job
->x264opts
;
892 job
->x264opts
= NULL
;
894 job
->indepth_scan
= subtitle_scan
;
895 fprintf( stderr
, "Subtitle Scan Enabled - enabling "
896 "subtitles if found for foreign language segments\n");
897 job
->select_subtitle
= malloc(sizeof(hb_subtitle_t
*));
898 *(job
->select_subtitle
) = NULL
;
901 * Add the pre-scan job
905 job
->x264opts
= x264opts_tmp
;
911 * If subtitle_scan is enabled then only turn it on
912 * for the first pass and then off again for the
915 hb_subtitle_t
**subtitle_tmp
= job
->select_subtitle
;
917 job
->select_subtitle
= NULL
;
921 job
->indepth_scan
= 0;
925 x264opts2
= strdup(x264opts
);
929 * If turbo options have been selected then append them
930 * to the x264opts now (size includes one ':' and the '\0')
932 if( turbo_opts_enabled
)
934 int size
= (x264opts
? strlen(x264opts
) : 0) + strlen(turbo_opts
) + 2;
937 tmp_x264opts
= malloc(size
* sizeof(char));
940 snprintf( tmp_x264opts
, size
, "%s:%s",
941 x264opts
, turbo_opts
);
945 * No x264opts to modify, but apply the turbo options
946 * anyway as they may be modifying defaults
948 snprintf( tmp_x264opts
, size
, "%s",
951 x264opts
= tmp_x264opts
;
953 fprintf( stderr
, "Modified x264 options for pass 1 to append turbo options: %s\n",
956 job
->x264opts
= x264opts
;
960 job
->select_subtitle
= subtitle_tmp
;
964 * On the second pass we turn off subtitle scan so that we
965 * can actually encode using any subtitles that were auto
966 * selected in the first pass (using the whacky select-subtitle
967 * attribute of the job).
969 job
->indepth_scan
= 0;
971 job
->x264opts
= x264opts2
;
978 * Turn on subtitle scan if requested, note that this option
979 * precludes encoding of any actual subtitles.
982 job
->indepth_scan
= 0;
990 #define p s.param.working
991 case HB_STATE_WORKING
:
992 fprintf( stdout
, "\rEncoding: task %d of %d, %.2f %%",
993 p
.job_cur
, p
.job_count
, 100.0 * p
.progress
);
996 fprintf( stdout
, " (%.2f fps, avg %.2f fps, ETA "
997 "%02dh%02dm%02ds)", p
.rate_cur
, p
.rate_avg
,
998 p
.hours
, p
.minutes
, p
.seconds
);
1004 #define p s.param.muxing
1005 case HB_STATE_MUXING
:
1007 fprintf( stdout
, "\rMuxing: %.2f %%", 100.0 * p
.progress
);
1013 #define p s.param.workdone
1014 case HB_STATE_WORKDONE
:
1015 /* Print error if any, then exit */
1019 fprintf( stderr
, "\nRip done!\n" );
1021 case HB_ERROR_CANCELED
:
1022 fprintf( stderr
, "\nRip canceled.\n" );
1025 fprintf( stderr
, "\nRip failed (error %x).\n",
1035 /****************************************************************************
1037 ****************************************************************************/
1038 static volatile int64_t i_die_date
= 0;
1039 void SigHandler( int i_signal
)
1044 i_die_date
= hb_get_date();
1045 fprintf( stderr
, "Signal %d received, terminating - do it "
1046 "again in case it gets stuck\n", i_signal
);
1048 else if( i_die_date
+ 500 < hb_get_date() )
1050 fprintf( stderr
, "Dying badly, files might remain in your /tmp\n" );
1055 /****************************************************************************
1057 ****************************************************************************/
1058 static void ShowHelp()
1063 "Syntax: HandBrakeCLI [options] -i <device> -o <file>\n"
1065 "### General Handbrake Options------------------------------------------------\n\n"
1066 " -h, --help Print help\n"
1067 " -u, --update Check for updates and exit\n"
1068 " -v, --verbose Be verbose\n"
1069 " -C, --cpu Set CPU count (default: autodetected)\n"
1070 " -Z. --preset <string> Use a built-in preset. Capitalization matters, and\n"
1071 " if the preset name has spaces, surround it with\n"
1072 " double quotation marks\n"
1073 " -z, --preset-list See a list of available built-in presets\n"
1076 "### Source Options-----------------------------------------------------------\n\n"
1077 " -i, --input <string> Set input device\n"
1078 " -t, --title <number> Select a title to encode (0 to scan only,\n"
1080 " -L, --longest Select the longest title\n"
1081 " -c, --chapters <string> Select chapters (e.g. \"1-3\" for chapters\n"
1082 " 1 to 3, or \"3\" for chapter 3 only,\n"
1083 " default: all chapters)\n"
1086 "### Destination Options------------------------------------------------------\n\n"
1087 " -o, --output <string> Set output file name\n"
1088 " -f, --format <string> Set output format (avi/mp4/ogm/mkv, default:\n"
1089 " autodetected from file name)\n"
1090 " -4, --large-file Use 64-bit mp4 files that can hold more than\n"
1091 " 4 GB. Note: Breaks iPod, @TV, PS3 compatibility.\n"""
1094 "### Picture Settings---------------------------------------------------------\n\n"
1095 " -w, --width <number> Set picture width\n"
1096 " -l, --height <number> Set picture height\n"
1097 " --crop <T:B:L:R> Set cropping values (default: autocrop)\n"
1098 " -Y, --maxHeight <#> Set maximum height\n"
1099 " -X, --maxWidth <#> Set maximum width\n"
1100 " -s, --subtitle <number> Select subtitle (default: none)\n"
1101 " -U, --subtitle-scan Scan for subtitles in an extra 1st pass, and choose\n"
1102 " the one that's only used 10 percent of the time\n"
1103 " or less. This should locate subtitles for short\n"
1104 " foreign language segments. Best used in conjunction\n"
1105 " with --subtitle-forced.\n"
1106 " -F, --subtitle-forced Only display subtitles from the selected stream if\n"
1107 " the subtitle has the forced flag set. May be used in\n"
1108 " conjunction with --subtitle-scan to auto-select\n"
1109 " a stream if it contains forced subtitles.\n"
1110 " -N, --native-language Select subtitles with this language if it does not\n"
1111 " <string> match the Audio language. Provide the language's\n"
1112 " iso639-2 code (fre, eng, spa, dut, et cetera)\n"
1113 " -m, --markers Add chapter markers (mp4 output format only)\n"
1116 "### Video Options------------------------------------------------------------\n\n"
1117 " -e, --encoder <string> Set video library encoder (ffmpeg,xvid,\n"
1118 " x264,x264b13,x264b30 default: ffmpeg)\n"
1119 " -q, --quality <float> Set video quality (0.0..1.0)\n"
1120 " -Q, --crf Use with -q for CRF instead of CQP\n"
1121 " -S, --size <MB> Set target size\n"
1122 " -b, --vb <kb/s> Set video bitrate (default: 1000)\n"
1123 " -r, --rate Set video framerate (" );
1124 for( i
= 0; i
< hb_video_rates_count
; i
++ )
1126 fprintf( stderr
, hb_video_rates
[i
].string
);
1127 if( i
!= hb_video_rates_count
- 1 )
1128 fprintf( stderr
, "/" );
1130 fprintf( stderr
, ")\n"
1132 " -2, --two-pass Use two-pass mode\n"
1133 " -d, --deinterlace Deinterlace video with yadif/mcdeint filter\n"
1134 " <YM:FD:MM:QP> (default 0:-1:-1:1)\n"
1136 " <fast/slow/slower/slowest>\n"
1137 " -7, --deblock Deblock video with pp7 filter\n"
1138 " <QP:M> (default 0:2)\n"
1139 " -8, --denoise Denoise video with hqdn3d filter\n"
1140 " <SL:SC:TL:TC> (default 4:3:6:4.5)\n"
1142 " <weak/medium/strong>\n"
1143 " -9, --detelecine Detelecine video with pullup filter\n"
1144 " <L:R:T:B:SB:MP> (default 1:1:4:4:0:0)\n"
1145 " -g, --grayscale Grayscale encoding\n"
1146 " -p, --pixelratio Store pixel aspect ratio in video stream\n"
1147 " -P, --loosePixelratio Store pixel aspect ratio with specified width\n"
1148 " <modulus> Takes as optional argument what number you want\n"
1149 " the dimensions to divide cleanly by (default 16)\n"
1155 "### Audio Options-----------------------------------------------------------\n\n"
1156 " -E, --aencoder <string> Set audio encoder (faac/lame/vorbis/ac3, ac3\n"
1157 " meaning passthrough, default: guessed)\n"
1158 " -B, --ab <kb/s> Set audio bitrate (default: 128)\n"
1159 " -a, --audio <string> Select audio channel(s), separated by commas\n"
1160 " (\"none\" for no audio, \"1,2,3\" for multiple\n"
1161 " tracks, default: first one, max: eight)\n"
1162 " -6, --mixdown <string> Format for surround sound downmixing\n"
1163 " (mono/stereo/dpl1/dpl2/6ch, default: dpl2)\n"
1164 " -R, --arate Set audio samplerate (" );
1165 for( i
= 0; i
< hb_audio_rates_count
; i
++ )
1167 fprintf( stderr
, hb_audio_rates
[i
].string
);
1168 if( i
!= hb_audio_rates_count
- 1 )
1169 fprintf( stderr
, "/" );
1171 fprintf( stderr
, " kHz)\n"
1178 "### Advanced Options---------------------------------------------------------\n\n"
1179 " -x, --x264opts <string> Specify advanced x264 options in the\n"
1180 " same style as mencoder:\n"
1181 " option1=value1:option2=value2\n"
1182 " -T, --turbo When using 2-pass use the turbo options\n"
1183 " on the first pass to improve speed\n"
1184 " (only works with x264, affects PSNR by about 0.05dB,\n"
1185 " and increases first pass speed two to four times)\n"
1186 " -V, --vfr Perform variable framerate detelecine on NTSC video\n"
1190 /****************************************************************************
1192 ****************************************************************************/
1193 static void ShowPresets()
1195 printf("\n+ Animation: -e x264 -b 1000 -B 160 -R 48 -E faac -f mkv -m -d -p -2 -T -x ref=5:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2\n");
1197 printf("\n+ AppleTV: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -m -p -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2:cabac=0\n");
1199 printf("\n+ Bedlam: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=16:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1\n");
1201 printf("\n+ Blind: -b 512 -B 128 -R 48 -E faac -f mp4 -w 512 -m\n");
1203 printf("\n+ Broke: -e x264 -S 695 -B 128 -R 48 -E faac -f mp4 -w 640 -m -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1205 printf("\n+ Classic: -b 1000 -B 160 -R 48 -E faac -f mp4\n");
1207 printf("\n+ Constant Quality Rate: -e x264 -q 0.64709997177124023 -Q -E ac3 -f mkv -m -p -x ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh\n");
1209 printf("\n+ Deux Six Quatre: -e x264 -b 1600 -E ac3 -f mkv -m -p -2 -T -x ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip\n");
1211 printf("\n+ Film: -e x264 -b 1800 -E ac3 -f mkv -m -p -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip\n");
1213 printf("\n+ iPhone / iPod Touch: -e x264b30 -b 960 -B 128 -R 48 -E faac -f mp4 -w 480 -m -x cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1\n");
1215 printf("\n+ iPod High-Rez: -e x264b30 -b 1500 -B 160 -R 48 -E faac -f mp4 -w 640 -m -x keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1217 printf("\n+ iPod Low-Rez: -e x264b30 -b 700 -B 160 -R 48 -E faac -f mp4 -w 320 -m -x keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1\n");
1219 printf("\n+ Normal: -e x264 -b 1500 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=2:bframes=2:subme=5:me=umh\n");
1221 printf("\n+ PS3: -e x264 -b 2500 -B 160 -R 48 -E faac -f mp4 -p -x level=41:subme=5:me=umh\n");
1223 printf("\n+ PSP: -b 1024 -B 128 -R 48 -E faac -f mp4 -w 368 -l 208 -m\n");
1225 printf("\n+ QuickTime: -e x264 -b 2000 -B 160 -R 48 -E faac -f mp4 -m -p -2 -T -x ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:trellis=1:no-fast-pskip\n");
1227 printf("\n+ Television: -e x264 -b 1300 -B 160 -R 48 -E faac -f mkv -m -d -2 -T -x ref=3:mixed-refs:bframes=16:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip\n");
1231 /****************************************************************************
1233 ****************************************************************************/
1234 static int ParseOptions( int argc
, char ** argv
)
1238 static struct option long_options
[] =
1240 { "help", no_argument
, NULL
, 'h' },
1241 { "update", no_argument
, NULL
, 'u' },
1242 { "verbose", no_argument
, NULL
, 'v' },
1243 { "cpu", required_argument
, NULL
, 'C' },
1245 { "format", required_argument
, NULL
, 'f' },
1246 { "input", required_argument
, NULL
, 'i' },
1247 { "output", required_argument
, NULL
, 'o' },
1248 { "large-file", no_argument
, NULL
, '4' },
1250 { "title", required_argument
, NULL
, 't' },
1251 { "longest", no_argument
, NULL
, 'L' },
1252 { "chapters", required_argument
, NULL
, 'c' },
1253 { "markers", optional_argument
, NULL
, 'm' },
1254 { "audio", required_argument
, NULL
, 'a' },
1255 { "mixdown", required_argument
, NULL
, '6' },
1256 { "subtitle", required_argument
, NULL
, 's' },
1257 { "subtitle-scan", no_argument
, NULL
, 'U' },
1258 { "subtitle-forced", no_argument
, NULL
, 'F' },
1259 { "native-language", required_argument
, NULL
,'N' },
1261 { "encoder", required_argument
, NULL
, 'e' },
1262 { "aencoder", required_argument
, NULL
, 'E' },
1263 { "two-pass", no_argument
, NULL
, '2' },
1264 { "deinterlace", optional_argument
, NULL
, 'd' },
1265 { "deblock", optional_argument
, NULL
, '7' },
1266 { "denoise", optional_argument
, NULL
, '8' },
1267 { "detelecine", optional_argument
, NULL
, '9' },
1268 { "grayscale", no_argument
, NULL
, 'g' },
1269 { "pixelratio", no_argument
, NULL
, 'p' },
1270 { "loosePixelratio", optional_argument
, NULL
, 'P' },
1271 { "width", required_argument
, NULL
, 'w' },
1272 { "height", required_argument
, NULL
, 'l' },
1273 { "crop", required_argument
, NULL
, 'n' },
1275 { "vb", required_argument
, NULL
, 'b' },
1276 { "quality", required_argument
, NULL
, 'q' },
1277 { "size", required_argument
, NULL
, 'S' },
1278 { "ab", required_argument
, NULL
, 'B' },
1279 { "rate", required_argument
, NULL
, 'r' },
1280 { "arate", required_argument
, NULL
, 'R' },
1281 { "crf", no_argument
, NULL
, 'Q' },
1282 { "x264opts", required_argument
, NULL
, 'x' },
1283 { "turbo", no_argument
, NULL
, 'T' },
1284 { "maxHeight", required_argument
, NULL
, 'Y' },
1285 { "maxWidth", required_argument
, NULL
, 'X' },
1286 { "preset", required_argument
, NULL
, 'Z' },
1287 { "preset-list", no_argument
, NULL
, 'z' },
1288 { "vfr", no_argument
, NULL
, 'V' },
1293 int option_index
= 0;
1296 c
= getopt_long( argc
, argv
,
1297 "hvuC:f:4i:o:t:Lc:ma:6:s:UFN:e:E:2d789gpP::w:l:n:b:q:S:B:r:R:Qx:TY:X:VZ:z",
1298 long_options
, &option_index
);
1313 debug
= HB_DEBUG_ALL
;
1316 cpu
= atoi( optarg
);
1321 preset_name
= strdup(optarg
);
1328 format
= strdup( optarg
);
1331 input
= strdup( optarg
);
1334 output
= strdup( optarg
);
1340 titleindex
= atoi( optarg
);
1348 if( sscanf( optarg
, "%d-%d", &start
, &end
) == 2 )
1350 chapter_start
= start
;
1353 else if( sscanf( optarg
, "%d", &start
) == 1 )
1355 chapter_start
= start
;
1356 chapter_end
= chapter_start
;
1360 fprintf( stderr
, "chapters: invalid syntax (%s)\n",
1367 if( optarg
!= NULL
)
1369 marker_file
= strdup( optarg
);
1371 chapter_markers
= 1;
1374 audios
= strdup( optarg
);
1377 if( !strcasecmp( optarg
, "mono" ) )
1379 audio_mixdown
= HB_AMIXDOWN_MONO
;
1381 else if( !strcasecmp( optarg
, "stereo" ) )
1383 audio_mixdown
= HB_AMIXDOWN_STEREO
;
1385 else if( !strcasecmp( optarg
, "dpl1" ) )
1387 audio_mixdown
= HB_AMIXDOWN_DOLBY
;
1389 else if( !strcasecmp( optarg
, "dpl2" ) )
1391 audio_mixdown
= HB_AMIXDOWN_DOLBYPLII
;
1393 else if( !strcasecmp( optarg
, "6ch" ) )
1395 audio_mixdown
= HB_AMIXDOWN_6CH
;
1399 sub
= atoi( optarg
);
1408 native_language
= strdup( optarg
);
1414 if( optarg
!= NULL
)
1416 if (!( strcmp( optarg
, "fast" ) ))
1418 deinterlace_opt
= "-1";
1420 else if (!( strcmp( optarg
, "slow" ) ))
1422 deinterlace_opt
= "0";
1424 else if (!( strcmp( optarg
, "slower" ) ))
1426 deinterlace_opt
= "2:-1:1";
1428 else if (!( strcmp( optarg
, "slowest" ) ))
1430 deinterlace_opt
= "1:-1:1";
1434 deinterlace_opt
= strdup( optarg
);
1440 if( optarg
!= NULL
)
1442 deblock_opt
= strdup( optarg
);
1447 if( optarg
!= NULL
)
1449 if (!( strcmp( optarg
, "weak" ) ))
1451 denoise_opt
= "2:1:2:3";
1453 else if (!( strcmp( optarg
, "medium" ) ))
1455 denoise_opt
= "3:2:2:3";
1457 else if (!( strcmp( optarg
, "strong" ) ))
1459 denoise_opt
= "7:7:5:5";
1463 denoise_opt
= strdup( optarg
);
1469 if( optarg
!= NULL
)
1471 detelecine_opt
= strdup( optarg
);
1482 loosePixelratio
= 1;
1483 if( optarg
!= NULL
)
1485 modulus
= atoi( optarg
);
1489 if( !strcasecmp( optarg
, "ffmpeg" ) )
1491 vcodec
= HB_VCODEC_FFMPEG
;
1493 else if( !strcasecmp( optarg
, "xvid" ) )
1495 vcodec
= HB_VCODEC_XVID
;
1497 else if( !strcasecmp( optarg
, "x264" ) )
1499 vcodec
= HB_VCODEC_X264
;
1501 else if( !strcasecmp( optarg
, "x264b13" ) )
1503 vcodec
= HB_VCODEC_X264
;
1506 else if( !strcasecmp( optarg
, "x264b30" ) )
1508 vcodec
= HB_VCODEC_X264
;
1513 fprintf( stderr
, "invalid codec (%s)\n", optarg
);
1518 if( !strcasecmp( optarg
, "ac3" ) )
1520 acodec
= HB_ACODEC_AC3
;
1522 else if( !strcasecmp( optarg
, "lame" ) )
1524 acodec
= HB_ACODEC_LAME
;
1526 else if( !strcasecmp( optarg
, "faac" ) )
1528 acodec
= HB_ACODEC_FAAC
;
1530 else if( !strcasecmp( optarg
, "vorbis") )
1532 acodec
= HB_ACODEC_VORBIS
;
1536 width
= atoi( optarg
);
1539 height
= atoi( optarg
);
1544 char * tmp
= optarg
;
1545 for( i
= 0; i
< 4; i
++ )
1549 crop
[i
] = strtol( tmp
, &tmp
, 0 );
1558 for( i
= 0; i
< hb_video_rates_count
; i
++ )
1560 if( !strcmp( optarg
, hb_video_rates
[i
].string
) )
1562 vrate
= hb_video_rates
[i
].rate
;
1568 fprintf( stderr
, "invalid framerate %s\n", optarg
);
1576 for( i
= 0; i
< hb_audio_rates_count
; i
++ )
1578 if( !strcmp( optarg
, hb_audio_rates
[i
].string
) )
1580 arate
= hb_audio_rates
[i
].rate
;
1586 fprintf( stderr
, "invalid framerate %s\n", optarg
);
1591 vbitrate
= atoi( optarg
);
1594 vquality
= atof( optarg
);
1597 size
= atoi( optarg
);
1600 abitrate
= atoi( optarg
);
1606 x264opts
= strdup( optarg
);
1609 turbo_opts_enabled
= 1;
1612 maxHeight
= atoi( optarg
);
1615 maxWidth
= atoi (optarg
);
1622 fprintf( stderr
, "unknown option (%s)\n", argv
[optind
] );
1630 static int CheckOptions( int argc
, char ** argv
)
1637 if( input
== NULL
|| *input
== '\0' )
1639 fprintf( stderr
, "Missing input device. Run %s --help for "
1640 "syntax.\n", argv
[0] );
1645 if( titleindex
> 0 )
1647 if( output
== NULL
|| *output
== '\0' )
1649 fprintf( stderr
, "Missing output file name. Run %s --help "
1650 "for syntax.\n", argv
[0] );
1656 char * p
= strrchr( output
, '.' );
1659 if( p
&& !strcasecmp( p
, ".avi" ) )
1663 else if( p
&& ( !strcasecmp( p
, ".mp4" ) ||
1664 !strcasecmp( p
, ".m4v" ) ) )
1671 else if( p
&& ( !strcasecmp( p
, ".ogm" ) ||
1672 !strcasecmp( p
, ".ogg" ) ) )
1676 else if( p
&& !strcasecmp(p
, ".mkv" ) )
1682 fprintf( stderr
, "Output format couldn't be guessed "
1683 "from file name, using default.\n" );
1687 else if( !strcasecmp( format
, "avi" ) )
1691 else if( !strcasecmp( format
, "mp4" ) )
1698 else if( !strcasecmp( format
, "ogm" ) ||
1699 !strcasecmp( format
, "ogg" ) )
1703 else if( !strcasecmp( format
, "mkv" ) )
1709 fprintf( stderr
, "Invalid output format (%s). Possible "
1710 "choices are avi, mp4, m4v, ogm, ogg and mkv\n.", format
);
1716 if( mux
== HB_MUX_MP4
|| mux
== HB_MUX_IPOD
)
1718 acodec
= HB_ACODEC_FAAC
;
1720 else if( mux
== HB_MUX_AVI
)
1722 acodec
= HB_ACODEC_LAME
;
1724 else if( mux
== HB_MUX_OGM
)
1726 acodec
= HB_ACODEC_VORBIS
;
1728 else if( mux
== HB_MUX_MKV
)
1730 acodec
= HB_ACODEC_AC3
;