6 use IPC::Open3 qw(open3);
8 use Data::Dump qw(dump);
12 use HTML::TreeBuilder;
16 my $movie = shift @ARGV
17 || 'www/media/video.ogv';
18 # || die "usage: $0 media/conference-lecture_name/video.ogv\n";
20 my $base_dir = $1 if $movie =~ m{^(.+)/[^/]+$};
22 if ( -d $movie && $movie =~ m{media/} ) {
24 $movie .= '/video.ogv';
25 } elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
26 my $movie_master = $movie;
27 $movie = "$base_dir/video.ogv";
28 symlink $movie_master, $movie unless -e $movie;
31 die "$movie $! - make symlink or specify path full path to video.ogv" unless -e $movie;
35 symlink $base_dir, 'www/media';
36 warn "# base_dir $base_dir\n";
38 my $edl = "/dev/shm/edl";
39 my $subtitles = $movie;
40 $subtitles =~ s{\.\w+$}{.srt};
44 my $slide_factor = 4; # 1/4 size of video
51 my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
57 '-vf' => 'screenshot',
60 my $epfd = epoll_create(10);
62 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN , EPOLLIN ) >= 0 || die $!;
63 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN ) >= 0 || die $!;
64 #epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer , EPOLLOUT ) >= 0 || die $!;
67 warn "$movie ", -s $movie, " bytes $edl\n";
68 print $to_mplayer qq|loadfile "$movie"\n|;
69 print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
73 my $term_id = `xdotool getwindowfocus`;
78 $mplayer_id ||= `xdotool search mplayer`;
79 warn "focus_mplayer $mplayer_id\n";
80 system "xdotool windowactivate $mplayer_id"
84 warn "focus_term $term_id\n";
85 system "xdotool windowactivate $term_id";
89 my ( $pos, $osd ) = @_;
91 warn "PREROLL $pos $osd\n";
92 print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
93 my $to = $pos - $preroll;
95 print $to_mplayer "set_property time_pos $to\n";
96 print $to_mplayer "get_property time_pos\n";
97 print $to_mplayer "play\n";
110 print $to_mplayer $cmd;
118 if ( ! $prop->{width} || ! $prop->{height} ) {
119 warn "SKIP no size yet\n";
123 if ( ! @subtitles ) {
124 warn "SKIP no subtitles yet\n";
135 foreach my $s ( @subtitles ) {
136 push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
137 startTime => $s->[0],
142 if ( $s->[2] =~ m{video:(.+)} ) {
144 my $path = "www/media/$video";
146 warn "MISSING $path: $!\n";
148 my $frame_dir = "www/media/s/$video";
149 system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 www/media/$video"
151 push @videos, [ @$s, $video ];
155 next unless $s->[2] =~ m{\[(\d+)\]};
158 push @{ $sync->{customEvents} }, {
159 startTime => $s->[0],
161 action => 'chapterChange',
163 carousel => 'theCarousel',
166 description => $s->[2],
167 src => sprintf('media/s/%dx%d/p%03d.jpg', $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
172 push @slide_t, $s->[0];
175 my $res = $prop->{width} . 'x' . $prop->{height};
177 foreach ( 0 .. $#slide_t ) {
178 push @{ $sync->{htmlEvents}->{'#slide'} }, {
179 startTime => $slide_t[$_],
180 endTime => $slide_t[$_ + 1] || $prop->{length},
181 html => sprintf( '<img src=media/s/%s/p%03d.jpg>', $res, $_ + 1 ),
185 my @slides_hires = glob 'www/media/s/hires/p*.jpg';
186 @slides_hires = glob 'shot*.png' unless @slides_hires;
189 foreach my $factor ( 4, 2, 1 ) {
190 my $w = $prop->{width} / $factor;
191 my $h = $prop->{height} / $factor;
193 my $path = "www/media/s/${w}x${h}";
194 $factor_s_path->{$factor} = $path;
198 warn "created $path\n";
200 foreach my $hires ( @slides_hires ) {
203 $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
205 my $im = Imager->new( file => $hires );
206 $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
207 warn "resized $file ", -s $file, " bytes\n";
213 my ( $slide_width, $slide_height );
215 my $im = Imager->new( file => $factor_s_path->{ 1 } . '/p001.jpg"')
216 || Imager->new( file => "shot0001.png" ) # from mplayer [s]
220 $slide_width = $im->getwidth / $slide_factor;
221 $slide_height = $im->getheight / $slide_factor;
223 warn "can't find first slide default to 1/$slide_factor of video size\n";
224 $slide_width = $prop->{width} / $slide_factor;
225 $slide_height = $prop->{height} / $slide_factor;
232 width => $slide_width,
233 height => $slide_height,
237 $html5tv->{video_tags} =
244 push @{ $html5tv->{sync}->{customEvents} }, {
245 startTime => $s->[0],
247 action => 'additional_video',
251 description => $s->[2],
252 src => "media/s/$s->[3]/00000001.jpg",
258 <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
259 <source src="media/$_->[3]" />
266 sub customEvents_sorted {
268 if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
270 warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
274 sort { $a->{startTime} <=> $b->{startTime} }
275 @{ $html5tv->{sync}->{customEvents} }
280 $_->{args}->{index} = $index++ foreach customEvents_sorted;
282 warn "last customEvent $index\n";
284 $html5tv->{subtitles_table}
285 = qq|<table id="subtitles">|
288 <tr id="sub_$_->{index}">
289 <td class="seek_video">$_->{startTime}</td>
290 <td class="seek_video">$_->{endTime}</td>
291 <td>$_->{args}->{title}</td>
296 . qq|</table><a href="media/video.srt">download subtitles</a>|
299 my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
300 my $hcal_path = 'www/media/hcalendar.html';
301 if ( -e $hcal_path ) {
302 $html5tv->{hcalendar} = read_file $hcal_path;
303 my $tree = HTML::TreeBuilder->new;
304 $tree->parse_file($hcal_path);
305 if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
306 $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
310 warn "# html5tv ", dump $html5tv;
312 my $sync_path = 'www/media/video.js';
313 write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
314 warn "sync $sync_path ", -s $sync_path, " bytes\n";
316 my $html = read_file 'www/tv.html';
317 $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
318 warn "no interpolation in template!";
320 write_file 'www/media.html', $html;
322 my $carousel_width = $prop->{width} + $slide_width - 80;
323 $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
324 my $carousel_height = $slide_height + 2;
326 write_file 'www/media/video.css', qq|
328 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
329 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
330 width: ${carousel_width}px;
331 height: ${carousel_height}px;
334 .jcarousel-skin-ie7 .jcarousel-item {
335 width: ${slide_width}px;
336 height: ${slide_height}px;
341 background-color: #ffc;
345 width: $prop->{width}px;
346 height: $prop->{height}px;
347 font-family: Arial, Helvetica, sans-serif;
348 margin: 0 10px 0px 0;
362 padding: 1px 10px 5px 10px ;
365 width: $html5tv->{video}->{width}px;
370 .jcarousel-skin-ie7 .jcarousel-item img:hover {
371 // border-color: #555 !important;
374 .jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
375 visibility: visible !important;
378 .jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
384 font-family: Arial, Verdana;
391 padding: 2px 0 2px 0;
392 width: ${slide_width}px;
397 font-family: monospace;
408 my $hh = int( $t / 3600 );
410 my $mm = int( $t / 60 );
412 my $srt = sprintf "%02d:%02d:%04.1f", $hh, $mm, $t;
423 foreach my $s ( @subtitles ) {
425 . t_srt( $s->[0] ) . " --> " . t_srt( $s->[1] ) . "\n"
431 write_file $subtitles, $srt;
432 YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
434 print $to_mplayer "sub_remove\n";
435 print $to_mplayer qq|sub_load "$subtitles"\n|;
436 print $to_mplayer "sub_select 1\n";
440 @subtitles = YAML::LoadFile "$subtitles.yaml";
441 warn "subtitles ", dump @subtitles;
445 load_subtitles if -e "$subtitles.yaml";
448 print $to_mplayer qq|pause\n|;
450 system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
455 print $to_mplayer qq|pause\n|;
459 warn "subtitles ", dump( @subtitles );
462 $subtitles[ $#subtitles ]->[2] = $line if defined $line;
468 preroll $subtitles[ $#subtitles ]->[0], $line;
472 print $to_mplayer qq|get_property time_pos\n|;
473 my $pos = <$from_mplayer>;
474 if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
475 warn "# time_pos $1\n";
482 sprintf "%1.5f - %1.5f %s %s\n", @$s, join(' | ',@_);
487 my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
488 warn "<<<< subtitle ", sub_fmt $s;
489 preroll $s->[0], $s->[2];
490 # print $to_mplayer "set_property time_pos $s->[0]\n";
494 my $pos = time_pos + $preroll;
495 my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
496 warn ">>>> subtitle ", sub_fmt $s;
497 preroll $s->[0], $s->[2];
498 # print $to_mplayer "set_property time_pos $s->[0]\n";
501 sub current_subtitle {
502 my $callback = shift;
505 foreach my $nr ( 0 .. $#subtitles ) {
506 my $s = $subtitles[$nr];
507 if ( $s->[0] <= $pos && $s->[1] >= $pos ) {
508 warn sub_fmt $s, $pos;
510 $callback->( $visible, $pos ) if $callback;
511 return ( $visible, $pos );
514 warn "# $pos no visible subtitle\n";
519 current_subtitle( sub {
520 my ( $nr, $pos ) = @_;
521 my $new_start = $subtitles[$nr]->[0] += $offset;
522 warn "subtitle $nr $pos $offset $new_start\n";
524 preroll $new_start, "$pos $offset $new_start";
530 # XXX main epool loop
534 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
536 warn "no events" unless $events;
538 foreach my $e ( @$events ) {
539 # warn "# event: ", dump($e), $/;
541 my $fileno = $e->[0];
543 if ( $fileno == fileno STDIN ) {
545 sysread STDIN, $chr, 1;
547 } elsif ( $fileno == fileno $from_mplayer ) {
549 sysread $from_mplayer, $chr, 1;
552 if ( $chr =~ m{[\n\r]} ) {
554 exit if $line =~ m{Exiting};
556 if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
558 warn "prop $1 = $2\n";
559 } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
561 # XXX keyboard shortcuts
564 : $1 eq ',' ? add_subtitle
565 : $1 eq 'F1' ? prev_subtitle
566 : $1 eq 'F2' ? move_subtitle( -0.3 )
567 : $1 eq 'F3' ? move_subtitle( +0.3 )
568 : $1 eq 'F4' ? next_subtitle
569 : $1 eq 'F5' ? save_subtitles
570 : $1 eq 'F9' ? add_subtitle
571 : $1 eq 'F12' ? edit_subtitles
575 } elsif ( $line =~ m{EDL}i ) {
577 print $to_mplayer qq|osd_show_text "$line"\n|;
579 if ( my $pos = time_pos ) {
580 if ( $line =~ m{start}i ) {
581 push @subtitles, [ $pos, $pos, '-' ];
583 $subtitles[ $#subtitles ]->[1] = $pos;
594 } elsif ( $fileno == fileno $to_mplayer ) {
597 die "invalid fileno $fileno";