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 = readlink 'media/editing';
22 } elsif ( -d $movie && $movie =~ m{media/} ) {
23 $movie .= '/video.ogv';
24 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
25 my $movie_master = $movie;
26 my $movie = base_dir($movie) . '/video.ogv';
27 symlink $movie_master, $movie;
28 warn "symlink $movie_master <- $movie\n";
29 } elsif ( -f $movie ) {
30 warn "using video $movie";
32 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
35 my $media_dir = base_dir($movie);
37 unlink 'media/editing';
38 symlink $movie, 'media/editing';
40 warn "# media_dir $media_dir\n";
42 my $edl = "/dev/shm/edl";
43 my $subtitles = $movie;
44 $subtitles =~ s{\.\w+$}{.srt};
48 my $slide_factor = 4; # 1/4 size of video
55 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
61 '-vf' => 'screenshot',
64 my $epfd = epoll_create(10);
66 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
67 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
68 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
71 warn "$movie ", -s $movie, " bytes $edl\n";
72 print $to_mplayer qq|loadfile "$movie"\n|;
73 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
77 my $term_id = `xdotool getwindowfocus`;
82 $mplayer_id ||= `xdotool search mplayer`;
83 warn "focus_mplayer $mplayer_id\n";
84 system "xdotool windowactivate $mplayer_id"
88 warn "focus_term $term_id\n";
89 system "xdotool windowactivate $term_id";
93 my ( $pos, $osd ) = @_;
95 warn "PREROLL $pos $osd\n";
96 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
97 my $to = $pos - $preroll;
99 print $to_mplayer "set_property time_pos $to\n";
100 print $to_mplayer "get_property time_pos\n";
101 print $to_mplayer "play\n";
114 print $to_mplayer $cmd;
122 if ( ! $prop->{width} || ! $prop->{height} ) {
123 warn "SKIP no size yet\n";
127 if ( ! @subtitles ) {
128 warn "SKIP no subtitles yet\n";
139 foreach my $s ( @subtitles ) {
140 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
141 startTime => $s->[0],
146 if ( $s->[2] =~ m{video:(.+)} ) {
148 my $path = "$media_dir/$video";
150 warn "MISSING $path: $!\n";
152 my $frame_dir = "$media_dir/s/$video";
153 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
155 push @videos, [ @$s, $video ];
159 next unless $s->[2] =~ m{\[(\d+)\]};
162 push @{ $sync->{customEvents} }, {
163 startTime => $s->[0],
165 action => 'chapterChange',
167 carousel => 'theCarousel',
170 description => $s->[2],
171 src => sprintf('%s/s/%dx%d/p%03d.jpg', $media_dir, $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
176 push @slide_t, $s->[0];
179 my $res = $prop->{width} . 'x' . $prop->{height};
181 foreach ( 0 .. $#slide_t ) {
182 push @{ $sync->{htmlEvents}->{'#slide'} }, {
183 startTime => $slide_t[$_],
184 endTime => $slide_t[$_ + 1] || $prop->{length},
185 html => sprintf( '<img src=%s/s/%s/p%03d.jpg>', $media_dir, $res, $_ + 1 ),
189 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
190 @slides_hires = glob 'shot*.png' unless @slides_hires;
193 foreach my $factor ( 4, 2, 1 ) {
194 my $w = $prop->{width} / $factor;
195 my $h = $prop->{height} / $factor;
197 my $path = "$media_dir/s/${w}x${h}";
198 $factor_s_path->{$factor} = $path;
202 warn "created $path\n";
204 foreach my $hires ( @slides_hires ) {
207 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
209 my $im = Imager->new( file => $hires );
210 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
211 warn "resized $file ", -s $file, " bytes\n";
217 my ( $slide_width, $slide_height );
219 my $im = Imager->new( file => $factor_s_path->{ 1 } . '/p001.jpg"')
220 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
224 $slide_width = $im->getwidth / $slide_factor;
225 $slide_height = $im->getheight / $slide_factor;
227 warn "can't find first slide default to 1/$slide_factor of video size\n";
228 $slide_width = $prop->{width} / $slide_factor;
229 $slide_height = $prop->{height} / $slide_factor;
236 width => $slide_width,
237 height => $slide_height,
241 $html5tv->{video_tags} =
248 push @{ $html5tv->{sync}->{customEvents} }, {
249 startTime => $s->[0],
251 action => 'additional_video',
255 description => $s->[2],
256 src => "$media_dir/s/$s->[3]/00000001.jpg",
262 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
263 <source src="$media_dir/$_->[3]" />
270 sub customEvents_sorted {
272 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
274 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
278 sort { $a->{startTime} <=> $b->{startTime} }
279 @{ $html5tv->{sync}->{customEvents} }
284 $_->{args}->{index} = $index++ foreach customEvents_sorted;
286 warn "last customEvent $index\n";
288 $html5tv->{subtitles_table}
289 = qq|<table id="subtitles">|
292 <tr id="sub_$_->{index}">
293 <td class="seek_video">$_->{startTime}</td>
294 <td class="seek_video">$_->{endTime}</td>
295 <td>$_->{args}->{title}</td>
300 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
303 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
304 my $hcal_path = '$media_dir/hcalendar.html';
305 if ( -e $hcal_path ) {
306 $html5tv->{hcalendar} = read_file $hcal_path;
307 my $tree = HTML::TreeBuilder->new;
308 $tree->parse_file($hcal_path);
309 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
310 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
314 warn "# html5tv ", dump $html5tv;
316 my $sync_path = '$media_dir/video.js';
317 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
318 warn "sync $sync_path ", -s $sync_path, " bytes\n";
320 my $html = read_file 'www/tv.html';
321 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
322 warn "no interpolation in template!";
324 write_file '$media_dir.html', $html;
326 my $carousel_width = $prop->{width} + $slide_width - 80;
327 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
328 my $carousel_height = $slide_height + 2;
330 write_file '$media_dir/video.css', qq|
332 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
333 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
334 width: ${carousel_width}px;
335 height: ${carousel_height}px;
338 .jcarousel-skin-ie7 .jcarousel-item {
339 width: ${slide_width}px;
340 height: ${slide_height}px;
345 background-color: #ffc;
349 width: $prop->{width}px;
350 height: $prop->{height}px;
351 font-family: Arial, Helvetica, sans-serif;
352 margin: 0 10px 0px 0;
366 padding: 1px 10px 5px 10px ;
369 width: $html5tv->{video}->{width}px;
374 .jcarousel-skin-ie7 .jcarousel-item img:hover {
375 // border-color: #555 !important;
378 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
379 visibility: visible !important;
382 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
388 font-family: Arial, Verdana;
395 padding: 2px 0 2px 0;
396 width: ${slide_width}px;
401 font-family: monospace;
412 my $hh = int( $t / 3600 );
414 my $mm = int( $t / 60 );
416 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
427 foreach my $s ( @subtitles ) {
429 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
435 write_file $subtitles, $srt;
436 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
438 print $to_mplayer "sub_remove\n";
439 print $to_mplayer qq|sub_load "$subtitles"\n|;
440 print $to_mplayer "sub_select 1\n";
444 @subtitles = YAML::LoadFile "$subtitles.yaml";
445 warn "subtitles ", dump @subtitles;
449 load_subtitles if -e "$subtitles.yaml";
452 print $to_mplayer qq|pause\n|;
454 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
459 print $to_mplayer qq|pause\n|;
463 warn "subtitles ", dump( @subtitles );
466 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
472 preroll $subtitles[ $#subtitles ]->[0], $line;
476 print $to_mplayer qq|get_property time_pos\n|;
477 my $pos = <$from_mplayer>;
478 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
479 warn "# time_pos $1\n";
486 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
491 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
492 warn "<<<< subtitle ", sub_fmt $s;
493 preroll $s->[0], $s->[2];
494 # print $to_mplayer "set_property time_pos $s->[0]\n";
498 my $pos = time_pos + $preroll;
499 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
500 warn ">>>> subtitle ", sub_fmt $s;
501 preroll $s->[0], $s->[2];
502 # print $to_mplayer "set_property time_pos $s->[0]\n";
505 sub current_subtitle {
506 my $callback = shift;
509 foreach my $nr ( 0 .. $#subtitles ) {
510 my $s = $subtitles[$nr];
511 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
512 warn sub_fmt $s, $pos;
514 $callback->( $visible, $pos ) if $callback;
515 return ( $visible, $pos );
518 warn "# $pos no visible subtitle\n";
523 current_subtitle( sub {
524 my ( $nr, $pos ) = @_;
525 my $new_start = $subtitles[$nr]->[0] += $offset;
526 warn "subtitle $nr $pos $offset $new_start\n";
528 preroll $new_start, "$pos $offset $new_start";
534 # XXX main epool loop
538 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
540 warn "no events" unless $events;
542 foreach my $e ( @$events ) {
543 # warn "# event: ", dump($e), $/;
545 my $fileno = $e->[0];
547 if ( $fileno == fileno STDIN ) {
549 sysread STDIN, $chr, 1;
551 } elsif ( $fileno == fileno $from_mplayer ) {
553 sysread $from_mplayer, $chr, 1;
556 if ( $chr =~ m{[\n\r]} ) {
558 exit if $line =~ m{Exiting};
560 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
562 warn "prop $1 = $2\n";
563 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
565 # XXX keyboard shortcuts
568 : $1 eq ',' ? add_subtitle
569 : $1 eq 'F1' ? prev_subtitle
570 : $1 eq 'F2' ? move_subtitle( -0.3 )
571 : $1 eq 'F3' ? move_subtitle( +0.3 )
572 : $1 eq 'F4' ? next_subtitle
573 : $1 eq 'F5' ? save_subtitles
574 : $1 eq 'F9' ? add_subtitle
575 : $1 eq 'F12' ? edit_subtitles
579 } elsif ( $line =~ m{EDL}i ) {
581 print $to_mplayer qq|osd_show_text "$line"\n|;
583 if ( my $pos = time_pos ) {
584 if ( $line =~ m{start}i ) {
585 push @subtitles, [ $pos, $pos, '-' ];
587 $subtitles[ $#subtitles ]->[1] = $pos;
598 } elsif ( $fileno == fileno $to_mplayer ) {
601 die "invalid fileno $fileno";