6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
14 use Time::HiRes qw(time);
19 my $debug = $ENV{DEBUG} || 0;
21 my $movie = shift @ARGV;
23 sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
24 sub base_name { $1 if $_[0] =~ m{^.+/([^/]+)$} }
26 if ( ! $movie && -e 'media/_editing' ) {
27 $movie = 'media/' . readlink('media/_editing') . '/video.ogv';
28 warn "using media/_editing -> $movie\n";
29 } elsif ( -d $movie && $movie =~ m{media/} ) {
30 $movie .= '/video.ogv';
31 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
32 my $movie_master = $movie;
33 $movie = base_dir($movie) . '/video.ogv';
35 symlink base_name($movie_master), $movie;
36 warn "symlink video.ogv -> $movie_master";
38 warn "using symlink video.ogv -> ", readlink $movie;
40 } elsif ( -f $movie ) {
41 warn "using video $movie";
43 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
46 my $media_part = my $media_dir = base_dir($movie);
47 $media_part =~ s{media/}{};
48 $media_part =~ s{/$}{};
50 unlink 'media/_editing';
51 symlink $media_part, 'media/_editing';
53 warn "# media_part $media_part\n";
55 my $edl = "/dev/shm/edl";
56 my $subtitles = $movie;
57 $subtitles =~ s{\.\w+$}{.srt};
61 my $slide_factor = 4; # 1/4 size of video
68 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
74 '-vf' => 'screenshot',
77 my $select = IO::Select->new();
78 #$select->add( \*STDIN );
79 $select->add( $from_mplayer );
84 warn "$movie ", -s $movie, " bytes $edl\n";
85 print $to_mplayer qq|loadfile "$movie"\n|;
90 my $term_id = `xdotool getwindowfocus`;
95 $mplayer_id ||= `xdotool search mplayer`;
96 warn "focus_mplayer $mplayer_id\n";
97 system "xdotool windowactivate $mplayer_id"
101 warn "focus_term $term_id\n";
102 system "xdotool windowactivate $term_id";
106 my ( $pos, $osd ) = @_;
108 warn "PREROLL $pos $osd\n";
109 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
110 my $to = $pos - $preroll;
112 print $to_mplayer "set_property time_pos $to\n";
113 print $to_mplayer "get_property time_pos\n";
114 print $to_mplayer "play\n";
127 print $to_mplayer $cmd;
134 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
140 my $t = join(',', @_);
141 system "oggThumb -t $t -o jpg -n $file $video";
146 return sprintf('%02d:%02d', int($t/60), int($t%60));
151 if ( ! $prop->{width} || ! $prop->{height} ) {
152 warn "SKIP no size yet\n";
156 if ( ! @subtitles ) {
157 warn "SKIP no subtitles yet\n";
169 foreach my $s ( @subtitles ) {
170 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
171 startTime => $s->[0],
176 if ( $s->[2] =~ m{video:(.+)} ) {
178 my $path = "$media_dir/$video";
180 warn "MISSING $path: $!\n";
182 my $frame_dir = "$media_dir/s/$video";
183 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
185 push @videos, [ @$s, $video ];
187 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
188 push @frames, [ $2, $1 ];
192 next unless $s->[2] =~ m{\[(\d+)\]};
195 push @{ $sync->{customEvents} }, {
196 startTime => $s->[0],
198 action => 'chapterChange',
200 carousel => 'theCarousel',
203 description => $s->[2],
204 src => slide_jpg( 4 => $1 ),
209 push @slide_t, $s->[0];
213 my $hires = "$media_dir/s/hires";
214 mkdir $hires unless -e $hires;
215 oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
217 foreach my $i ( 0 .. $#frames ) {
218 my $from = "$hires/.f$i.jpg";
219 my $to = "$hires/f" . $frames[$i]->[1] . '.jpg';
220 rename $from, $to || warn "can't rename $from -> $to: $!";
224 foreach ( 0 .. $#slide_t ) {
225 my $slide_nr = $_ + 1;
226 push @{ $sync->{htmlEvents}->{'#slide'} }, {
227 startTime => $slide_t[$_],
228 endTime => $slide_t[$_ + 1] || $prop->{length},
229 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
233 my @slides_hires = glob "$media_dir/s/hires/*";
235 my $path = "$media_dir/s";
238 warn "create slides images in $path";
242 foreach my $hires ( @slides_hires ) {
244 my $im = Graphics::Magick->new;
245 $im->ReadImage( $hires );
247 my ($slide_width, $slide_height) = Graphics::Magick->new->Ping( $hires );
248 my $slide_aspect = $slide_width / $slide_height;
250 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
253 foreach my $factor ( 1, 2, 4 ) {
255 mkdir "$path/$factor" unless -e "$path/$factor";
257 my $file = slide_jpg( $factor => $nr );
260 my $w = $prop->{height} / $factor * $slide_aspect;
261 my $h = $prop->{height} / $factor;
263 warn "slide [$nr] $hires -> ${w}x${h} $file\n";
265 $im->Resize( width => $w, height => $h, filter => 13, blur => 0.9 );
269 font => 'Sans', pointsize => $h / 10,
270 #text => "$factor = $w*$h",
275 #warn "# info ", dump %info;
276 #warn dump $im->QueryFontMetrics( %info );
277 my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = $im->QueryFontMetrics( %info );
278 my $background = Graphics::Magick->new( size => $width . 'x' . $height );
279 $background->ReadImage( 'xc:black' );
280 $im->Composite( image => $background, compose => 'Over', x => $c, y => $c, opacity => 75 );
281 $info{y} += $ascender;
282 $im->Annotate( fill => 'yellow', %info );
283 $im->Write( filename => $file );
288 my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
294 width => $slide_width,
295 height => $slide_height,
299 $html5tv->{html}->{video_tags} =
306 push @{ $html5tv->{sync}->{customEvents} }, {
307 startTime => $s->[0],
309 action => 'additional_video',
313 description => $s->[2],
314 src => "$media_dir/s/1/00000001.jpg",
320 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
321 <source src="$media_dir/$_->[3]" />
328 sub customEvents_sorted {
330 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
332 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
336 sort { $a->{startTime} <=> $b->{startTime} }
337 @{ $html5tv->{sync}->{customEvents} }
342 $_->{args}->{index} = $index++ foreach customEvents_sorted;
344 warn "last customEvent $index\n";
346 $html5tv->{html}->{subtitles_table}
347 = qq|<table id="subtitles">|
350 my $s = fmt_mmss( $_->{startTime} );
351 my $e = fmt_mmss( $_->{endTime} );
353 <tr id="sub_$_->{index}">
354 <td class="seek_video"><a href="#$_->{startTime}">$s</a></td>
355 <td class="seek_video"><a href="#$_->{endTime}">$e</a></td>
356 <td>$_->{args}->{title}</td>
362 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
365 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
366 my $hcal_path = "$media_dir/hCalendar.html";
367 if ( -e $hcal_path ) {
368 $html5tv->{hCalendar} = read_file $hcal_path;
369 my $tree = HTML::TreeBuilder->new;
370 $tree->parse_file($hcal_path);
371 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
372 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
376 warn "# html5tv ", dump $html5tv if $debug;
378 my $sync_path = "$media_dir/video.js";
379 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
380 warn "sync $sync_path ", -s $sync_path, " bytes\n";
382 my $html = read_file 'www/tv.html';
383 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
384 warn "no interpolation in template!";
386 write_file "www/_editing.html", $html;
387 $html =~ s{media/_editing}{media/$media_part}gs;
388 write_file "www/$media_part.html", $html;
390 my $carousel_width = $prop->{width} + $slide_width - 80;
391 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
392 my $carousel_height = $slide_height + 2;
394 write_file "$media_dir/video.css", qq|
396 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
397 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
398 width: ${carousel_width}px;
399 height: ${carousel_height}px;
402 .jcarousel-skin-ie7 .jcarousel-item {
403 width: ${slide_width}px;
404 height: ${slide_height}px;
409 background-color: #ffc;
413 width: $prop->{width}px;
414 height: $prop->{height}px;
415 font-family: Arial, Helvetica, sans-serif;
416 margin: 0 10px 0px 0;
430 padding: 1px 10px 5px 10px ;
433 width: $html5tv->{video}->{width}px;
438 .jcarousel-skin-ie7 .jcarousel-item img:hover {
439 // border-color: #555 !important;
442 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
443 visibility: visible !important;
446 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
452 font-family: Arial, Verdana;
459 padding: 2px 0 2px 0;
460 width: ${slide_width}px;
465 font-family: monospace;
476 my $hh = int( $t / 3600 );
478 my $mm = int( $t / 60 );
480 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
491 foreach my $s ( @subtitles ) {
493 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
499 write_file $subtitles, $srt;
500 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
502 print $to_mplayer "sub_remove\n";
503 print $to_mplayer qq|sub_load "$subtitles"\n|;
504 print $to_mplayer "sub_select 1\n";
508 if ( ! -e "$subtitles.yaml" ) {
509 warn "no subtitles $subtitles to load\n";
512 @subtitles = YAML::LoadFile "$subtitles.yaml";
513 warn "subtitles ", dump @subtitles;
518 print $to_mplayer qq|pause\n|;
520 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
525 print $to_mplayer qq|pause\n|;
529 warn "subtitles ", dump( @subtitles );
532 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
538 preroll $subtitles[ $#subtitles ]->[0], $line;
542 print $to_mplayer qq|get_property time_pos\n|;
543 my $pos = <$from_mplayer>;
544 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
545 warn "# time_pos $1\n";
552 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
557 my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
558 warn "<<<< subtitle ", sub_fmt $s;
559 preroll $s->[0], $s->[2];
560 # print $to_mplayer "set_property time_pos $s->[0]\n";
565 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
566 warn ">>>> subtitle ", sub_fmt $s;
567 preroll $s->[0], $s->[2];
568 # print $to_mplayer "set_property time_pos $s->[0]\n";
571 sub current_subtitle {
572 my $callback = shift;
575 foreach my $nr ( 0 .. $#subtitles ) {
576 my $s = $subtitles[$nr];
577 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
578 warn sub_fmt $s, $pos;
580 $callback->( $visible, $pos ) if $callback;
581 return ( $visible, $pos );
584 warn "# $pos no visible subtitle\n";
589 current_subtitle( sub {
590 my ( $nr, $pos ) = @_;
591 my $new_start = $subtitles[$nr]->[0] += $offset;
592 warn "subtitle $nr $pos $offset $new_start\n";
594 preroll $new_start, "$pos $offset $new_start";
600 # XXX main epool loop
604 my $slides = HTML5TV::Slides->new(
608 foreach my $s ( @subtitles ) {
609 $nr = $1 if $s->[2] =~ m{\[(\d+)\]} && $s->[0] < $t;
615 print $to_mplayer "get_property $_\n" foreach grep { ! $prop->{$_} } ( qw/metadata video_codec video_bitrate width height fps length/ );
620 my @fd_selected = $select->can_read(0.3);
624 if ( ! @fd_selected && $dt > 1.5 ) {
625 my $pos = time_pos - $dt;
626 $slides->show( $pos );
630 foreach my $fileno ( @fd_selected ) {
632 if ( $fileno == $from_mplayer ) {
634 my $line = <$from_mplayer>;
638 exit if $line =~ m{Exiting};
640 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
642 warn "prop $1 = $2\n";
643 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
645 # XXX keyboard shortcuts
648 : $1 eq ',' ? add_subtitle
649 : $1 eq 'F1' ? prev_subtitle
650 : $1 eq 'F2' ? move_subtitle( -0.3 )
651 : $1 eq 'F3' ? move_subtitle( +0.3 )
652 : $1 eq 'F4' ? next_subtitle
653 : $1 eq 'F5' ? save_subtitles
654 : $1 eq 'F9' ? add_subtitle
655 : $1 eq 'F12' ? edit_subtitles
659 } elsif ( $line =~ m{EDL}i ) {
661 print $to_mplayer qq|osd_show_text "$line"\n|;
663 if ( my $pos = time_pos ) {
664 if ( $line =~ m{start}i ) {
665 push @subtitles, [ $pos, $pos, '-' ];
667 $subtitles[ $#subtitles ]->[1] = $pos;
670 } elsif ( $line =~ m{(shot\d+.png)} ) {
673 warn "shot $t $shot\n";
675 my @existing_slides = glob("$media_dir/s/hires/*");
676 my $nr = $#existing_slides + 2;
678 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
680 warn "slide $nr from video $t file $shot\n";
687 die "invalid fileno $fileno";