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{^(.+)/[^/]+$} }
19 sub base_name { $1 if $_[0] =~ m{^.+/([^/]+)$} }
21 if ( ! $movie && -e 'media/_editing' ) {
22 $movie = 'media/' . readlink('media/_editing') . '/video.ogv';
23 warn "using media/_editing -> $movie\n";
24 } elsif ( -d $movie && $movie =~ m{media/} ) {
25 $movie .= '/video.ogv';
26 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
27 my $movie_master = $movie;
28 $movie = base_dir($movie) . '/video.ogv';
30 symlink base_name($movie_master), $movie;
31 warn "symlink video.ogv -> $movie_master";
33 warn "using symlink video.ogv -> ", readlink $movie;
35 } elsif ( -f $movie ) {
36 warn "using video $movie";
38 die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
41 my $media_part = my $media_dir = base_dir($movie);
42 $media_part =~ s{media/}{};
43 $media_part =~ s{/$}{};
45 unlink 'media/_editing';
46 symlink $media_part, 'media/_editing';
48 warn "# media_part $media_part\n";
50 my $edl = "/dev/shm/edl";
51 my $subtitles = $movie;
52 $subtitles =~ s{\.\w+$}{.srt};
56 my $slide_factor = 4; # 1/4 size of video
63 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
69 '-vf' => 'screenshot',
72 my $epfd = epoll_create(10);
74 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
75 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
76 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
81 warn "$movie ", -s $movie, " bytes $edl\n";
82 print $to_mplayer qq|loadfile "$movie"\n|;
83 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
88 my $term_id = `xdotool getwindowfocus`;
93 $mplayer_id ||= `xdotool search mplayer`;
94 warn "focus_mplayer $mplayer_id\n";
95 system "xdotool windowactivate $mplayer_id"
99 warn "focus_term $term_id\n";
100 system "xdotool windowactivate $term_id";
104 my ( $pos, $osd ) = @_;
106 warn "PREROLL $pos $osd\n";
107 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
108 my $to = $pos - $preroll;
110 print $to_mplayer "set_property time_pos $to\n";
111 print $to_mplayer "get_property time_pos\n";
112 print $to_mplayer "play\n";
125 print $to_mplayer $cmd;
132 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
137 if ( ! $prop->{width} || ! $prop->{height} ) {
138 warn "SKIP no size yet\n";
142 if ( ! @subtitles ) {
143 warn "SKIP no subtitles yet\n";
154 foreach my $s ( @subtitles ) {
155 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
156 startTime => $s->[0],
161 if ( $s->[2] =~ m{video:(.+)} ) {
163 my $path = "$media_dir/$video";
165 warn "MISSING $path: $!\n";
167 my $frame_dir = "$media_dir/s/$video";
168 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
170 push @videos, [ @$s, $video ];
172 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
173 my ( $nr, $t ) = ( $1, $2 );
175 my $hires = "$media_dir/s/hires";
176 mkdir $hires unless -e $hires;
178 my $shot_path = sprintf "$hires/s%03d.jpg", $nr;
179 if ( ! -e $shot_path ) {
180 my $frame_dir = "$media_dir/s/shot/";
181 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss $t $movie";
182 rename "$media_dir/s/shot/00000001.jpg", $shot_path;
183 warn "created $shot_path from $movie at $t for slide $nr\n";
187 next unless $s->[2] =~ m{\[(\d+)\]};
190 push @{ $sync->{customEvents} }, {
191 startTime => $s->[0],
193 action => 'chapterChange',
195 carousel => 'theCarousel',
198 description => $s->[2],
199 src => slide_jpg( 4 => $1 ),
204 push @slide_t, $s->[0];
207 foreach ( 0 .. $#slide_t ) {
208 my $slide_nr = $_ + 1;
209 push @{ $sync->{htmlEvents}->{'#slide'} }, {
210 startTime => $slide_t[$_],
211 endTime => $slide_t[$_ + 1] || $prop->{length},
212 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
216 my @slides_hires = glob "$media_dir/s/hires/*";
218 foreach my $factor ( 4, 2, 1 ) {
219 my $w = $prop->{width} / $factor;
220 my $h = $prop->{height} / $factor;
222 my $path = "$media_dir/s";
224 warn "create slides imaes in $path";
228 $path .= '/' . $factor;
232 warn "created $path\n";
236 foreach my $hires ( @slides_hires ) {
238 my $nr = $1 if $hires =~ m{^\s+(\d+)\.\w+$} || warn "can't find number in $hires";
240 my $file = slide_jpg( $factor => $nr );
241 warn "slide $hires -> $file\n";
244 if ( my $im = Imager->new( file => $hires ) ) {
245 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
246 warn "resized $file ", -s $file, " bytes\n";
248 die "can't open $hires: $!";
254 my ( $slide_width, $slide_height );
256 my $im = Imager->new( file => slide_jpg( 1 => 1 ) );
259 $slide_width = $im->getwidth / $slide_factor;
260 $slide_height = $im->getheight / $slide_factor;
262 warn "can't find first slide default to 1/$slide_factor of video size\n";
263 $slide_width = $prop->{width} / $slide_factor;
264 $slide_height = $prop->{height} / $slide_factor;
271 width => $slide_width,
272 height => $slide_height,
276 $html5tv->{html}->{video_tags} =
283 push @{ $html5tv->{sync}->{customEvents} }, {
284 startTime => $s->[0],
286 action => 'additional_video',
290 description => $s->[2],
291 src => "$media_dir/s/1/00000001.jpg",
297 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
298 <source src="$media_dir/$_->[3]" />
305 sub customEvents_sorted {
307 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
309 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
313 sort { $a->{startTime} <=> $b->{startTime} }
314 @{ $html5tv->{sync}->{customEvents} }
319 $_->{args}->{index} = $index++ foreach customEvents_sorted;
321 warn "last customEvent $index\n";
323 $html5tv->{html}->{subtitles_table}
324 = qq|<table id="subtitles">|
327 <tr id="sub_$_->{index}">
328 <td class="seek_video">$_->{startTime}</td>
329 <td class="seek_video">$_->{endTime}</td>
330 <td>$_->{args}->{title}</td>
335 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
338 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
339 my $hcal_path = '$media_dir/hCalendar.html';
340 if ( -e $hcal_path ) {
341 $html5tv->{hCalendar} = read_file $hcal_path;
342 my $tree = HTML::TreeBuilder->new;
343 $tree->parse_file($hcal_path);
344 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
345 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
349 warn "# html5tv ", dump $html5tv;
351 my $sync_path = "$media_dir/video.js";
352 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
353 warn "sync $sync_path ", -s $sync_path, " bytes\n";
355 my $html = read_file 'www/tv.html';
356 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
357 warn "no interpolation in template!";
359 write_file "www/_editing.html", $html;
360 $html =~ s{media/_editing}{media/$media_part}gs;
361 write_file "www/$media_part.html", $html;
363 my $carousel_width = $prop->{width} + $slide_width - 80;
364 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
365 my $carousel_height = $slide_height + 2;
367 write_file "$media_dir/video.css", qq|
369 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
370 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
371 width: ${carousel_width}px;
372 height: ${carousel_height}px;
375 .jcarousel-skin-ie7 .jcarousel-item {
376 width: ${slide_width}px;
377 height: ${slide_height}px;
382 background-color: #ffc;
386 width: $prop->{width}px;
387 height: $prop->{height}px;
388 font-family: Arial, Helvetica, sans-serif;
389 margin: 0 10px 0px 0;
403 padding: 1px 10px 5px 10px ;
406 width: $html5tv->{video}->{width}px;
411 .jcarousel-skin-ie7 .jcarousel-item img:hover {
412 // border-color: #555 !important;
415 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
416 visibility: visible !important;
419 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
425 font-family: Arial, Verdana;
432 padding: 2px 0 2px 0;
433 width: ${slide_width}px;
438 font-family: monospace;
449 my $hh = int( $t / 3600 );
451 my $mm = int( $t / 60 );
453 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
464 foreach my $s ( @subtitles ) {
466 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
472 write_file $subtitles, $srt;
473 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
475 print $to_mplayer "sub_remove\n";
476 print $to_mplayer qq|sub_load "$subtitles"\n|;
477 print $to_mplayer "sub_select 1\n";
481 if ( ! -e "$subtitles.yaml" ) {
482 warn "no subtitles $subtitles to load\n";
485 @subtitles = YAML::LoadFile "$subtitles.yaml";
486 warn "subtitles ", dump @subtitles;
491 print $to_mplayer qq|pause\n|;
493 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
498 print $to_mplayer qq|pause\n|;
502 warn "subtitles ", dump( @subtitles );
505 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
511 preroll $subtitles[ $#subtitles ]->[0], $line;
515 print $to_mplayer qq|get_property time_pos\n|;
516 my $pos = <$from_mplayer>;
517 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
518 warn "# time_pos $1\n";
525 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
530 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
531 warn "<<<< subtitle ", sub_fmt $s;
532 preroll $s->[0], $s->[2];
533 # print $to_mplayer "set_property time_pos $s->[0]\n";
537 my $pos = time_pos + $preroll;
538 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
539 warn ">>>> subtitle ", sub_fmt $s;
540 preroll $s->[0], $s->[2];
541 # print $to_mplayer "set_property time_pos $s->[0]\n";
544 sub current_subtitle {
545 my $callback = shift;
548 foreach my $nr ( 0 .. $#subtitles ) {
549 my $s = $subtitles[$nr];
550 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
551 warn sub_fmt $s, $pos;
553 $callback->( $visible, $pos ) if $callback;
554 return ( $visible, $pos );
557 warn "# $pos no visible subtitle\n";
562 current_subtitle( sub {
563 my ( $nr, $pos ) = @_;
564 my $new_start = $subtitles[$nr]->[0] += $offset;
565 warn "subtitle $nr $pos $offset $new_start\n";
567 preroll $new_start, "$pos $offset $new_start";
573 # XXX main epool loop
577 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
579 warn "no events" unless $events;
581 foreach my $e ( @$events ) {
582 # warn "# event: ", dump($e), $/;
584 my $fileno = $e->[0];
586 if ( $fileno == fileno STDIN ) {
588 sysread STDIN, $chr, 1;
590 } elsif ( $fileno == fileno $from_mplayer ) {
592 sysread $from_mplayer, $chr, 1;
595 if ( $chr =~ m{[\n\r]} ) {
597 exit if $line =~ m{Exiting};
599 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
601 warn "prop $1 = $2\n";
602 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
604 # XXX keyboard shortcuts
607 : $1 eq ',' ? add_subtitle
608 : $1 eq 'F1' ? prev_subtitle
609 : $1 eq 'F2' ? move_subtitle( -0.3 )
610 : $1 eq 'F3' ? move_subtitle( +0.3 )
611 : $1 eq 'F4' ? next_subtitle
612 : $1 eq 'F5' ? save_subtitles
613 : $1 eq 'F9' ? add_subtitle
614 : $1 eq 'F12' ? edit_subtitles
618 } elsif ( $line =~ m{EDL}i ) {
620 print $to_mplayer qq|osd_show_text "$line"\n|;
622 if ( my $pos = time_pos ) {
623 if ( $line =~ m{start}i ) {
624 push @subtitles, [ $pos, $pos, '-' ];
626 $subtitles[ $#subtitles ]->[1] = $pos;
629 } elsif ( $line =~ m{(shot\d+.png)} ) {
632 warn "shot $t $shot\n";
634 my $dir = "$media_dir/s/shot";
637 warn "created $dir\n";
640 my $hires = "$media_dir/s/hires";
641 mkdir $hires unless -e $hires;
643 my $shot_path = "$dir/$t.png";
644 rename $1, $shot_path;
645 my $nr = scalar glob("$hires/*") + 1;
647 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
649 symlink $shot_path, sprintf("../hires/s%03d.png", $nr);
650 warn "slide $nr from video $t file $shot\n";
660 } elsif ( $fileno == fileno $to_mplayer ) {
663 die "invalid fileno $fileno";