6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
17 my $debug = $ENV{DEBUG} || 0;
19 my $movie = shift @ARGV;
21 sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
22 sub base_name { $1 if $_[0] =~ m{^.+/([^/]+)$} }
24 if ( ! $movie && -e 'media/_editing' ) {
25 $movie = 'media/' . readlink('media/_editing') . '/video.ogv';
26 warn "using media/_editing -> $movie\n";
27 } elsif ( -d $movie && $movie =~ m{media/} ) {
28 $movie .= '/video.ogv';
29 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
30 my $movie_master = $movie;
31 $movie = base_dir($movie) . '/video.ogv';
33 symlink base_name($movie_master), $movie;
34 warn "symlink video.ogv -> $movie_master";
36 warn "using symlink video.ogv -> ", readlink $movie;
38 } elsif ( -f $movie ) {
39 warn "using video $movie";
41 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
44 my $media_part = my $media_dir = base_dir($movie);
45 $media_part =~ s{media/}{};
46 $media_part =~ s{/$}{};
48 unlink 'media/_editing';
49 symlink $media_part, 'media/_editing';
51 warn "# media_part $media_part\n";
53 my $edl = "/dev/shm/edl";
54 my $subtitles = $movie;
55 $subtitles =~ s{\.\w+$}{.srt};
59 my $slide_factor = 4; # 1/4 size of video
66 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
72 '-vf' => 'screenshot',
75 my $select = IO::Select->new();
76 #$select->add( \*STDIN );
77 $select->add( $from_mplayer );
82 warn "$movie ", -s $movie, " bytes $edl\n";
83 print $to_mplayer qq|loadfile "$movie"\n|;
88 my $term_id = `xdotool getwindowfocus`;
93 $mplayer_id ||= `xdotool search mplayer`;
94 warn "focus_mplayer $mplayer_id\n";
95 system "xdotool windowactivate $mplayer_id"
99 warn "focus_term $term_id\n";
100 system "xdotool windowactivate $term_id";
104 my ( $pos, $osd ) = @_;
106 warn "PREROLL $pos $osd\n";
107 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
108 my $to = $pos - $preroll;
110 print $to_mplayer "set_property time_pos $to\n";
111 print $to_mplayer "get_property time_pos\n";
112 print $to_mplayer "play\n";
125 print $to_mplayer $cmd;
132 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
138 my $t = join(',', @_);
139 system "oggThumb -t $t -o jpg -n $file $video";
144 return sprintf('%02d:%02d', int($t/60), int($t%60));
149 if ( ! $prop->{width} || ! $prop->{height} ) {
150 warn "SKIP no size yet\n";
154 if ( ! @subtitles ) {
155 warn "SKIP no subtitles yet\n";
167 foreach my $s ( @subtitles ) {
168 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
169 startTime => $s->[0],
174 if ( $s->[2] =~ m{video:(.+)} ) {
176 my $path = "$media_dir/$video";
178 warn "MISSING $path: $!\n";
180 my $frame_dir = "$media_dir/s/$video";
181 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
183 push @videos, [ @$s, $video ];
185 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
186 push @frames, [ $2, $1 ];
190 next unless $s->[2] =~ m{\[(\d+)\]};
193 push @{ $sync->{customEvents} }, {
194 startTime => $s->[0],
196 action => 'chapterChange',
198 carousel => 'theCarousel',
201 description => $s->[2],
202 src => slide_jpg( 4 => $1 ),
207 push @slide_t, $s->[0];
211 my $hires = "$media_dir/s/hires";
212 mkdir $hires unless -e $hires;
213 oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
215 foreach my $i ( 0 .. $#frames ) {
216 my $from = "$hires/.f$i.jpg";
217 my $to = "$hires/f" . $frames[$i]->[1] . '.jpg';
218 rename $from, $to || warn "can't rename $from -> $to: $!";
222 foreach ( 0 .. $#slide_t ) {
223 my $slide_nr = $_ + 1;
224 push @{ $sync->{htmlEvents}->{'#slide'} }, {
225 startTime => $slide_t[$_],
226 endTime => $slide_t[$_ + 1] || $prop->{length},
227 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
231 my @slides_hires = glob "$media_dir/s/hires/*";
233 my $path = "$media_dir/s";
236 warn "create slides images in $path";
240 foreach my $hires ( @slides_hires ) {
242 my $im = Graphics::Magick->new;
243 $im->ReadImage( $hires );
245 my ($slide_width, $slide_height) = Graphics::Magick->new->Ping( $hires );
246 my $slide_aspect = $slide_width / $slide_height;
248 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
251 foreach my $factor ( 1, 2, 4 ) {
253 mkdir "$path/$factor" unless -e "$path/$factor";
255 my $file = slide_jpg( $factor => $nr );
258 my $w = $prop->{height} / $factor * $slide_aspect;
259 my $h = $prop->{height} / $factor;
261 warn "slide [$nr] $hires -> ${w}x${h} $file\n";
263 $im->Resize( width => $w, height => $h, filter => 13, blur => 0.9 );
267 font => 'Sans', pointsize => $h / 10,
268 #text => "$factor = $w*$h",
273 #warn "# info ", dump %info;
274 #warn dump $im->QueryFontMetrics( %info );
275 my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = $im->QueryFontMetrics( %info );
276 my $background = Graphics::Magick->new( size => $width . 'x' . $height );
277 $background->ReadImage( 'xc:black' );
278 $im->Composite( image => $background, compose => 'Over', x => $c, y => $c, opacity => 75 );
279 $info{y} += $ascender;
280 $im->Annotate( fill => 'yellow', %info );
281 $im->Write( filename => $file );
286 my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
292 width => $slide_width,
293 height => $slide_height,
297 $html5tv->{html}->{video_tags} =
304 push @{ $html5tv->{sync}->{customEvents} }, {
305 startTime => $s->[0],
307 action => 'additional_video',
311 description => $s->[2],
312 src => "$media_dir/s/1/00000001.jpg",
318 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
319 <source src="$media_dir/$_->[3]" />
326 sub customEvents_sorted {
328 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
330 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
334 sort { $a->{startTime} <=> $b->{startTime} }
335 @{ $html5tv->{sync}->{customEvents} }
340 $_->{args}->{index} = $index++ foreach customEvents_sorted;
342 warn "last customEvent $index\n";
344 $html5tv->{html}->{subtitles_table}
345 = qq|<table id="subtitles">|
348 my $s = fmt_mmss( $_->{startTime} );
349 my $e = fmt_mmss( $_->{endTime} );
351 <tr id="sub_$_->{index}">
352 <td class="seek_video"><a href="#$_->{startTime}">$s</a></td>
353 <td class="seek_video"><a href="#$_->{endTime}">$e</a></td>
354 <td>$_->{args}->{title}</td>
360 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
363 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
364 my $hcal_path = "$media_dir/hCalendar.html";
365 if ( -e $hcal_path ) {
366 $html5tv->{hCalendar} = read_file $hcal_path;
367 my $tree = HTML::TreeBuilder->new;
368 $tree->parse_file($hcal_path);
369 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
370 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
374 warn "# html5tv ", dump $html5tv if $debug;
376 my $sync_path = "$media_dir/video.js";
377 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
378 warn "sync $sync_path ", -s $sync_path, " bytes\n";
380 my $html = read_file 'www/tv.html';
381 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
382 warn "no interpolation in template!";
384 write_file "www/_editing.html", $html;
385 $html =~ s{media/_editing}{media/$media_part}gs;
386 write_file "www/$media_part.html", $html;
388 my $carousel_width = $prop->{width} + $slide_width - 80;
389 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
390 my $carousel_height = $slide_height + 2;
392 write_file "$media_dir/video.css", qq|
394 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
395 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
396 width: ${carousel_width}px;
397 height: ${carousel_height}px;
400 .jcarousel-skin-ie7 .jcarousel-item {
401 width: ${slide_width}px;
402 height: ${slide_height}px;
407 background-color: #ffc;
411 width: $prop->{width}px;
412 height: $prop->{height}px;
413 font-family: Arial, Helvetica, sans-serif;
414 margin: 0 10px 0px 0;
428 padding: 1px 10px 5px 10px ;
431 width: $html5tv->{video}->{width}px;
436 .jcarousel-skin-ie7 .jcarousel-item img:hover {
437 // border-color: #555 !important;
440 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
441 visibility: visible !important;
444 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
450 font-family: Arial, Verdana;
457 padding: 2px 0 2px 0;
458 width: ${slide_width}px;
463 font-family: monospace;
474 my $hh = int( $t / 3600 );
476 my $mm = int( $t / 60 );
478 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
489 foreach my $s ( @subtitles ) {
491 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
497 write_file $subtitles, $srt;
498 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
500 print $to_mplayer "sub_remove\n";
501 print $to_mplayer qq|sub_load "$subtitles"\n|;
502 print $to_mplayer "sub_select 1\n";
506 if ( ! -e "$subtitles.yaml" ) {
507 warn "no subtitles $subtitles to load\n";
510 @subtitles = YAML::LoadFile "$subtitles.yaml";
511 warn "subtitles ", dump @subtitles;
516 print $to_mplayer qq|pause\n|;
518 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
523 print $to_mplayer qq|pause\n|;
527 warn "subtitles ", dump( @subtitles );
530 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
536 preroll $subtitles[ $#subtitles ]->[0], $line;
540 print $to_mplayer qq|get_property time_pos\n|;
541 my $pos = <$from_mplayer>;
542 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
543 warn "# time_pos $1\n";
550 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
555 my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
556 warn "<<<< subtitle ", sub_fmt $s;
557 preroll $s->[0], $s->[2];
558 # print $to_mplayer "set_property time_pos $s->[0]\n";
563 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
564 warn ">>>> subtitle ", sub_fmt $s;
565 preroll $s->[0], $s->[2];
566 # print $to_mplayer "set_property time_pos $s->[0]\n";
569 sub current_subtitle {
570 my $callback = shift;
573 foreach my $nr ( 0 .. $#subtitles ) {
574 my $s = $subtitles[$nr];
575 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
576 warn sub_fmt $s, $pos;
578 $callback->( $visible, $pos ) if $callback;
579 return ( $visible, $pos );
582 warn "# $pos no visible subtitle\n";
587 current_subtitle( sub {
588 my ( $nr, $pos ) = @_;
589 my $new_start = $subtitles[$nr]->[0] += $offset;
590 warn "subtitle $nr $pos $offset $new_start\n";
592 preroll $new_start, "$pos $offset $new_start";
598 # XXX main epool loop
602 my $slides = HTML5TV::Slides->new(
606 foreach my $s ( @subtitles ) {
607 $nr = $1 if $s->[2] =~ m{\[(\d+)\]} && $s->[0] < $t;
613 print $to_mplayer "get_property $_\n" foreach grep { ! $prop->{$_} } ( qw/metadata video_codec video_bitrate width height fps length/ );
616 my @fd_selected = $select->can_read(1);
618 $slides->show( time_pos ) if ! @fd_selected;
620 foreach my $fileno ( @fd_selected ) {
622 if ( $fileno == $from_mplayer ) {
624 my $line = <$from_mplayer>;
626 exit if $line =~ m{Exiting};
628 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
630 warn "prop $1 = $2\n";
631 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
633 # XXX keyboard shortcuts
636 : $1 eq ',' ? add_subtitle
637 : $1 eq 'F1' ? prev_subtitle
638 : $1 eq 'F2' ? move_subtitle( -0.3 )
639 : $1 eq 'F3' ? move_subtitle( +0.3 )
640 : $1 eq 'F4' ? next_subtitle
641 : $1 eq 'F5' ? save_subtitles
642 : $1 eq 'F9' ? add_subtitle
643 : $1 eq 'F12' ? edit_subtitles
647 } elsif ( $line =~ m{EDL}i ) {
649 print $to_mplayer qq|osd_show_text "$line"\n|;
651 if ( my $pos = time_pos ) {
652 if ( $line =~ m{start}i ) {
653 push @subtitles, [ $pos, $pos, '-' ];
655 $subtitles[ $#subtitles ]->[1] = $pos;
658 } elsif ( $line =~ m{(shot\d+.png)} ) {
661 warn "shot $t $shot\n";
663 my @existing_slides = glob("$media_dir/s/hires/*");
664 my $nr = $#existing_slides + 2;
666 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
668 warn "slide $nr from video $t file $shot\n";
675 die "invalid fileno $fileno";