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;
127 sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
132 if ( ! $prop->{width} || ! $prop->{height} ) {
133 warn "SKIP no size yet\n";
137 if ( ! @subtitles ) {
138 warn "SKIP no subtitles yet\n";
149 foreach my $s ( @subtitles ) {
150 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
151 startTime => $s->[0],
156 if ( $s->[2] =~ m{video:(.+)} ) {
158 my $path = "$media_dir/$video";
160 warn "MISSING $path: $!\n";
162 my $frame_dir = "$media_dir/s/$video";
163 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
165 push @videos, [ @$s, $video ];
169 next unless $s->[2] =~ m{\[(\d+)\]};
172 push @{ $sync->{customEvents} }, {
173 startTime => $s->[0],
175 action => 'chapterChange',
177 carousel => 'theCarousel',
180 description => $s->[2],
181 src => slide_jpg( 4 => $1 ),
186 push @slide_t, $s->[0];
189 foreach ( 0 .. $#slide_t ) {
191 push @{ $sync->{htmlEvents}->{'#slide'} }, {
192 startTime => $slide_t[$_],
193 endTime => $slide_t[$_ + 1] || $prop->{length},
194 html => '<img src=' . slide_jpg( 1 => $_ + 1 ) . '>',
198 my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
199 @slides_hires = glob 'shot*.png' unless @slides_hires;
201 foreach my $factor ( 4, 2, 1 ) {
202 my $w = $prop->{width} / $factor;
203 my $h = $prop->{height} / $factor;
205 my $path = "$media_dir/s";
207 warn "create slides imaes in $path";
211 $path .= '/' . $factor;
215 warn "created $path\n";
219 foreach my $hires ( @slides_hires ) {
221 my $nr = $1 if $hires =~ m{^.+(\d+)\.\w+$} || warn "can't find number in $hires";
223 my $file = slide_jpg( $factor => $nr );
224 warn "slide $hires -> $file\n";
227 my $im = Imager->new( file => $hires );
228 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
229 warn "resized $file ", -s $file, " bytes\n";
234 my ( $slide_width, $slide_height );
236 my $im = Imager->new( file => slide_jpg( 1 => 1 ) )
237 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
241 $slide_width = $im->getwidth / $slide_factor;
242 $slide_height = $im->getheight / $slide_factor;
244 warn "can't find first slide default to 1/$slide_factor of video size\n";
245 $slide_width = $prop->{width} / $slide_factor;
246 $slide_height = $prop->{height} / $slide_factor;
253 width => $slide_width,
254 height => $slide_height,
258 $html5tv->{html}->{video_tags} =
265 push @{ $html5tv->{sync}->{customEvents} }, {
266 startTime => $s->[0],
268 action => 'additional_video',
272 description => $s->[2],
273 src => "$media_dir/s/1/00000001.jpg",
279 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
280 <source src="$media_dir/$_->[3]" />
287 sub customEvents_sorted {
289 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
291 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
295 sort { $a->{startTime} <=> $b->{startTime} }
296 @{ $html5tv->{sync}->{customEvents} }
301 $_->{args}->{index} = $index++ foreach customEvents_sorted;
303 warn "last customEvent $index\n";
305 $html5tv->{html}->{subtitles_table}
306 = qq|<table id="subtitles">|
309 <tr id="sub_$_->{index}">
310 <td class="seek_video">$_->{startTime}</td>
311 <td class="seek_video">$_->{endTime}</td>
312 <td>$_->{args}->{title}</td>
317 . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
320 my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
321 my $hcal_path = '$media_dir/hCalendar.html';
322 if ( -e $hcal_path ) {
323 $html5tv->{hCalendar} = read_file $hcal_path;
324 my $tree = HTML::TreeBuilder->new;
325 $tree->parse_file($hcal_path);
326 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
327 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
331 warn "# html5tv ", dump $html5tv;
333 my $sync_path = "$media_dir/video.js";
334 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
335 warn "sync $sync_path ", -s $sync_path, " bytes\n";
337 my $html = read_file 'www/tv.html';
338 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
339 warn "no interpolation in template!";
341 write_file "www/_editing.html", $html;
342 $html =~ s{media/_editing}{media/$media_part}gs;
343 write_file "www/$media_part.html", $html;
345 my $carousel_width = $prop->{width} + $slide_width - 80;
346 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
347 my $carousel_height = $slide_height + 2;
349 write_file "$media_dir/video.css", qq|
351 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
352 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
353 width: ${carousel_width}px;
354 height: ${carousel_height}px;
357 .jcarousel-skin-ie7 .jcarousel-item {
358 width: ${slide_width}px;
359 height: ${slide_height}px;
364 background-color: #ffc;
368 width: $prop->{width}px;
369 height: $prop->{height}px;
370 font-family: Arial, Helvetica, sans-serif;
371 margin: 0 10px 0px 0;
385 padding: 1px 10px 5px 10px ;
388 width: $html5tv->{video}->{width}px;
393 .jcarousel-skin-ie7 .jcarousel-item img:hover {
394 // border-color: #555 !important;
397 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
398 visibility: visible !important;
401 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
407 font-family: Arial, Verdana;
414 padding: 2px 0 2px 0;
415 width: ${slide_width}px;
420 font-family: monospace;
431 my $hh = int( $t / 3600 );
433 my $mm = int( $t / 60 );
435 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
446 foreach my $s ( @subtitles ) {
448 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
454 write_file $subtitles, $srt;
455 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
457 print $to_mplayer "sub_remove\n";
458 print $to_mplayer qq|sub_load "$subtitles"\n|;
459 print $to_mplayer "sub_select 1\n";
463 if ( ! -e "$subtitles.yaml" ) {
464 warn "no subtitles $subtitles to load\n";
467 @subtitles = YAML::LoadFile "$subtitles.yaml";
468 warn "subtitles ", dump @subtitles;
473 print $to_mplayer qq|pause\n|;
475 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
480 print $to_mplayer qq|pause\n|;
484 warn "subtitles ", dump( @subtitles );
487 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
493 preroll $subtitles[ $#subtitles ]->[0], $line;
497 print $to_mplayer qq|get_property time_pos\n|;
498 my $pos = <$from_mplayer>;
499 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
500 warn "# time_pos $1\n";
507 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
512 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
513 warn "<<<< subtitle ", sub_fmt $s;
514 preroll $s->[0], $s->[2];
515 # print $to_mplayer "set_property time_pos $s->[0]\n";
519 my $pos = time_pos + $preroll;
520 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
521 warn ">>>> subtitle ", sub_fmt $s;
522 preroll $s->[0], $s->[2];
523 # print $to_mplayer "set_property time_pos $s->[0]\n";
526 sub current_subtitle {
527 my $callback = shift;
530 foreach my $nr ( 0 .. $#subtitles ) {
531 my $s = $subtitles[$nr];
532 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
533 warn sub_fmt $s, $pos;
535 $callback->( $visible, $pos ) if $callback;
536 return ( $visible, $pos );
539 warn "# $pos no visible subtitle\n";
544 current_subtitle( sub {
545 my ( $nr, $pos ) = @_;
546 my $new_start = $subtitles[$nr]->[0] += $offset;
547 warn "subtitle $nr $pos $offset $new_start\n";
549 preroll $new_start, "$pos $offset $new_start";
555 # XXX main epool loop
559 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
561 warn "no events" unless $events;
563 foreach my $e ( @$events ) {
564 # warn "# event: ", dump($e), $/;
566 my $fileno = $e->[0];
568 if ( $fileno == fileno STDIN ) {
570 sysread STDIN, $chr, 1;
572 } elsif ( $fileno == fileno $from_mplayer ) {
574 sysread $from_mplayer, $chr, 1;
577 if ( $chr =~ m{[\n\r]} ) {
579 exit if $line =~ m{Exiting};
581 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
583 warn "prop $1 = $2\n";
584 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
586 # XXX keyboard shortcuts
589 : $1 eq ',' ? add_subtitle
590 : $1 eq 'F1' ? prev_subtitle
591 : $1 eq 'F2' ? move_subtitle( -0.3 )
592 : $1 eq 'F3' ? move_subtitle( +0.3 )
593 : $1 eq 'F4' ? next_subtitle
594 : $1 eq 'F5' ? save_subtitles
595 : $1 eq 'F9' ? add_subtitle
596 : $1 eq 'F12' ? edit_subtitles
600 } elsif ( $line =~ m{EDL}i ) {
602 print $to_mplayer qq|osd_show_text "$line"\n|;
604 if ( my $pos = time_pos ) {
605 if ( $line =~ m{start}i ) {
606 push @subtitles, [ $pos, $pos, '-' ];
608 $subtitles[ $#subtitles ]->[1] = $pos;
611 } elsif ( $line =~ m{(shot\d+.png)} ) {
614 warn "shot $t $shot\n";
615 my $dir = "$media_dir/s/shot";
618 warn "created $dir\n";
620 rename $1, "$dir/$t.png";
622 my $hires = "media_dir/s/hires";
623 mkdir $hires unless -e $hires;
624 my $max_slide = scalar( glob("$hires/*") ) || 0;
625 symlink "$dir/$t.png", "$hires/s$max_slide.png";
626 push @subtitles, [ $t, $t, "[$max_slide] video time $t slide $max_slide" ];
627 warn "created slide $max_slide at $t from $shot\n";
636 } elsif ( $fileno == fileno $to_mplayer ) {
639 die "invalid fileno $fileno";