summaryrefslogtreecommitdiffstats
path: root/tools/gen_frikanalen_video.pl
blob: 26ca45c99a0767afb8ba16deb585ba296f71a886 (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
#!/usr/bin/perl
#
# Script for adding start/end poster and convert to frikanalen
# acceptable avi format anamporphic PAL with pillarboxing
#
# DV PAL Anamophic i en AVI-fil eller på en DV/DVCam-tape (25 Mbit DV,
# 25 fps, interlaced, lower field first, 720x576
#
# See also
# http://www.frikanalen.tv/lage-tv/mange-spør-om-formater-og-logistikk
# http://kevinlocke.name/bits/2012/08/25/letterboxing-with-ffmpeg-avconv-for-mobile/
# (export INPUT_FILE=Nina_Paley_tribute-to-EFF.m4v OUTPUT_FILE=Nina_Paley_tribute-to-EFF.avi MAX_WIDTH=720 MAX_HEIGHT=576; avconv     -i "$INPUT_FILE"     -map 0     -vf "scale=iw*sar*min($MAX_WIDTH/(iw*sar)\,$MAX_HEIGHT/ih):ih*min($MAX_WIDTH/(iw*sar)\,$MAX_HEIGHT/ih),pad=$MAX_WIDTH:$MAX_HEIGHT:(ow-iw)/2:(oh-ih)/2"     -c:v libx264     -vprofile baseline -level 30     -c:a libvo_aacenc     "$OUTPUT_FILE")

# Script is work in progress 2010-09-04 /JB
# standard backggrund for NUUG is in ./lib/graphic/tv-bg.png (relative
# to script location in svn-tree)
#
# ffmpeg tip for videos with wrong field order
# http://ffmpeg.org/ffmpeg-all.html#yadif-1
# -vf yadif=parity=tff or bff (top field first or bottom field first)
#
# metafile format is like this:
#
#title=Radioamatørenes sporingssystem
#presenter=Øyvind Hanssen
#date=2009-03-17
#place=Oslo
#venue=Høgskolen i Oslo
#url=http://www.nuug.no/
#organizer=Petter Reinholdtsen
#introduction=Petter Reinholdtsen
#email=sekretariat@nuug.no
#aspect=4:3
#camera=Stian Hübener, Hans Petter Fjeld
#videomixer=Ole Kristian Lien
#sound=?
#spokenlanguage=no
#clipin=1:20
#clipout=1:20:20
#
# PS: If this script is used on an sshfs mounted filesystem, the
# option sshfs -o workaround=rename must be used .  If not,
# normalize-audio will just silently fail and no audio normalization
# will take place.

use strict;
use warnings;

use Data::Dumper;
use Getopt::Std;
use File::Spec;

my %opts;
my $intro_length = 10;
my $pid = $$;

getopts('di:m:o:b:s:S:', \%opts);
my $debug = $opts{d} || 0;
my $workdir = "./fk-temp-$pid";
my $startposter = "$workdir/startposter.jpg";
my $endposter = "$workdir/endposter.jpg";
my $startposter_dv = "$workdir/startposter.dv";
my $endposter_dv = "$workdir/endposter.dv";
my $outxml = "$workdir/playout.xml";
my $metafile;
my $srcfile;
my $srtfile;
my $bgfile;
my $outputfile;
my $normalize_cmd = "/usr/bin/normalize-audio";
# http://normalize.nongnu.org/
my $soundlevel_dbfs = '-18dBFS';

my $MAX_WIDTH = 720;
my $MAX_HEIGHT = 576;

#foreach (keys %opts ) { print "$_\n"; };
if ( $opts{'m'} ) {
  $metafile = $opts{'m'} ;
} else {
  usage();
  exit 1;
}

my $meta = read_meta();

if ( $opts{'b'} ) {
  $bgfile = $opts{'b'} ;
} else {
  usage();
  exit 1;
}


if ( $ARGV[0] && $ARGV[0] eq 'front' ) {
  create_startposter_png($startposter,$bgfile);
  print "Frontpage in $startposter\n";
  print "Check it out !\n";
  exit ;
}

if ( $opts{'o'} ) {
  $outputfile = $opts{'o'} ;
} else {
  usage();
  exit 1;
}

if ( $opts{'i'} ) {
  # Convert to absolute path to make sure file names with colon in them are
  # not interpreted as URLs.
  $srcfile = File::Spec->rel2abs($opts{'i'});
} else {
  usage();
  exit 1;
}
my $subdelay = "";

if ( $opts{'s'} ) {
  $srtfile = getsrtfile();
  if ( ! -f $srtfile ) {
   print "$srtfile does not exist\n";
   exit 1 ;
  }
  if ( $opts{'S'} ) {
   $subdelay = "-subdelay $opts{'S'}";
  }
}



`mkdir -p $workdir`;

create_startposter_png($startposter,$bgfile);
create_endposter_png($endposter,$bgfile);
my $outfile = File::Spec->rel2abs($opts{'o'});

my $framerate = 25; # frames per second
my $durationframes = $intro_length * $framerate;
my @cmd = ("melt");

# Define output profile
push(@cmd, "-profile", "dv_pal_wide");

# Do audio normalization (also require xml consumer and second run)
push(@cmd, "-filter", "sox:analysis");

# Add intro page for a few seconds.
push(@cmd, $startposter, "out=$durationframes");

# Then the video itself
push(@cmd, "$srcfile");

push(@cmd, "in=" . duration2sec($meta->{'clipin'}) * $framerate)
     if (exists $meta->{'clipin'});
push(@cmd, "out=" . duration2sec($meta->{'clipout'}) * $framerate)
     if (exists $meta->{'clipout'});

# Next, the out image
push(@cmd, $endposter, "out=$durationframes");

# Finally the XML consumer to handle the audio normalization
push(@cmd, "-consumer", "xml:$outxml", "video_off=1", "all=1");

# FIXME missing subtitle handling
runcmd(@cmd);

# The outfile must be a .dv file for melt to pick good defaults.  If
# it is .avi, the video quality is very bad.
runcmd("melt", $outxml,
       "-consumer", "avformat:$outfile",
       # http://www.mltframework.org/bin/view/MLT/ConsumerAvformat#field_order
       # http://www.frikanalen.tv/lage-tv/mange-spør-om-formater-og-logistikk
       # lower field first
#       "field_order=tb",
    );

if ( -d $workdir ) {
    `rm -rf $workdir`;
}

#### Functions #########

sub usage {
    print <<EOF;
Usage: $0 -i inputfile.dv -m metafile -o outputfile.avi -b backgroundfile.png [-s /dir/where/srtfiles/are ]

If -s is given the script expects a file named <basename_of_raw_file>.srt
located in the path given as arg to -s option
EOF
}

sub duration2sec {
    my $str = shift;
    my $duration = 0.0;
    for my $part (split(/:/, $str)) {
        $duration *= 60.0;
        $duration += $part;
    }
    return $duration;
}

sub read_meta {
  my $ret;

  # Set some default values
  $ret->{'url'} = 'http://www.nuug.no/';
  $ret->{'email'} = 'sekretariat@nuug.no';
#  $ret->{'editor'} = 'Petter Reinholdtsen';

  open M, "$metafile" or die "Cannot open $metafile for read :$!";
  while (<M>) {
    chomp;
    my @l = split("=",$_);
    if (defined $l[1]) {
       $ret->{$l[0]} = $l[1];
    }
  }
  close M;
  return $ret;
}

sub count_words_n_space {
  my $word = shift;
  my $count = 0;
  while ( $word =~ /(.)/g ) { $count++; }
  $count++;
  return $count;
}

sub break_title {
  my $title = shift;
#  print $title;
  my $cols = 30;
  my $count = 0 ;
  my $ln = 0;
  my @lines = ('', '', '', '');
  my @words = split(" ",$title);
  foreach my $word (@words) {
    $count += count_words_n_space($word);
    if ($count < $cols ) {
      $lines[$ln] .= "$word ";
    } else {
#      print "$lines[$ln]\n";
      $count = 0;
      $ln++;
      $count += count_words_n_space($word);
      $lines[$ln] .= "$word ";
    }
  }
  return \@lines;
}

sub create_startposter_png {
  my $name = shift;
  my $title_lines = break_title($meta->{'title'});
  my $bgfile = shift;
  my @cmd =
      ("convert", "$bgfile",
       "-fill", "white", "-gravity", "NorthWest",
       "-pointsize", "36",
       "-draw", "\"text 450,167 \'Foreningen NUUG presenterer\'\"",
       "-pointsize", "72",
       "-draw", "\"text 450,247 \'$meta->{'presenter'}\'\"",
       "-pointsize", "60",
       "-draw", "\"text 450,380 \'$title_lines->[0]\'\"",
       "-draw", "\"text 450,460 \'$title_lines->[1]\'\"",
       "-draw", "\"text 450,540 \'$title_lines->[2]\'\"",
       "-draw", "\"text 450,620 \'$title_lines->[3]\'\"",
       "-pointsize", "36",
       "-draw", "\"text 52,790 \'$meta->{'url'}\'\"",
       "-draw", "\"text 52,826 \'$meta->{'email'}\'\"",
       "-draw", "\"text 750,640 \'$meta->{'place'}, $meta->{'date'}\'\" $name");

  if ( !runcmd(@cmd) ) { die "Failed to execute system command in" . (caller(0))[3] ."\n"; }
}

sub create_endposter_png {
# $cmd_body .= " -draw "text $left_margin,$pos \'$n: $meta->{$n}\'"
  my %keyword_map = (
      "introduction" => "Introdusert av",
#      "editor" => "Redaktør",
      "venue" => "Lokaler",
      "organizer" => "Organisert av",
      "camera" => "Kamera",
      "sound" => "Lydoppsett",
      "videomixer" => "Videomiks",
      "teksting" => "Teksting",
      );
  my $line_distance = 52;
  my $text_size = 40;
  my $pos = 180;
  my $left_margin = 450;
  my $cmd_body = "";
  my @endnotes;
  my @endnote_tags = ("introduction","organizer", "venue","camera","sound","videomixer","editor","teksting" );
  foreach my $n ( @endnote_tags ) {
    if ($meta->{$n} ) {

# Only show organizer if introduction and organizer are identical.
      next if ("introduction" eq $n && $meta->{$n} eq $meta->{'organizer'});

      push(@endnotes,"$keyword_map{$n}: $meta->{$n}");
    }
  }
  foreach my $line ( @endnotes ) {
    $cmd_body .= "  -draw \"text $left_margin,$pos \'$line \'\"";
    $pos += $line_distance;
  }
  my $name = shift;
  my $bgfile = shift;
  my @cmd =
      ("convert", "$bgfile",
       "-pointsize", "$text_size",
       "-fill", "white",
       "-gravity", "NorthWest",
       "$cmd_body",
       "-pointsize", "36",
       "-draw", "\"text 52,790 \'$meta->{'url'}\'\"",
       "-draw", "\"text 52,826 \'$meta->{'email'}\'\"",
       "-draw", "\"text 750,640 \'$meta->{'place'}, $meta->{'date'}\'\" $name");
  if ( !runcmd(@cmd) ) { die "Failed to execute system command in" . (caller(0))[3] ."\n"; }
}

sub getsrtfile {
    $opts{'i'} =~ /.+\/(.+)\..+$/; # Could be .dv or .avi or whatnot. This strips it off anyway.
    return "$opts{'s'}/$1.srt";
}

sub runcmd {
  my $cmd = join(" ", @_);
  print "Cmd: $cmd\n" if $debug;
  my $f = `$cmd  || echo  -n -1`;
  return 0 if ( $f eq -1 );
  return 1;
}