6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
15 my $debug = $ENV{DEBUG} || 0;
17 my $movie = shift @ARGV;
19 sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
20 sub base_name { $1 if $_[0] =~ m{^.+/([^/]+)$} }
22 if ( ! $movie && -e 'media/_editing' ) {
23 $movie = 'media/' . readlink('media/_editing') . '/video.ogv';
24 warn "using media/_editing -> $movie\n";
25 } elsif ( -d $movie && $movie =~ m{media/} ) {
26 $movie .= '/video.ogv';
27 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
28 my $movie_master = $movie;
29 $movie = base_dir($movie) . '/video.ogv';
31 symlink base_name($movie_master), $movie;
32 warn "symlink video.ogv -> $movie_master";
34 warn "using symlink video.ogv -> ", readlink $movie;
36 } elsif ( -f $movie ) {
37 warn "using video $movie";
39 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
42 my $media_part = my $media_dir = base_dir($movie);
43 $media_part =~ s{media/}{};
44 $media_part =~ s{/$}{};
46 unlink 'media/_editing';
47 symlink $media_part, 'media/_editing';
49 warn "# media_part $media_part\n";
51 my $edl = "/dev/shm/edl";
52 my $subtitles = $movie;
53 $subtitles =~ s{\.\w+$}{.srt};
57 my $slide_factor = 4; # 1/4 size of video
64 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
70 '-vf' => 'screenshot',
73 my $select = IO::Select->new();
74 #$select->add( \*STDIN );
75 $select->add( $from_mplayer );
80 warn "$movie ", -s $movie, " bytes $edl\n";
81 print $to_mplayer qq|loadfile "$movie"\n|;
82 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
87 my $term_id = `xdotool getwindowfocus`;
92 $mplayer_id ||= `xdotool search mplayer`;
93 warn "focus_mplayer $mplayer_id\n";
94 system "xdotool windowactivate $mplayer_id"
98 warn "focus_term $term_id\n";
99 system "xdotool windowactivate $term_id";
103 my ( $pos, $osd ) = @_;
105 warn "PREROLL $pos $osd\n";
106 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
107 my $to = $pos - $preroll;
109 print $to_mplayer "set_property time_pos $to\n";
110 print $to_mplayer "get_property time_pos\n";
111 print $to_mplayer "play\n";
124 print $to_mplayer $cmd;
131 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
137 my $t = join(',', @_);
138 system "oggThumb -t $t -o jpg -n $file $video";
143 return sprintf('%02d:%02d', int($t/60), int($t%60));
148 if ( ! $prop->{width} || ! $prop->{height} ) {
149 warn "SKIP no size yet\n";
153 if ( ! @subtitles ) {
154 warn "SKIP no subtitles yet\n";
166 foreach my $s ( @subtitles ) {
167 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
168 startTime => $s->[0],
173 if ( $s->[2] =~ m{video:(.+)} ) {
175 my $path = "$media_dir/$video";
177 warn "MISSING $path: $!\n";
179 my $frame_dir = "$media_dir/s/$video";
180 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
182 push @videos, [ @$s, $video ];
184 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
185 push @frames, [ $2, $1 ];
189 next unless $s->[2] =~ m{\[(\d+)\]};
192 push @{ $sync->{customEvents} }, {
193 startTime => $s->[0],
195 action => 'chapterChange',
197 carousel => 'theCarousel',
200 description => $s->[2],
201 src => slide_jpg( 4 => $1 ),
206 push @slide_t, $s->[0];
210 my $hires = "$media_dir/s/hires";
211 mkdir $hires unless -e $hires;
212 oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
214 foreach my $i ( 0 .. $#frames ) {
215 my $from = "$hires/.f$i.jpg";
216 my $to = "$hires/f" . $frames[$i]->[1] . '.jpg';
217 rename $from, $to || warn "can't rename $from -> $to: $!";
221 foreach ( 0 .. $#slide_t ) {
222 my $slide_nr = $_ + 1;
223 push @{ $sync->{htmlEvents}->{'#slide'} }, {
224 startTime => $slide_t[$_],
225 endTime => $slide_t[$_ + 1] || $prop->{length},
226 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
230 my @slides_hires = glob "$media_dir/s/hires/*";
232 my $path = "$media_dir/s";
235 warn "create slides images in $path";
239 foreach my $hires ( @slides_hires ) {
241 my $im = Graphics::Magick->new;
242 $im->ReadImage( $hires );
244 my ($slide_width, $slide_height) = Graphics::Magick->new->Ping( $hires );
245 my $slide_aspect = $slide_width / $slide_height;
247 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
250 foreach my $factor ( 1, 2, 4 ) {
252 mkdir "$path/$factor" unless -e "$path/$factor";
254 my $file = slide_jpg( $factor => $nr );
257 my $w = $prop->{height} / $factor * $slide_aspect;
258 my $h = $prop->{height} / $factor;
260 warn "slide [$nr] $hires -> ${w}x${h} $file\n";
262 $im->Resize( width => $w, height => $h, filter => 13, blur => 0.9 );
266 font => 'Sans', pointsize => $h / 10,
267 #text => "$factor = $w*$h",
272 #warn "# info ", dump %info;
273 #warn dump $im->QueryFontMetrics( %info );
274 my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = $im->QueryFontMetrics( %info );
275 my $background = Graphics::Magick->new( size => $width . 'x' . $height );
276 $background->ReadImage( 'xc:black' );
277 $im->Composite( image => $background, compose => 'Over', x => $c, y => $c, opacity => 75 );
278 $info{y} += $ascender;
279 $im->Annotate( fill => 'yellow', %info );
280 $im->Write( filename => $file );
285 my $slide_overview = Graphics::Magick->new;
289 my $n_a = $1 if $a =~ m{(\d+)};
290 my $n_b = $1 if $b =~ m{(\d+)};
291 $n_a <=> $n_b || $a cmp $b
292 } glob("$media_dir/s/2/*")
294 my $s = Graphics::Magick->new;
295 $s->ReadImage( $slide );
296 push @$slide_overview, $s;
299 my $montage = $slide_overview->Montage(
300 geometry => "320x200+5+5>",
303 background => '#ffffff',
304 font => 'Sans', pointsize => 18, fill => '#ff0', stroke => 'none',
307 $montage->Set( matte => 'false' );
308 $montage->Write( filename => "$media_dir/s/overview.jpg" );
309 # $montage->Write('win:'); # blocks
311 my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
317 width => $slide_width,
318 height => $slide_height,
322 $html5tv->{html}->{video_tags} =
329 push @{ $html5tv->{sync}->{customEvents} }, {
330 startTime => $s->[0],
332 action => 'additional_video',
336 description => $s->[2],
337 src => "$media_dir/s/1/00000001.jpg",
343 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
344 <source src="$media_dir/$_->[3]" />
351 sub customEvents_sorted {
353 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
355 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
359 sort { $a->{startTime} <=> $b->{startTime} }
360 @{ $html5tv->{sync}->{customEvents} }
365 $_->{args}->{index} = $index++ foreach customEvents_sorted;
367 warn "last customEvent $index\n";
369 $html5tv->{html}->{subtitles_table}
370 = qq|<table id="subtitles">|
373 my $s = fmt_mmss( $_->{startTime} );
374 my $e = fmt_mmss( $_->{endTime} );
376 <tr id="sub_$_->{index}">
377 <td class="seek_video"><a href="#$_->{startTime}">$s</a></td>
378 <td class="seek_video"><a href="#$_->{endTime}">$e</a></td>
379 <td>$_->{args}->{title}</td>
385 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
388 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
389 my $hcal_path = "$media_dir/hCalendar.html";
390 if ( -e $hcal_path ) {
391 $html5tv->{hCalendar} = read_file $hcal_path;
392 my $tree = HTML::TreeBuilder->new;
393 $tree->parse_file($hcal_path);
394 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
395 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
399 warn "# html5tv ", dump $html5tv if $debug;
401 my $sync_path = "$media_dir/video.js";
402 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
403 warn "sync $sync_path ", -s $sync_path, " bytes\n";
405 my $html = read_file 'www/tv.html';
406 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
407 warn "no interpolation in template!";
409 write_file "www/_editing.html", $html;
410 $html =~ s{media/_editing}{media/$media_part}gs;
411 write_file "www/$media_part.html", $html;
413 my $carousel_width = $prop->{width} + $slide_width - 80;
414 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
415 my $carousel_height = $slide_height + 2;
417 write_file "$media_dir/video.css", qq|
419 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
420 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
421 width: ${carousel_width}px;
422 height: ${carousel_height}px;
425 .jcarousel-skin-ie7 .jcarousel-item {
426 width: ${slide_width}px;
427 height: ${slide_height}px;
432 background-color: #ffc;
436 width: $prop->{width}px;
437 height: $prop->{height}px;
438 font-family: Arial, Helvetica, sans-serif;
439 margin: 0 10px 0px 0;
453 padding: 1px 10px 5px 10px ;
456 width: $html5tv->{video}->{width}px;
461 .jcarousel-skin-ie7 .jcarousel-item img:hover {
462 // border-color: #555 !important;
465 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
466 visibility: visible !important;
469 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
475 font-family: Arial, Verdana;
482 padding: 2px 0 2px 0;
483 width: ${slide_width}px;
488 font-family: monospace;
499 my $hh = int( $t / 3600 );
501 my $mm = int( $t / 60 );
503 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
514 foreach my $s ( @subtitles ) {
516 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
522 write_file $subtitles, $srt;
523 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
525 print $to_mplayer "sub_remove\n";
526 print $to_mplayer qq|sub_load "$subtitles"\n|;
527 print $to_mplayer "sub_select 1\n";
531 if ( ! -e "$subtitles.yaml" ) {
532 warn "no subtitles $subtitles to load\n";
535 @subtitles = YAML::LoadFile "$subtitles.yaml";
536 warn "subtitles ", dump @subtitles;
541 print $to_mplayer qq|pause\n|;
543 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
548 print $to_mplayer qq|pause\n|;
552 warn "subtitles ", dump( @subtitles );
555 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
561 preroll $subtitles[ $#subtitles ]->[0], $line;
565 print $to_mplayer qq|get_property time_pos\n|;
566 my $pos = <$from_mplayer>;
567 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
568 warn "# time_pos $1\n";
575 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
580 my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
581 warn "<<<< subtitle ", sub_fmt $s;
582 preroll $s->[0], $s->[2];
583 # print $to_mplayer "set_property time_pos $s->[0]\n";
588 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
589 warn ">>>> subtitle ", sub_fmt $s;
590 preroll $s->[0], $s->[2];
591 # print $to_mplayer "set_property time_pos $s->[0]\n";
594 sub current_subtitle {
595 my $callback = shift;
598 foreach my $nr ( 0 .. $#subtitles ) {
599 my $s = $subtitles[$nr];
600 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
601 warn sub_fmt $s, $pos;
603 $callback->( $visible, $pos ) if $callback;
604 return ( $visible, $pos );
607 warn "# $pos no visible subtitle\n";
612 current_subtitle( sub {
613 my ( $nr, $pos ) = @_;
614 my $new_start = $subtitles[$nr]->[0] += $offset;
615 warn "subtitle $nr $pos $offset $new_start\n";
617 preroll $new_start, "$pos $offset $new_start";
623 # XXX main epool loop
628 my @fd_selected = $select->can_read(1);
630 foreach my $fileno ( @fd_selected ) {
632 if ( $fileno == $from_mplayer ) {
634 my $line = <$from_mplayer>;
636 exit if $line =~ m{Exiting};
638 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
640 warn "prop $1 = $2\n";
641 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
643 # XXX keyboard shortcuts
646 : $1 eq ',' ? add_subtitle
647 : $1 eq 'F1' ? prev_subtitle
648 : $1 eq 'F2' ? move_subtitle( -0.3 )
649 : $1 eq 'F3' ? move_subtitle( +0.3 )
650 : $1 eq 'F4' ? next_subtitle
651 : $1 eq 'F5' ? save_subtitles
652 : $1 eq 'F9' ? add_subtitle
653 : $1 eq 'F12' ? edit_subtitles
657 } elsif ( $line =~ m{EDL}i ) {
659 print $to_mplayer qq|osd_show_text "$line"\n|;
661 if ( my $pos = time_pos ) {
662 if ( $line =~ m{start}i ) {
663 push @subtitles, [ $pos, $pos, '-' ];
665 $subtitles[ $#subtitles ]->[1] = $pos;
668 } elsif ( $line =~ m{(shot\d+.png)} ) {
671 warn "shot $t $shot\n";
673 my @existing_slides = glob("$media_dir/s/hires/*");
674 my $nr = $#existing_slides + 2;
676 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
678 warn "slide $nr from video $t file $shot\n";
685 die "invalid fileno $fileno";