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 foreach ( 0 .. $#slide_t ) {
186 push @{ $sync->{htmlEvents}->{'#slide'} }, {
187 startTime => $slide_t[$_],
188 endTime => $slide_t[$_ + 1] || $prop->{length},
189 html => sprintf( '<img src=%s/s/1/p%03d.jpg>', $media_dir, $_ + 1 ),
193 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
194 @slides_hires = glob 'shot*.png' unless @slides_hires;
196 foreach my $factor ( 4, 2, 1 ) {
197 my $w = $prop->{width} / $factor;
198 my $h = $prop->{height} / $factor;
200 my $path = "$media_dir/s";
202 warn "create slides imaes in $path";
206 $path .= '/' . $factor;
210 warn "created $path\n";
214 foreach my $hires ( @slides_hires ) {
217 $file =~ s{^.+(\d+)\.\w+$}{$path/$1.jpg} || warn "can't rewrite $file";
218 warn "slide $hires -> $file\n";
221 my $im = Imager->new( file => $hires );
222 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
223 warn "resized $file ", -s $file, " bytes\n";
228 my ( $slide_width, $slide_height );
230 my $im = Imager->new( file => "$media_dir/s/1/p001.jpg" )
231 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
235 $slide_width = $im->getwidth / $slide_factor;
236 $slide_height = $im->getheight / $slide_factor;
238 warn "can't find first slide default to 1/$slide_factor of video size\n";
239 $slide_width = $prop->{width} / $slide_factor;
240 $slide_height = $prop->{height} / $slide_factor;
247 width => $slide_width,
248 height => $slide_height,
252 $html5tv->{video_tags} =
259 push @{ $html5tv->{sync}->{customEvents} }, {
260 startTime => $s->[0],
262 action => 'additional_video',
266 description => $s->[2],
267 src => "$media_dir/s/1/00000001.jpg",
273 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
274 <source src="$media_dir/$_->[3]" />
281 sub customEvents_sorted {
283 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
285 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
289 sort { $a->{startTime} <=> $b->{startTime} }
290 @{ $html5tv->{sync}->{customEvents} }
295 $_->{args}->{index} = $index++ foreach customEvents_sorted;
297 warn "last customEvent $index\n";
299 $html5tv->{subtitles_table}
300 = qq|<table id="subtitles">|
303 <tr id="sub_$_->{index}">
304 <td class="seek_video">$_->{startTime}</td>
305 <td class="seek_video">$_->{endTime}</td>
306 <td>$_->{args}->{title}</td>
311 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
314 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
315 my $hcal_path = '$media_dir/hcalendar.html';
316 if ( -e $hcal_path ) {
317 $html5tv->{hcalendar} = read_file $hcal_path;
318 my $tree = HTML::TreeBuilder->new;
319 $tree->parse_file($hcal_path);
320 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
321 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
325 warn "# html5tv ", dump $html5tv;
327 my $sync_path = "$media_dir/video.js";
328 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
329 warn "sync $sync_path ", -s $sync_path, " bytes\n";
331 my $html = read_file 'www/tv.html';
332 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
333 warn "no interpolation in template!";
335 write_file "www/_editing.html", $html;
336 $html =~ s{media/_editing}{media/$media_part}gs;
337 write_file "www/$media_part.html", $html;
339 my $carousel_width = $prop->{width} + $slide_width - 80;
340 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
341 my $carousel_height = $slide_height + 2;
343 write_file "$media_dir/video.css", qq|
345 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
346 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
347 width: ${carousel_width}px;
348 height: ${carousel_height}px;
351 .jcarousel-skin-ie7 .jcarousel-item {
352 width: ${slide_width}px;
353 height: ${slide_height}px;
358 background-color: #ffc;
362 width: $prop->{width}px;
363 height: $prop->{height}px;
364 font-family: Arial, Helvetica, sans-serif;
365 margin: 0 10px 0px 0;
379 padding: 1px 10px 5px 10px ;
382 width: $html5tv->{video}->{width}px;
387 .jcarousel-skin-ie7 .jcarousel-item img:hover {
388 // border-color: #555 !important;
391 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
392 visibility: visible !important;
395 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
401 font-family: Arial, Verdana;
408 padding: 2px 0 2px 0;
409 width: ${slide_width}px;
414 font-family: monospace;
425 my $hh = int( $t / 3600 );
427 my $mm = int( $t / 60 );
429 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
440 foreach my $s ( @subtitles ) {
442 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
448 write_file $subtitles, $srt;
449 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
451 print $to_mplayer "sub_remove\n";
452 print $to_mplayer qq|sub_load "$subtitles"\n|;
453 print $to_mplayer "sub_select 1\n";
457 if ( ! -e "$subtitles.yaml" ) {
458 warn "no subtitles $subtitles to load\n";
461 @subtitles = YAML::LoadFile "$subtitles.yaml";
462 warn "subtitles ", dump @subtitles;
467 print $to_mplayer qq|pause\n|;
469 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
474 print $to_mplayer qq|pause\n|;
478 warn "subtitles ", dump( @subtitles );
481 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
487 preroll $subtitles[ $#subtitles ]->[0], $line;
491 print $to_mplayer qq|get_property time_pos\n|;
492 my $pos = <$from_mplayer>;
493 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
494 warn "# time_pos $1\n";
501 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
506 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
507 warn "<<<< subtitle ", sub_fmt $s;
508 preroll $s->[0], $s->[2];
509 # print $to_mplayer "set_property time_pos $s->[0]\n";
513 my $pos = time_pos + $preroll;
514 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
515 warn ">>>> subtitle ", sub_fmt $s;
516 preroll $s->[0], $s->[2];
517 # print $to_mplayer "set_property time_pos $s->[0]\n";
520 sub current_subtitle {
521 my $callback = shift;
524 foreach my $nr ( 0 .. $#subtitles ) {
525 my $s = $subtitles[$nr];
526 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
527 warn sub_fmt $s, $pos;
529 $callback->( $visible, $pos ) if $callback;
530 return ( $visible, $pos );
533 warn "# $pos no visible subtitle\n";
538 current_subtitle( sub {
539 my ( $nr, $pos ) = @_;
540 my $new_start = $subtitles[$nr]->[0] += $offset;
541 warn "subtitle $nr $pos $offset $new_start\n";
543 preroll $new_start, "$pos $offset $new_start";
549 # XXX main epool loop
553 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
555 warn "no events" unless $events;
557 foreach my $e ( @$events ) {
558 # warn "# event: ", dump($e), $/;
560 my $fileno = $e->[0];
562 if ( $fileno == fileno STDIN ) {
564 sysread STDIN, $chr, 1;
566 } elsif ( $fileno == fileno $from_mplayer ) {
568 sysread $from_mplayer, $chr, 1;
571 if ( $chr =~ m{[\n\r]} ) {
573 exit if $line =~ m{Exiting};
575 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
577 warn "prop $1 = $2\n";
578 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
580 # XXX keyboard shortcuts
583 : $1 eq ',' ? add_subtitle
584 : $1 eq 'F1' ? prev_subtitle
585 : $1 eq 'F2' ? move_subtitle( -0.3 )
586 : $1 eq 'F3' ? move_subtitle( +0.3 )
587 : $1 eq 'F4' ? next_subtitle
588 : $1 eq 'F5' ? save_subtitles
589 : $1 eq 'F9' ? add_subtitle
590 : $1 eq 'F12' ? edit_subtitles
594 } elsif ( $line =~ m{EDL}i ) {
596 print $to_mplayer qq|osd_show_text "$line"\n|;
598 if ( my $pos = time_pos ) {
599 if ( $line =~ m{start}i ) {
600 push @subtitles, [ $pos, $pos, '-' ];
602 $subtitles[ $#subtitles ]->[1] = $pos;
613 } elsif ( $fileno == fileno $to_mplayer ) {
616 die "invalid fileno $fileno";