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 $epfd = epoll_create(10);
75 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
76 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
77 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
82 warn "$movie ", -s $movie, " bytes $edl\n";
83 print $to_mplayer qq|loadfile "$movie"\n|;
84 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
89 my $term_id = `xdotool getwindowfocus`;
94 $mplayer_id ||= `xdotool search mplayer`;
95 warn "focus_mplayer $mplayer_id\n";
96 system "xdotool windowactivate $mplayer_id"
100 warn "focus_term $term_id\n";
101 system "xdotool windowactivate $term_id";
105 my ( $pos, $osd ) = @_;
107 warn "PREROLL $pos $osd\n";
108 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
109 my $to = $pos - $preroll;
111 print $to_mplayer "set_property time_pos $to\n";
112 print $to_mplayer "get_property time_pos\n";
113 print $to_mplayer "play\n";
126 print $to_mplayer $cmd;
133 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
139 my $t = join(',', @_);
140 system "oggThumb -t $t -o jpg -n $file $video";
145 return sprintf('%02d:%02d', int($t/60), int($t%60));
150 if ( ! $prop->{width} || ! $prop->{height} ) {
151 warn "SKIP no size yet\n";
155 if ( ! @subtitles ) {
156 warn "SKIP no subtitles yet\n";
168 foreach my $s ( @subtitles ) {
169 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
170 startTime => $s->[0],
175 if ( $s->[2] =~ m{video:(.+)} ) {
177 my $path = "$media_dir/$video";
179 warn "MISSING $path: $!\n";
181 my $frame_dir = "$media_dir/s/$video";
182 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
184 push @videos, [ @$s, $video ];
186 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
187 push @frames, [ $2, $1 ];
191 next unless $s->[2] =~ m{\[(\d+)\]};
194 push @{ $sync->{customEvents} }, {
195 startTime => $s->[0],
197 action => 'chapterChange',
199 carousel => 'theCarousel',
202 description => $s->[2],
203 src => slide_jpg( 4 => $1 ),
208 push @slide_t, $s->[0];
212 my $hires = "$media_dir/s/hires";
213 mkdir $hires unless -e $hires;
214 oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
216 foreach my $i ( 0 .. $#frames ) {
217 my $from = "$hires/.f$i.jpg";
218 my $to = "$hires/f" . $frames[$i]->[1] . '.jpg';
219 rename $from, $to || warn "can't rename $from -> $to: $!";
223 foreach ( 0 .. $#slide_t ) {
224 my $slide_nr = $_ + 1;
225 push @{ $sync->{htmlEvents}->{'#slide'} }, {
226 startTime => $slide_t[$_],
227 endTime => $slide_t[$_ + 1] || $prop->{length},
228 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
232 my @slides_hires = glob "$media_dir/s/hires/*";
234 my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( $slides_hires[0] );
235 my $slide_aspect = $slide_width / $slide_height;
237 foreach my $factor ( 4, 2, 1 ) {
238 my $w = $prop->{height} / $factor * $slide_aspect;
239 my $h = $prop->{height} / $factor;
241 my $path = "$media_dir/s";
243 warn "create slides imaes in $path";
247 $path .= '/' . $factor;
251 warn "created $path\n";
255 foreach my $hires ( @slides_hires ) {
257 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
259 my $file = slide_jpg( $factor => $nr );
261 warn "slide $hires -> $file\n";
263 my $im = Graphics::Magick->new;
264 $im->ReadImage( $hires );
265 $im->Resize( width => $w, height => $h, filter => 13, blur => 0.9 );
268 font => 'Sans', pointsize => $h / 10,
269 #text => "$factor = $w*$h",
274 #warn "# info ", dump %info;
275 #warn dump $im->QueryFontMetrics( %info );
276 my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = $im->QueryFontMetrics( %info );
277 my $background = Graphics::Magick->new( size => $width . 'x' . $height );
278 $background->ReadImage( 'xc:black' );
279 $im->Composite( image => $background, compose => 'Over', x => $c, y => $c, opacity => 75 );
280 $info{y} += $ascender;
281 $im->Annotate( fill => 'yellow', %info );
282 $im->Write( filename => $file );
287 my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
289 $slide_width ||= $prop->{width} / $slide_factor;
290 $slide_height ||= $prop->{height} / $slide_factor;
296 width => $slide_width,
297 height => $slide_height,
301 $html5tv->{html}->{video_tags} =
308 push @{ $html5tv->{sync}->{customEvents} }, {
309 startTime => $s->[0],
311 action => 'additional_video',
315 description => $s->[2],
316 src => "$media_dir/s/1/00000001.jpg",
322 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
323 <source src="$media_dir/$_->[3]" />
330 sub customEvents_sorted {
332 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
334 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
338 sort { $a->{startTime} <=> $b->{startTime} }
339 @{ $html5tv->{sync}->{customEvents} }
344 $_->{args}->{index} = $index++ foreach customEvents_sorted;
346 warn "last customEvent $index\n";
348 $html5tv->{html}->{subtitles_table}
349 = qq|<table id="subtitles">|
352 my $s = fmt_mmss( $_->{startTime} );
353 my $e = fmt_mmss( $_->{endTime} );
355 <tr id="sub_$_->{index}">
356 <td class="seek_video"><a href="#$_->{startTime}">$s</a></td>
357 <td class="seek_video"><a href="#$_->{endTime}">$e</a></td>
358 <td>$_->{args}->{title}</td>
364 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
367 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
368 my $hcal_path = "$media_dir/hCalendar.html";
369 if ( -e $hcal_path ) {
370 $html5tv->{hCalendar} = read_file $hcal_path;
371 my $tree = HTML::TreeBuilder->new;
372 $tree->parse_file($hcal_path);
373 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
374 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
378 warn "# html5tv ", dump $html5tv if $debug;
380 my $sync_path = "$media_dir/video.js";
381 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
382 warn "sync $sync_path ", -s $sync_path, " bytes\n";
384 my $html = read_file 'www/tv.html';
385 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
386 warn "no interpolation in template!";
388 write_file "www/_editing.html", $html;
389 $html =~ s{media/_editing}{media/$media_part}gs;
390 write_file "www/$media_part.html", $html;
392 my $carousel_width = $prop->{width} + $slide_width - 80;
393 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
394 my $carousel_height = $slide_height + 2;
396 write_file "$media_dir/video.css", qq|
398 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
399 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
400 width: ${carousel_width}px;
401 height: ${carousel_height}px;
404 .jcarousel-skin-ie7 .jcarousel-item {
405 width: ${slide_width}px;
406 height: ${slide_height}px;
411 background-color: #ffc;
415 width: $prop->{width}px;
416 height: $prop->{height}px;
417 font-family: Arial, Helvetica, sans-serif;
418 margin: 0 10px 0px 0;
432 padding: 1px 10px 5px 10px ;
435 width: $html5tv->{video}->{width}px;
440 .jcarousel-skin-ie7 .jcarousel-item img:hover {
441 // border-color: #555 !important;
444 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
445 visibility: visible !important;
448 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
454 font-family: Arial, Verdana;
461 padding: 2px 0 2px 0;
462 width: ${slide_width}px;
467 font-family: monospace;
478 my $hh = int( $t / 3600 );
480 my $mm = int( $t / 60 );
482 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
493 foreach my $s ( @subtitles ) {
495 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
501 write_file $subtitles, $srt;
502 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
504 print $to_mplayer "sub_remove\n";
505 print $to_mplayer qq|sub_load "$subtitles"\n|;
506 print $to_mplayer "sub_select 1\n";
510 if ( ! -e "$subtitles.yaml" ) {
511 warn "no subtitles $subtitles to load\n";
514 @subtitles = YAML::LoadFile "$subtitles.yaml";
515 warn "subtitles ", dump @subtitles;
520 print $to_mplayer qq|pause\n|;
522 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
527 print $to_mplayer qq|pause\n|;
531 warn "subtitles ", dump( @subtitles );
534 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
540 preroll $subtitles[ $#subtitles ]->[0], $line;
544 print $to_mplayer qq|get_property time_pos\n|;
545 my $pos = <$from_mplayer>;
546 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
547 warn "# time_pos $1\n";
554 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
559 my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
560 warn "<<<< subtitle ", sub_fmt $s;
561 preroll $s->[0], $s->[2];
562 # print $to_mplayer "set_property time_pos $s->[0]\n";
567 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
568 warn ">>>> subtitle ", sub_fmt $s;
569 preroll $s->[0], $s->[2];
570 # print $to_mplayer "set_property time_pos $s->[0]\n";
573 sub current_subtitle {
574 my $callback = shift;
577 foreach my $nr ( 0 .. $#subtitles ) {
578 my $s = $subtitles[$nr];
579 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
580 warn sub_fmt $s, $pos;
582 $callback->( $visible, $pos ) if $callback;
583 return ( $visible, $pos );
586 warn "# $pos no visible subtitle\n";
591 current_subtitle( sub {
592 my ( $nr, $pos ) = @_;
593 my $new_start = $subtitles[$nr]->[0] += $offset;
594 warn "subtitle $nr $pos $offset $new_start\n";
596 preroll $new_start, "$pos $offset $new_start";
602 # XXX main epool loop
606 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
608 warn "no events" unless $events;
610 foreach my $e ( @$events ) {
611 # warn "# event: ", dump($e), $/;
613 my $fileno = $e->[0];
615 if ( $fileno == fileno STDIN ) {
617 sysread STDIN, $chr, 1;
619 } elsif ( $fileno == fileno $from_mplayer ) {
621 sysread $from_mplayer, $chr, 1;
624 if ( $chr =~ m{[\n\r]} ) {
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";
678 } elsif ( $fileno == fileno $to_mplayer ) {
681 die "invalid fileno $fileno";