6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
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 "oggvideotools/src/oggThumb -t $t -o jpg -n $file $video";
145 if ( ! $prop->{width} || ! $prop->{height} ) {
146 warn "SKIP no size yet\n";
150 if ( ! @subtitles ) {
151 warn "SKIP no subtitles yet\n";
163 foreach my $s ( @subtitles ) {
164 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
165 startTime => $s->[0],
170 if ( $s->[2] =~ m{video:(.+)} ) {
172 my $path = "$media_dir/$video";
174 warn "MISSING $path: $!\n";
176 my $frame_dir = "$media_dir/s/$video";
177 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
179 push @videos, [ @$s, $video ];
181 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
182 push @frames, [ $2, $1 ];
186 next unless $s->[2] =~ m{\[(\d+)\]};
189 push @{ $sync->{customEvents} }, {
190 startTime => $s->[0],
192 action => 'chapterChange',
194 carousel => 'theCarousel',
197 description => $s->[2],
198 src => slide_jpg( 4 => $1 ),
203 push @slide_t, $s->[0];
207 my $hires = "$media_dir/s/hires";
208 mkdir $hires unless -e $hires;
209 oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
211 foreach my $i ( 0 .. $#frames ) {
212 my $from = "$hires/.f$i.jpg";
213 my $to = "$hires/f" . $frames[$i]->[1] . '.jpg';
214 rename $from, $to || warn "can't rename $from -> $to: $!";
218 foreach ( 0 .. $#slide_t ) {
219 my $slide_nr = $_ + 1;
220 push @{ $sync->{htmlEvents}->{'#slide'} }, {
221 startTime => $slide_t[$_],
222 endTime => $slide_t[$_ + 1] || $prop->{length},
223 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
227 my @slides_hires = glob "$media_dir/s/hires/*";
229 foreach my $factor ( 4, 2, 1 ) {
230 my $w = $prop->{width} / $factor;
231 my $h = $prop->{height} / $factor;
233 my $path = "$media_dir/s";
235 warn "create slides imaes in $path";
239 $path .= '/' . $factor;
243 warn "created $path\n";
247 foreach my $hires ( @slides_hires ) {
249 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
251 my $file = slide_jpg( $factor => $nr );
252 warn "slide $hires -> $file\n";
255 if ( my $im = Imager->new( file => $hires ) ) {
256 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
257 warn "resized $file ", -s $file, " bytes\n";
259 die "can't open $hires: $!";
265 my ( $slide_width, $slide_height );
267 my $im = Imager->new( file => slide_jpg( 1 => 1 ) );
270 $slide_width = $im->getwidth / $slide_factor;
271 $slide_height = $im->getheight / $slide_factor;
273 warn "can't find first slide default to 1/$slide_factor of video size\n";
274 $slide_width = $prop->{width} / $slide_factor;
275 $slide_height = $prop->{height} / $slide_factor;
282 width => $slide_width,
283 height => $slide_height,
287 $html5tv->{html}->{video_tags} =
294 push @{ $html5tv->{sync}->{customEvents} }, {
295 startTime => $s->[0],
297 action => 'additional_video',
301 description => $s->[2],
302 src => "$media_dir/s/1/00000001.jpg",
308 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
309 <source src="$media_dir/$_->[3]" />
316 sub customEvents_sorted {
318 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
320 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
324 sort { $a->{startTime} <=> $b->{startTime} }
325 @{ $html5tv->{sync}->{customEvents} }
330 $_->{args}->{index} = $index++ foreach customEvents_sorted;
332 warn "last customEvent $index\n";
334 $html5tv->{html}->{subtitles_table}
335 = qq|<table id="subtitles">|
338 <tr id="sub_$_->{index}">
339 <td class="seek_video">$_->{startTime}</td>
340 <td class="seek_video">$_->{endTime}</td>
341 <td>$_->{args}->{title}</td>
346 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
349 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
350 my $hcal_path = '$media_dir/hCalendar.html';
351 if ( -e $hcal_path ) {
352 $html5tv->{hCalendar} = read_file $hcal_path;
353 my $tree = HTML::TreeBuilder->new;
354 $tree->parse_file($hcal_path);
355 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
356 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
360 warn "# html5tv ", dump $html5tv if $debug;
362 my $sync_path = "$media_dir/video.js";
363 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
364 warn "sync $sync_path ", -s $sync_path, " bytes\n";
366 my $html = read_file 'www/tv.html';
367 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
368 warn "no interpolation in template!";
370 write_file "www/_editing.html", $html;
371 $html =~ s{media/_editing}{media/$media_part}gs;
372 write_file "www/$media_part.html", $html;
374 my $carousel_width = $prop->{width} + $slide_width - 80;
375 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
376 my $carousel_height = $slide_height + 2;
378 write_file "$media_dir/video.css", qq|
380 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
381 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
382 width: ${carousel_width}px;
383 height: ${carousel_height}px;
386 .jcarousel-skin-ie7 .jcarousel-item {
387 width: ${slide_width}px;
388 height: ${slide_height}px;
393 background-color: #ffc;
397 width: $prop->{width}px;
398 height: $prop->{height}px;
399 font-family: Arial, Helvetica, sans-serif;
400 margin: 0 10px 0px 0;
414 padding: 1px 10px 5px 10px ;
417 width: $html5tv->{video}->{width}px;
422 .jcarousel-skin-ie7 .jcarousel-item img:hover {
423 // border-color: #555 !important;
426 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
427 visibility: visible !important;
430 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
436 font-family: Arial, Verdana;
443 padding: 2px 0 2px 0;
444 width: ${slide_width}px;
449 font-family: monospace;
460 my $hh = int( $t / 3600 );
462 my $mm = int( $t / 60 );
464 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
475 foreach my $s ( @subtitles ) {
477 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
483 write_file $subtitles, $srt;
484 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
486 print $to_mplayer "sub_remove\n";
487 print $to_mplayer qq|sub_load "$subtitles"\n|;
488 print $to_mplayer "sub_select 1\n";
492 if ( ! -e "$subtitles.yaml" ) {
493 warn "no subtitles $subtitles to load\n";
496 @subtitles = YAML::LoadFile "$subtitles.yaml";
497 warn "subtitles ", dump @subtitles;
502 print $to_mplayer qq|pause\n|;
504 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
509 print $to_mplayer qq|pause\n|;
513 warn "subtitles ", dump( @subtitles );
516 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
522 preroll $subtitles[ $#subtitles ]->[0], $line;
526 print $to_mplayer qq|get_property time_pos\n|;
527 my $pos = <$from_mplayer>;
528 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
529 warn "# time_pos $1\n";
536 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
541 my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
542 warn "<<<< subtitle ", sub_fmt $s;
543 preroll $s->[0], $s->[2];
544 # print $to_mplayer "set_property time_pos $s->[0]\n";
549 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
550 warn ">>>> subtitle ", sub_fmt $s;
551 preroll $s->[0], $s->[2];
552 # print $to_mplayer "set_property time_pos $s->[0]\n";
555 sub current_subtitle {
556 my $callback = shift;
559 foreach my $nr ( 0 .. $#subtitles ) {
560 my $s = $subtitles[$nr];
561 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
562 warn sub_fmt $s, $pos;
564 $callback->( $visible, $pos ) if $callback;
565 return ( $visible, $pos );
568 warn "# $pos no visible subtitle\n";
573 current_subtitle( sub {
574 my ( $nr, $pos ) = @_;
575 my $new_start = $subtitles[$nr]->[0] += $offset;
576 warn "subtitle $nr $pos $offset $new_start\n";
578 preroll $new_start, "$pos $offset $new_start";
584 # XXX main epool loop
588 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
590 warn "no events" unless $events;
592 foreach my $e ( @$events ) {
593 # warn "# event: ", dump($e), $/;
595 my $fileno = $e->[0];
597 if ( $fileno == fileno STDIN ) {
599 sysread STDIN, $chr, 1;
601 } elsif ( $fileno == fileno $from_mplayer ) {
603 sysread $from_mplayer, $chr, 1;
606 if ( $chr =~ m{[\n\r]} ) {
608 exit if $line =~ m{Exiting};
610 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
612 warn "prop $1 = $2\n";
613 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
615 # XXX keyboard shortcuts
618 : $1 eq ',' ? add_subtitle
619 : $1 eq 'F1' ? prev_subtitle
620 : $1 eq 'F2' ? move_subtitle( -0.3 )
621 : $1 eq 'F3' ? move_subtitle( +0.3 )
622 : $1 eq 'F4' ? next_subtitle
623 : $1 eq 'F5' ? save_subtitles
624 : $1 eq 'F9' ? add_subtitle
625 : $1 eq 'F12' ? edit_subtitles
629 } elsif ( $line =~ m{EDL}i ) {
631 print $to_mplayer qq|osd_show_text "$line"\n|;
633 if ( my $pos = time_pos ) {
634 if ( $line =~ m{start}i ) {
635 push @subtitles, [ $pos, $pos, '-' ];
637 $subtitles[ $#subtitles ]->[1] = $pos;
640 } elsif ( $line =~ m{(shot\d+.png)} ) {
643 warn "shot $t $shot\n";
645 my @existing_slides = glob("$media_dir/s/hires/*");
646 my $nr = $#existing_slides + 2;
648 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
650 warn "slide $nr from video $t file $shot\n";
660 } elsif ( $fileno == fileno $to_mplayer ) {
663 die "invalid fileno $fileno";