kill debug setting
[undvd.git] / encvid.pl
blob237b43ef1fba44e133167ef0f38bd8703c670f52
1 #!/usr/bin/perl
3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
6 use strict;
7 use Getopt::Long qw(:config no_ignore_case);
9 BEGIN {
10 use File::Basename;
11 push(@INC, dirname(grep(-l, $0) ? readlink $0 : $0));
12 require colors; colors->import(qw(:DEFAULT));
13 require common; common->import(qw(:DEFAULT $suite $defaults $tools));
17 my $usage = "Usage: " . s_b($suite->{tool_name}) . " "
18 . s_bb("<file(s)>")
19 . " [" . s_b("options") . "]
20 <file(s)> files to encode\n
21 --start start after this many seconds (usually for testing)
22 -e --end end after this many seconds (usually for testing)\n
23 -C do sanity check (check for missing tools)
24 -z --adv <show advanced options>
25 --version show " . $suite->{name} . " version\n";
27 my $adv_usage = "Advanced usage: " . s_b($suite->{tool_name}) . " "
28 . s_bb("<file(s)>")
29 . " [" . s_b("options") . "]
30 -o --size output file size in mb (integer value)
31 --bpp bits per pixel (float value)
32 -1 force 1-pass encoding
33 -2 force 2-pass encoding
34 -c --crop autocrop video
35 -r --scale scale video to x:y (integer value or " . s_bb("0") . ") or " . s_bb("off") . " to disable
36 -f --smooth use picture smoothing filter
37 -D --dryrun dry run (display encoding parameters without encoding)\n
38 --cont set container format
39 --acodec set audio codec
40 --vcodec set video codec\n";
42 my ($opts_start, $opts_end, $target_size, $bpp, $autocrop, $prescale, $postscale);
43 my ($dry_run, $opts_acodec, $opts_vcodec, $opts_cont);
44 my $custom_scale = "off";
45 my $target_passes = 1;
47 my $parse = GetOptions(
48 "start=f"=>\$opts_start,
49 "e|end=f"=>\$opts_end,
51 "C"=> sub { init_cmds(1); exit; },
52 "z|adv"=> sub { print $adv_usage; exit; },
53 "version"=>\&print_version,
55 "o|size=i"=>\$target_size,
56 "bpp=f"=>\$bpp,
57 "1"=> sub { $target_passes = 1; },
58 "2"=> sub { $target_passes = 2; },
59 "c|crop"=> sub { $autocrop = 1; },
60 "r|scale=s"=>\$custom_scale,
61 "f|smooth"=> sub { $prescale = "spp,"; $postscale = ",hqdn3d"; },
62 "D|dryrun"=> sub { $dry_run = 1; },
64 "cont=s"=>\$opts_cont,
65 "acodec=s"=>\$opts_acodec,
66 "vcodec=s"=>\$opts_vcodec,
69 print_tool_banner();
71 if (! $parse) {
72 print $usage;
73 exit 2;
76 my @startpos = ("-ss", $opts_start ? $opts_start : 0);
77 my @endpos;
78 if ($opts_end) {
79 push(@endpos, "-endpos", $opts_end);
82 my @files = @ARGV;
83 if (scalar @files < 1) {
84 nonfatal("No files to encode, exiting");
85 print $usage;
86 exit 2;
90 init_logdir();
93 # Set container and codecs
95 my $container = $opts_cont ? $opts_cont : $defaults->{container};
96 my ($audio_codec, $video_codec, $ext, @cont_args) = set_container_opts($opts_acodec,
97 $opts_vcodec, $container);
99 print " - Output format :: "
100 . "container: " . s_it($container)
101 . " audio: " . s_it($audio_codec)
102 . " video: " . s_it($video_codec) . "\n";
105 # Display dry-run status
107 if ($dry_run) {
108 print " * Performing dry-run\n";
109 print_title_line(1);
112 foreach my $file (@files) {
114 if (! -e $file) {
115 nonfatal("File %%%$file%%% does not exist");
116 next;
117 } elsif (-d $file) {
118 nonfatal("%%%$file%%% is a directory");
119 next;
122 my $title_name = $file;
123 $title_name =~ s/^(.*)\..*$/$1/g;
126 # Display encode status
128 if (! $dry_run) {
129 print " * Now encoding file " . s_bb(trunc(38, 1, $file, "..."));
130 if ($opts_start and $opts_end) {
131 print " [" . s_bb($opts_start) . "s - " . s_bb($opts_end) . "s]";
132 } elsif ($opts_start) {
133 print " [" . s_bb($opts_start) . "s -> ]";
134 } elsif ($opts_end) {
135 print " [ -> " . s_bb($opts_end) . "s]";
137 print "\n";
141 # Extract information from the title
143 my $title = examine_title($file);
145 # Init encoding target info
147 my $ntitle = deep_copy($title);
148 $ntitle->{aformat} = $audio_codec;
149 $ntitle->{vformat} = $video_codec;
150 $ntitle->{filename} = "$title_name.$ext";
153 # Do we need to crop?
155 if ($autocrop) {
156 print " + Finding out how much to crop...\r";
157 my ($width, $height, @crop_opts) = crop_title($file);
158 if (! $width or ! $height or ! @crop_opts) {
159 fatal("Crop detection failed");
161 $ntitle->{width} = $width;
162 $ntitle->{height} = $height;
165 # Find out how to scale the dimensions
167 my ($width, $height) =
168 scale_title($ntitle->{width}, $ntitle->{height}, $custom_scale);
169 $ntitle->{width} = $width;
170 $ntitle->{height} = $height;
171 my @scale_args = ("scale=$width:$height");
173 # Estimate filesize of audio
175 $ntitle->{abitrate} = set_acodec_opts($container, $ntitle->{aformat},
176 $ntitle->{abitrate}, 1);
177 my $audio_size = compute_media_size($ntitle->{length}, $ntitle->{abitrate});
178 my @acodec_args = set_acodec_opts($container, $ntitle->{aformat},
179 $ntitle->{abitrate});
181 # Decide bpp
183 if ($bpp) {
184 $ntitle->{bpp} = $bpp;
185 } elsif ($target_size) {
186 my $video_size = $target_size - $audio_size;
187 $video_size = 1 if $video_size <= 0;
188 $ntitle->{bpp} = compute_bpp($ntitle->{width}, $ntitle->{height},
189 $ntitle->{fps}, $ntitle->{length}, $video_size);
190 } else {
191 $ntitle->{bpp} = set_bpp($video_codec, $target_passes);
194 # Reset the number of passes based on the bpp
196 if ($target_passes) {
197 $ntitle->{passes} = $target_passes;
198 } else {
199 $ntitle->{passes} = set_passes($video_codec, $ntitle->{bpp});
202 # Compute bitrate
204 $ntitle->{vbitrate} = compute_vbitrate($ntitle->{width},
205 $ntitle->{height}, $ntitle->{fps}, $ntitle->{bpp});
208 # Dry run
210 if ($dry_run) {
212 # Estimate output size
214 if ($target_size) {
215 $ntitle->{filesize} = $target_size;
216 } else {
217 my $video_size = compute_media_size($ntitle->{length},
218 $ntitle->{vbitrate});
219 $ntitle->{filesize} = int($video_size + $audio_size);
222 print_title_line(0, $title);
223 print_title_line(0, $ntitle);