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, @_;
137 my ($video,$t,$file) = @_;
138 system "oggvideotools/src/oggThumb -t $t -o jpg -n $file $video";
139 warn "oggThumb $file at $t from $video\n";
144 if ( ! $prop->{width} || ! $prop->{height} ) {
145 warn "SKIP no size yet\n";
149 if ( ! @subtitles ) {
150 warn "SKIP no subtitles yet\n";
161 foreach my $s ( @subtitles ) {
162 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
163 startTime => $s->[0],
168 if ( $s->[2] =~ m{video:(.+)} ) {
170 my $path = "$media_dir/$video";
172 warn "MISSING $path: $!\n";
174 my $frame_dir = "$media_dir/s/$video";
175 system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
177 push @videos, [ @$s, $video ];
179 } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
180 my ( $nr, $t ) = ( $1, $2 );
182 my $hires = "$media_dir/s/hires";
183 mkdir $hires unless -e $hires;
185 my $shot_path = sprintf "$hires/s%03d.jpg", $nr;
186 oggThumb $movie, $t, $shot_path unless -e $shot_path;
189 next unless $s->[2] =~ m{\[(\d+)\]};
192 push @{ $sync->{customEvents} }, {
193 startTime => $s->[0],
195 action => 'chapterChange',
197 carousel => 'theCarousel',
200 description => $s->[2],
201 src => slide_jpg( 4 => $1 ),
206 push @slide_t, $s->[0];
209 foreach ( 0 .. $#slide_t ) {
210 my $slide_nr = $_ + 1;
211 push @{ $sync->{htmlEvents}->{'#slide'} }, {
212 startTime => $slide_t[$_],
213 endTime => $slide_t[$_ + 1] || $prop->{length},
214 html => '<img src=' . slide_jpg( 1 => $slide_nr ) . '>',
218 my @slides_hires = glob "$media_dir/s/hires/*";
220 foreach my $factor ( 4, 2, 1 ) {
221 my $w = $prop->{width} / $factor;
222 my $h = $prop->{height} / $factor;
224 my $path = "$media_dir/s";
226 warn "create slides imaes in $path";
230 $path .= '/' . $factor;
234 warn "created $path\n";
238 foreach my $hires ( @slides_hires ) {
240 my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
242 my $file = slide_jpg( $factor => $nr );
243 warn "slide $hires -> $file\n";
246 if ( my $im = Imager->new( file => $hires ) ) {
247 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
248 warn "resized $file ", -s $file, " bytes\n";
250 die "can't open $hires: $!";
256 my ( $slide_width, $slide_height );
258 my $im = Imager->new( file => slide_jpg( 1 => 1 ) );
261 $slide_width = $im->getwidth / $slide_factor;
262 $slide_height = $im->getheight / $slide_factor;
264 warn "can't find first slide default to 1/$slide_factor of video size\n";
265 $slide_width = $prop->{width} / $slide_factor;
266 $slide_height = $prop->{height} / $slide_factor;
273 width => $slide_width,
274 height => $slide_height,
278 $html5tv->{html}->{video_tags} =
285 push @{ $html5tv->{sync}->{customEvents} }, {
286 startTime => $s->[0],
288 action => 'additional_video',
292 description => $s->[2],
293 src => "$media_dir/s/1/00000001.jpg",
299 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
300 <source src="$media_dir/$_->[3]" />
307 sub customEvents_sorted {
309 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
311 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
315 sort { $a->{startTime} <=> $b->{startTime} }
316 @{ $html5tv->{sync}->{customEvents} }
321 $_->{args}->{index} = $index++ foreach customEvents_sorted;
323 warn "last customEvent $index\n";
325 $html5tv->{html}->{subtitles_table}
326 = qq|<table id="subtitles">|
329 <tr id="sub_$_->{index}">
330 <td class="seek_video">$_->{startTime}</td>
331 <td class="seek_video">$_->{endTime}</td>
332 <td>$_->{args}->{title}</td>
337 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
340 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
341 my $hcal_path = '$media_dir/hCalendar.html';
342 if ( -e $hcal_path ) {
343 $html5tv->{hCalendar} = read_file $hcal_path;
344 my $tree = HTML::TreeBuilder->new;
345 $tree->parse_file($hcal_path);
346 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
347 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
351 warn "# html5tv ", dump $html5tv if $debug;
353 my $sync_path = "$media_dir/video.js";
354 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
355 warn "sync $sync_path ", -s $sync_path, " bytes\n";
357 my $html = read_file 'www/tv.html';
358 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
359 warn "no interpolation in template!";
361 write_file "www/_editing.html", $html;
362 $html =~ s{media/_editing}{media/$media_part}gs;
363 write_file "www/$media_part.html", $html;
365 my $carousel_width = $prop->{width} + $slide_width - 80;
366 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
367 my $carousel_height = $slide_height + 2;
369 write_file "$media_dir/video.css", qq|
371 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
372 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
373 width: ${carousel_width}px;
374 height: ${carousel_height}px;
377 .jcarousel-skin-ie7 .jcarousel-item {
378 width: ${slide_width}px;
379 height: ${slide_height}px;
384 background-color: #ffc;
388 width: $prop->{width}px;
389 height: $prop->{height}px;
390 font-family: Arial, Helvetica, sans-serif;
391 margin: 0 10px 0px 0;
405 padding: 1px 10px 5px 10px ;
408 width: $html5tv->{video}->{width}px;
413 .jcarousel-skin-ie7 .jcarousel-item img:hover {
414 // border-color: #555 !important;
417 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
418 visibility: visible !important;
421 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
427 font-family: Arial, Verdana;
434 padding: 2px 0 2px 0;
435 width: ${slide_width}px;
440 font-family: monospace;
451 my $hh = int( $t / 3600 );
453 my $mm = int( $t / 60 );
455 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
466 foreach my $s ( @subtitles ) {
468 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
474 write_file $subtitles, $srt;
475 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
477 print $to_mplayer "sub_remove\n";
478 print $to_mplayer qq|sub_load "$subtitles"\n|;
479 print $to_mplayer "sub_select 1\n";
483 if ( ! -e "$subtitles.yaml" ) {
484 warn "no subtitles $subtitles to load\n";
487 @subtitles = YAML::LoadFile "$subtitles.yaml";
488 warn "subtitles ", dump @subtitles;
493 print $to_mplayer qq|pause\n|;
495 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
500 print $to_mplayer qq|pause\n|;
504 warn "subtitles ", dump( @subtitles );
507 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
513 preroll $subtitles[ $#subtitles ]->[0], $line;
517 print $to_mplayer qq|get_property time_pos\n|;
518 my $pos = <$from_mplayer>;
519 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
520 warn "# time_pos $1\n";
527 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
532 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
533 warn "<<<< subtitle ", sub_fmt $s;
534 preroll $s->[0], $s->[2];
535 # print $to_mplayer "set_property time_pos $s->[0]\n";
539 my $pos = time_pos + $preroll;
540 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
541 warn ">>>> subtitle ", sub_fmt $s;
542 preroll $s->[0], $s->[2];
543 # print $to_mplayer "set_property time_pos $s->[0]\n";
546 sub current_subtitle {
547 my $callback = shift;
550 foreach my $nr ( 0 .. $#subtitles ) {
551 my $s = $subtitles[$nr];
552 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
553 warn sub_fmt $s, $pos;
555 $callback->( $visible, $pos ) if $callback;
556 return ( $visible, $pos );
559 warn "# $pos no visible subtitle\n";
564 current_subtitle( sub {
565 my ( $nr, $pos ) = @_;
566 my $new_start = $subtitles[$nr]->[0] += $offset;
567 warn "subtitle $nr $pos $offset $new_start\n";
569 preroll $new_start, "$pos $offset $new_start";
575 # XXX main epool loop
579 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
581 warn "no events" unless $events;
583 foreach my $e ( @$events ) {
584 # warn "# event: ", dump($e), $/;
586 my $fileno = $e->[0];
588 if ( $fileno == fileno STDIN ) {
590 sysread STDIN, $chr, 1;
592 } elsif ( $fileno == fileno $from_mplayer ) {
594 sysread $from_mplayer, $chr, 1;
597 if ( $chr =~ m{[\n\r]} ) {
599 exit if $line =~ m{Exiting};
601 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
603 warn "prop $1 = $2\n";
604 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
606 # XXX keyboard shortcuts
609 : $1 eq ',' ? add_subtitle
610 : $1 eq 'F1' ? prev_subtitle
611 : $1 eq 'F2' ? move_subtitle( -0.3 )
612 : $1 eq 'F3' ? move_subtitle( +0.3 )
613 : $1 eq 'F4' ? next_subtitle
614 : $1 eq 'F5' ? save_subtitles
615 : $1 eq 'F9' ? add_subtitle
616 : $1 eq 'F12' ? edit_subtitles
620 } elsif ( $line =~ m{EDL}i ) {
622 print $to_mplayer qq|osd_show_text "$line"\n|;
624 if ( my $pos = time_pos ) {
625 if ( $line =~ m{start}i ) {
626 push @subtitles, [ $pos, $pos, '-' ];
628 $subtitles[ $#subtitles ]->[1] = $pos;
631 } elsif ( $line =~ m{(shot\d+.png)} ) {
634 warn "shot $t $shot\n";
636 my @existing_slides = glob("$media_dir/s/hires/*");
637 my $nr = $#existing_slides + 2;
639 push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
641 warn "slide $nr from video $t file $shot\n";
651 } elsif ( $fileno == fileno $to_mplayer ) {
654 die "invalid fileno $fileno";