6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
16 my $movie = shift @ARGV;
18 sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
20 if ( ! $movie && -e 'media/editing' ) {
21 $movie = 'media/' . readlink('media/editing') . '/video.ogv';
22 warn "using media/editing -> $movie\n";
23 } elsif ( -d $movie && $movie =~ m{media/} ) {
24 $movie .= '/video.ogv';
25 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
26 my $movie_master = $movie;
27 my $to = $movie = base_dir($movie) . '/video.ogv';
29 symlink $movie_master, $to;
30 warn "symlink $to -> $movie\n";
31 } elsif ( -f $movie ) {
32 warn "using video $movie";
34 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
37 my $media_part = my $media_dir = base_dir($movie);
38 $media_part =~ s{media/}{};
40 unlink 'media/editing';
41 symlink $media_part, 'media/editing';
43 warn "# media_part $media_part\n";
45 my $edl = "/dev/shm/edl";
46 my $subtitles = $movie;
47 $subtitles =~ s{\.\w+$}{.srt};
51 my $slide_factor = 4; # 1/4 size of video
58 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
64 '-vf' => 'screenshot',
67 my $epfd = epoll_create(10);
69 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
70 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
71 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
76 warn "$movie ", -s $movie, " bytes $edl\n";
77 print $to_mplayer qq|loadfile "$movie"\n|;
78 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
83 my $term_id = `xdotool getwindowfocus`;
88 $mplayer_id ||= `xdotool search mplayer`;
89 warn "focus_mplayer $mplayer_id\n";
90 system "xdotool windowactivate $mplayer_id"
94 warn "focus_term $term_id\n";
95 system "xdotool windowactivate $term_id";
99 my ( $pos, $osd ) = @_;
101 warn "PREROLL $pos $osd\n";
102 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
103 my $to = $pos - $preroll;
105 print $to_mplayer "set_property time_pos $to\n";
106 print $to_mplayer "get_property time_pos\n";
107 print $to_mplayer "play\n";
120 print $to_mplayer $cmd;
128 if ( ! $prop->{width} || ! $prop->{height} ) {
129 warn "SKIP no size yet\n";
133 if ( ! @subtitles ) {
134 warn "SKIP no subtitles yet\n";
145 foreach my $s ( @subtitles ) {
146 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
147 startTime => $s->[0],
152 if ( $s->[2] =~ m{video:(.+)} ) {
154 my $path = "$media_dir/$video";
156 warn "MISSING $path: $!\n";
158 my $frame_dir = "$media_dir/s/$video";
159 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
161 push @videos, [ @$s, $video ];
165 next unless $s->[2] =~ m{\[(\d+)\]};
168 push @{ $sync->{customEvents} }, {
169 startTime => $s->[0],
171 action => 'chapterChange',
173 carousel => 'theCarousel',
176 description => $s->[2],
177 src => sprintf('%s/s/%dx%d/p%03d.jpg', $media_dir, $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
182 push @slide_t, $s->[0];
185 my $res = $prop->{width} . 'x' . $prop->{height};
187 foreach ( 0 .. $#slide_t ) {
188 push @{ $sync->{htmlEvents}->{'#slide'} }, {
189 startTime => $slide_t[$_],
190 endTime => $slide_t[$_ + 1] || $prop->{length},
191 html => sprintf( '<img src=%s/s/%s/p%03d.jpg>', $media_dir, $res, $_ + 1 ),
195 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
196 @slides_hires = glob 'shot*.png' unless @slides_hires;
199 foreach my $factor ( 4, 2, 1 ) {
200 my $w = $prop->{width} / $factor;
201 my $h = $prop->{height} / $factor;
203 my $path = "$media_dir/s/${w}x${h}";
204 $factor_s_path->{$factor} = $path;
208 warn "created $path\n";
210 foreach my $hires ( @slides_hires ) {
213 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
215 my $im = Imager->new( file => $hires );
216 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
217 warn "resized $file ", -s $file, " bytes\n";
223 my ( $slide_width, $slide_height );
225 my $im = Imager->new( file => $factor_s_path->{ 1 } . '/p001.jpg"')
226 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
230 $slide_width = $im->getwidth / $slide_factor;
231 $slide_height = $im->getheight / $slide_factor;
233 warn "can't find first slide default to 1/$slide_factor of video size\n";
234 $slide_width = $prop->{width} / $slide_factor;
235 $slide_height = $prop->{height} / $slide_factor;
242 width => $slide_width,
243 height => $slide_height,
247 $html5tv->{video_tags} =
254 push @{ $html5tv->{sync}->{customEvents} }, {
255 startTime => $s->[0],
257 action => 'additional_video',
261 description => $s->[2],
262 src => "$media_dir/s/$s->[3]/00000001.jpg",
268 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
269 <source src="$media_dir/$_->[3]" />
276 sub customEvents_sorted {
278 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
280 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
284 sort { $a->{startTime} <=> $b->{startTime} }
285 @{ $html5tv->{sync}->{customEvents} }
290 $_->{args}->{index} = $index++ foreach customEvents_sorted;
292 warn "last customEvent $index\n";
294 $html5tv->{subtitles_table}
295 = qq|<table id="subtitles">|
298 <tr id="sub_$_->{index}">
299 <td class="seek_video">$_->{startTime}</td>
300 <td class="seek_video">$_->{endTime}</td>
301 <td>$_->{args}->{title}</td>
306 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
309 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
310 my $hcal_path = '$media_dir/hcalendar.html';
311 if ( -e $hcal_path ) {
312 $html5tv->{hcalendar} = read_file $hcal_path;
313 my $tree = HTML::TreeBuilder->new;
314 $tree->parse_file($hcal_path);
315 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
316 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
320 warn "# html5tv ", dump $html5tv;
322 my $sync_path = "$media_dir/video.js";
323 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
324 warn "sync $sync_path ", -s $sync_path, " bytes\n";
326 my $html = read_file 'www/tv.html';
327 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
328 warn "no interpolation in template!";
330 write_file "www/editing.html", $html;
331 $html =~ s{media/editing}{media/$media_part}gs;
332 write_file "www/$media_part.html", $html;
334 my $carousel_width = $prop->{width} + $slide_width - 80;
335 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
336 my $carousel_height = $slide_height + 2;
338 write_file "$media_dir/video.css", qq|
340 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
341 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
342 width: ${carousel_width}px;
343 height: ${carousel_height}px;
346 .jcarousel-skin-ie7 .jcarousel-item {
347 width: ${slide_width}px;
348 height: ${slide_height}px;
353 background-color: #ffc;
357 width: $prop->{width}px;
358 height: $prop->{height}px;
359 font-family: Arial, Helvetica, sans-serif;
360 margin: 0 10px 0px 0;
374 padding: 1px 10px 5px 10px ;
377 width: $html5tv->{video}->{width}px;
382 .jcarousel-skin-ie7 .jcarousel-item img:hover {
383 // border-color: #555 !important;
386 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
387 visibility: visible !important;
390 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
396 font-family: Arial, Verdana;
403 padding: 2px 0 2px 0;
404 width: ${slide_width}px;
409 font-family: monospace;
420 my $hh = int( $t / 3600 );
422 my $mm = int( $t / 60 );
424 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
435 foreach my $s ( @subtitles ) {
437 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
443 write_file $subtitles, $srt;
444 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
446 print $to_mplayer "sub_remove\n";
447 print $to_mplayer qq|sub_load "$subtitles"\n|;
448 print $to_mplayer "sub_select 1\n";
452 if ( ! -e "$subtitles.yaml" ) {
453 warn "no subtitles $subtitles to load\n";
456 @subtitles = YAML::LoadFile "$subtitles.yaml";
457 warn "subtitles ", dump @subtitles;
462 print $to_mplayer qq|pause\n|;
464 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
469 print $to_mplayer qq|pause\n|;
473 warn "subtitles ", dump( @subtitles );
476 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
482 preroll $subtitles[ $#subtitles ]->[0], $line;
486 print $to_mplayer qq|get_property time_pos\n|;
487 my $pos = <$from_mplayer>;
488 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
489 warn "# time_pos $1\n";
496 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
501 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
502 warn "<<<< subtitle ", sub_fmt $s;
503 preroll $s->[0], $s->[2];
504 # print $to_mplayer "set_property time_pos $s->[0]\n";
508 my $pos = time_pos + $preroll;
509 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
510 warn ">>>> subtitle ", sub_fmt $s;
511 preroll $s->[0], $s->[2];
512 # print $to_mplayer "set_property time_pos $s->[0]\n";
515 sub current_subtitle {
516 my $callback = shift;
519 foreach my $nr ( 0 .. $#subtitles ) {
520 my $s = $subtitles[$nr];
521 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
522 warn sub_fmt $s, $pos;
524 $callback->( $visible, $pos ) if $callback;
525 return ( $visible, $pos );
528 warn "# $pos no visible subtitle\n";
533 current_subtitle( sub {
534 my ( $nr, $pos ) = @_;
535 my $new_start = $subtitles[$nr]->[0] += $offset;
536 warn "subtitle $nr $pos $offset $new_start\n";
538 preroll $new_start, "$pos $offset $new_start";
544 # XXX main epool loop
548 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
550 warn "no events" unless $events;
552 foreach my $e ( @$events ) {
553 # warn "# event: ", dump($e), $/;
555 my $fileno = $e->[0];
557 if ( $fileno == fileno STDIN ) {
559 sysread STDIN, $chr, 1;
561 } elsif ( $fileno == fileno $from_mplayer ) {
563 sysread $from_mplayer, $chr, 1;
566 if ( $chr =~ m{[\n\r]} ) {
568 exit if $line =~ m{Exiting};
570 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
572 warn "prop $1 = $2\n";
573 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
575 # XXX keyboard shortcuts
578 : $1 eq ',' ? add_subtitle
579 : $1 eq 'F1' ? prev_subtitle
580 : $1 eq 'F2' ? move_subtitle( -0.3 )
581 : $1 eq 'F3' ? move_subtitle( +0.3 )
582 : $1 eq 'F4' ? next_subtitle
583 : $1 eq 'F5' ? save_subtitles
584 : $1 eq 'F9' ? add_subtitle
585 : $1 eq 'F12' ? edit_subtitles
589 } elsif ( $line =~ m{EDL}i ) {
591 print $to_mplayer qq|osd_show_text "$line"\n|;
593 if ( my $pos = time_pos ) {
594 if ( $line =~ m{start}i ) {
595 push @subtitles, [ $pos, $pos, '-' ];
597 $subtitles[ $#subtitles ]->[1] = $pos;
608 } elsif ( $fileno == fileno $to_mplayer ) {
611 die "invalid fileno $fileno";