added presentation.pdf symlinks
[HTML5TV.git] / bin / mplayer.pl
index 9f69a76..806820d 100755 (executable)
@@ -3,15 +3,24 @@
 use warnings;
 use strict;
 
+use autodie;
+
 use IPC::Open3 qw(open3);
-use IO::Epoll;
+use IO::Select;
 use Data::Dump qw(dump);
 use File::Slurp;
 use YAML;
 use JSON;
-use HTML::TreeBuilder;
-use Imager;
+use Graphics::Magick;
+use Time::HiRes qw(time);
+use File::Path qw(rmtree);
+
+use lib 'lib';
+use HTML5TV::Slides;
+use HTML5TV::hCalendar;
 
+my $debug = $ENV{DEBUG} || 0;
+my $generate = $ENV{GENERATE} || 0;
 
 my $movie = shift @ARGV;
 
@@ -23,17 +32,12 @@ if ( ! $movie && -e 'media/_editing' ) {
        warn "using media/_editing -> $movie\n";
 } elsif ( -d $movie && $movie =~ m{media/} ) {
        $movie .= '/video.ogv';
-} elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
+} elsif ( -f $movie && $movie =~ m{\.og[vg]\b}i ) {
        my $movie_master = $movie;
        $movie = base_dir($movie) . '/video.ogv';
-       if ( ! -e $movie ) {
-               symlink base_name($movie_master), $movie;
-               warn "symlink video.ogv -> $movie_master";
-       } else {
-               warn "using symlink video.ogv -> ", readlink $movie;
-       }
-} elsif ( -f $movie ) {
-       warn "using video $movie";
+       unlink $movie;
+       symlink base_name($movie_master), $movie;
+       warn "symlink video.ogv -> $movie_master";
 } else {
        die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
 }
@@ -55,32 +59,37 @@ my $preroll = 3;
 
 my $slide_factor = 4; # 1/4 size of video
 
+my $min_slide_height = 480;
+
 our $to_mplayer;
 our $from_mplayer;
 our $err_mplayer;
 our $prop = {};
 
-my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
+my @mplayer_command = (
         'mplayer',
                '-slave', '-idle',
-               '-quiet',
+#              '-quiet',
+#              '-msglevel', 'demux=9', '-msgmodule',
                '-edlout', $edl,
                '-osdlevel', 3,
                '-vf' => 'screenshot',
 );
 
-my $epfd = epoll_create(10);
+push @mplayer_command, qw/ -vo null -ao null / if $generate;
+
+my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer, @mplayer_command );
 
-epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN         , EPOLLIN  ) >= 0 || die $!;
-epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN  ) >= 0 || die $!;
-#epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer   , EPOLLOUT ) >= 0 || die $!;
+my $select = IO::Select->new();
+#$select->add( \*STDIN );
+$select->add( $from_mplayer );
+#$select->add( $err_mplayer );
 
 sub load_subtitles;
 
 sub load_movie {
        warn "$movie ", -s $movie, " bytes $edl\n";
        print $to_mplayer qq|loadfile "$movie"\n|;
-       print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
        load_subtitles;
 }
 
@@ -100,15 +109,18 @@ sub focus_term {
        system "xdotool windowactivate $term_id";
 }
 
+our $pos;
+
 sub preroll {
-       my ( $pos, $osd ) = @_;
+       my ( $to, $osd ) = @_;
        $osd =~ s{\W+}{ }gs;
-       warn "PREROLL $pos $osd\n";
-       print $to_mplayer "osd_show_text \"PREROLL $osd\" ", $preroll * 1000, "\n";
-       my $to = $pos - $preroll;
-       $to = 0 if $to < 0;
-       print $to_mplayer "set_property time_pos $to\n";
+       my $preroll_to = $to - $preroll;
+       $preroll_to = 0 if $preroll_to < 0;
+       print $to_mplayer "set_property time_pos $preroll_to\n";
+       my $osd_ms = ( $to - time_pos() ) * 1000;
        print $to_mplayer "get_property time_pos\n";
+       print $to_mplayer "osd_show_text \"PREROLL $osd\"\n"; # $osd_ms\n";
+       warn "PREROLL $to -> $pos [$osd_ms] $osd\n";
        print $to_mplayer "play\n";
 }
 
@@ -132,10 +144,31 @@ sub slide_jpg {
        sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
 }
 
+sub oggThumb {
+       my $video = shift;
+       my $file  = shift;
+       my $t = join(',', @_);
+       system "oggThumb -t $t -o jpg -n $file $video";
+}
+
+sub fmt_mmss {
+       my $t = shift;
+       return sprintf('%02d:%02d', int($t/60), int($t%60));
+}
+
+my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
+my $escape_re  = join '|' => keys %escape;
+sub html_escape {
+       my $what = join('',@_);
+       $what =~ s/($escape_re)/$escape{$1}/gs;
+       warn "XXX html_escape $what\n";
+       return $what;
+}
+
 sub html5tv {
 
        if ( ! $prop->{width} || ! $prop->{height} ) {
-               warn "SKIP no size yet\n";
+               warn "SKIP no size yet\n" if $debug;
                return;
        }
 
@@ -144,12 +177,14 @@ sub html5tv {
                return;
        }
 
+       warn "html5tv";
 
        my $sync;
 
        my @slide_t;
 
        my @videos;
+       my @frames;
 
        foreach my $s ( @subtitles ) {
                push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
@@ -165,23 +200,13 @@ sub html5tv {
                                warn "MISSING $path: $!\n";
                        } else {
                                my $frame_dir = "$media_dir/s/$video";
-                               system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 $media_dir/$video"
+                               system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss 0 -ao null -really-quiet $media_dir/$video"
                                        if ! -e $frame_dir;
                                push @videos, [ @$s, $video ];
                        }
                } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) {
-                       my ( $nr, $t ) = ( $1, $2 );
-
-                       my $hires = "$media_dir/s/hires";
-                       mkdir $hires unless -e $hires;
-
-                       my $shot_path = sprintf "$hires/s%03d.jpg", $nr;
-                       if ( ! -e $shot_path ) {
-                               my $frame_dir = "$media_dir/s/shot/";
-                               system "mplayer -vo jpeg:outdir=$frame_dir,quality=95 -frames 1 -ss $t $movie";
-                               rename "$media_dir/s/shot/00000001.jpg", $shot_path;
-                               warn "created $shot_path from $movie at $t for slide $nr\n";
-                       }
+                       push @frames, [ $2, $1 ] unless -e "$media_dir/s/hires/f$1.jpg";
+                       next;
                }
 
                next unless $s->[2] =~ m{\[(\d+)\]};
@@ -213,57 +238,116 @@ sub html5tv {
                };
        }
 
-       my @slides_hires = glob "$media_dir/s/hires/*";
 
-       foreach my $factor ( 4, 2, 1 ) {
-               my $w = $prop->{width}  / $factor;
-               my $h = $prop->{height} / $factor;
+       my $max_slide_height = 480; # XXX
+
+       my $path  = "$media_dir/s";
+       my $hires = "$media_dir/s/hires";
+
+       if ( ! -d $path ) {
+               warn "create slides in $path";
+               mkdir $path;
+               mkdir $hires;
+
+               my $path = "$media_dir/presentation.pdf";
+               $path = $media_dir . '/' . readlink($path) if -l $path;
 
-               my $path = "$media_dir/s";
-               if ( ! -d $path ) {
-                       warn "create slides imaes in $path";
-                       mkdir $path;
+               if ( -e $path ) {
+                       warn "render pdf slides from $path\n";
+                       system "pdftoppm -png -r 100 $path $hires/p";
                }
+       }
+
 
-               $path .= '/' . $factor;
+       if ( @frames ) {
+               warn "create slides from video frames";
+               rmdir $_ foreach glob "$media_dir/s/[124]";
 
-               if ( ! -d $path ) {
-                       mkdir $path;
-                       warn "created $path\n";
+               oggThumb $movie, "$hires/.f%.jpg", map { $_->[0] } @frames;
 
+               foreach my $i ( 0 .. $#frames ) {
+                       my $from = "$hires/.f$i.jpg";
+                       my $to   = "$hires/f" . $frames[$i]->[1] . '.jpg';
+                       rename $from, $to || warn "can't rename $from -> $to: $!";
                }
 
-               foreach my $hires ( @slides_hires ) {
+       }
+
+
+       my @slides_hires = glob "$hires/*";
+
+
+       foreach my $hires ( @slides_hires ) {
+
+               my ($slide_width, $slide_height) = Graphics::Magick->new->Ping( $hires );
+               my $slide_aspect = $slide_width / $slide_height;
+
+               my $nr = $1 if $hires =~ m{(\d+)\.\w+$} || warn "can't find number in $hires";
+               next unless $nr;
+
+               my $im;
+
+               foreach my $factor ( 1, 2, 4 ) {
+
+                       mkdir "$path/$factor" unless -e "$path/$factor";
 
-                       my $nr = $1 if $hires =~ m{^\s+(\d+)\.\w+$} || warn "can't find number in $hires";
-                       next unless $nr;
                        my $file = slide_jpg( $factor => $nr );
-                       warn "slide $hires -> $file\n";
                        next if -e $file;
 
-                       if ( my $im = Imager->new( file => $hires ) ) {
-                               $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
-                               warn "resized $file ", -s $file, " bytes\n";
-                       } else {
-                               die "can't open $hires: $!";
+                       my $w = int( $max_slide_height / $factor * $slide_aspect );
+                       my $h = int( $max_slide_height / $factor );
+
+                       warn "slide [$nr] $hires -> ${w}x${h} $file\n";
+
+                       if ( ! $im ) {
+                               warn "loading $hires ", -s $hires, " bytes\n";
+                               $im = Graphics::Magick->new;
+                               $im->ReadImage( $hires );
                        }
+                       $im->Resize( width => $w, height => $h, filter => 13, blur => 0.9 );
+
+                       my $c = 1; # $h / 10;
+                       my %info = (
+                               font => 'Sans', pointsize => $h / 10,
+                               #text => "$factor = $w*$h",
+                               text => " $nr ",
+                               y => $c,
+                               x => $c,
+                       );
+
+                       if (0) {
+
+                       #warn "# info ", dump %info;
+                       #warn dump $im->QueryFontMetrics( %info );
+                       my ($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) = $im->QueryFontMetrics( %info );
+                       my $background = Graphics::Magick->new( size => $width . 'x' . $height );
+                       $background->ReadImage( 'xc:black' );
+                       $im->Composite( image => $background, compose => 'Over', x => $c, y => $c, opacity => 75 );
+                       $info{y} += $ascender;
+                       $im->Annotate( fill => 'yellow', %info );
+
+                       } # Annotate
+
+                       $im->Write( filename => $file );
                }
 
        }
 
-       my ( $slide_width, $slide_height );
-
-       my $im = Imager->new( file => slide_jpg( 1 => 1 ) );
 
-       if ( $im ) {
-               $slide_width  = $im->getwidth  / $slide_factor;
-               $slide_height = $im->getheight / $slide_factor;
-       } else {
-               warn "can't find first slide default to 1/$slide_factor of video size\n";
-               $slide_width  = $prop->{width}  / $slide_factor;
-               $slide_height = $prop->{height} / $slide_factor;
+       my $bars = "$path/bars.png";
+       if ( 1 || ! -e $bars ) { # FIXME
+               my $im = Graphics::Magick->new;
+               $im->ReadImage( "$media_dir/../SMPTE_Color_Bars.svg" );
+               my ($slide_width, $slide_height) = Graphics::Magick->new->Ping( slide_jpg( 1 => 1 ) );
+               $im->Resize( width => $slide_width, height => $slide_height );
+               $im->Write( filename => $bars );
+               warn "created $bars ", -s $bars, " bytes\n";
        }
 
+
+       my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( 1 => 1 ) );
+       my ($c_slide_width, $c_slide_height) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
+
        my $html5tv = {
                sync => $sync,
                video => $prop,
@@ -271,6 +355,10 @@ sub html5tv {
                        width => $slide_width,
                        height => $slide_height,
                },
+               carousel => {
+                       width => $c_slide_width,
+                       height => $c_slide_height,
+               },
        };
 
        $html5tv->{html}->{video_tags} =
@@ -288,7 +376,7 @@ sub html5tv {
                                                id => $id,
                                                title => $s->[2],
                                                description => $s->[2],
-                                               src => "$media_dir/s/1/00000001.jpg",
+                                               src => "$media_dir/s/$s->[3]/00000001.jpg",
                                                href => '',
                                        },
                                };
@@ -302,55 +390,72 @@ sub html5tv {
                )
        ;
 
-       sub customEvents_sorted {
-
-               if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
-                       my $max = 
-                       warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
-                       return;
-               }
+       my @customEvents_sorted;
 
+       if ( ref $html5tv->{sync}->{customEvents} ne 'ARRAY' ) {
+               my $max = 
+               warn "ERROR: no slide markers [1] .. [", scalar @slides_hires, "] in subtitles\n";
+               return;
+       } else {
+               @customEvents_sorted =
                sort { $a->{startTime} <=> $b->{startTime} }
                @{ $html5tv->{sync}->{customEvents} }
+               ;
        }
 
        my $index = 1;
 
-       $_->{args}->{index} = $index++ foreach customEvents_sorted;
+       $_->{args}->{index} = $index++ foreach @customEvents_sorted;
 
        warn "last customEvent $index\n";
 
        $html5tv->{html}->{subtitles_table}
-               = qq|<table id="subtitles">|
+               = qq|
+                       <table id="subtitles">
+                       <tr><th>&#8676;</th><th>&#8677;</th><th>#</th><th>slide</th></tr>
+               |
                . join("\n",
-                       map { qq|
-                               <tr id="sub_$_->{index}">
-                                       <td class="seek_video">$_->{startTime}</td>
-                                       <td class="seek_video">$_->{endTime}</td>
-                                       <td>$_->{args}->{title}</td>
+                       map {
+                               my $s = $_->{startTime};
+                               my $e = $_->{endTime};
+                               my $i = $_->{index};
+                               my $t = html_escape( $_->{args}->{title} );
+                               my $slide = '';
+                               $slide = $1 if $t =~ s{\s*\[(\d+)\]\s*}{};
+
+                               my ( $s_f, $e_f ) = map { fmt_mmss $_ } ( $s, $e );
+
+                               qq|
+                               <tr id="sub_$i">
+                                       <td class="seek_video"><a href="#$s">$s_f</a></td>
+                                       <td class="seek_video"><a href="#$e">$e_f</a></td>
+                                       <td class="slide">$slide</td>
+                                       <td>$t</td>
                                </tr>
-                       | }
-                       customEvents_sorted
+                               |
+                       }
+                       @customEvents_sorted
                )
-               . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
+               . qq|</table>|
                ;
 
        my $hCalendar = '<div style="color: red">Create <tt>hCalendar.html</tt> to fill this space</div>';
-       my $hcal_path = '$media_dir/hCalendar.html';
+       my $hcal_path = "$media_dir/hCalendar.html";
        if ( -e $hcal_path ) {
                $html5tv->{hCalendar} = read_file $hcal_path;
-               my $tree = HTML::TreeBuilder->new;
-               $tree->parse_file($hcal_path);
-               if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
-                       $html5tv->{title} = $vevent->look_down( class=> 'summary' )->as_trimmed_text;
-               }
+               my $hcal = HTML5TV::hCalendar->new( $hcal_path );
+               $html5tv->{title} = $hcal->summary;
        }
 
-       warn "# html5tv ", dump $html5tv;
+       foreach my $file ( qw/video.ogv presentation.pdf video.srt video.srt.yaml/ ) {
+               my $path = "$media_dir/$file";
+               my $type = 'binary';
+               $type = $1 if $file =~ m{\.([^\.]+)$};
+               my $size = -s $path;
+               $html5tv->{html}->{download} .= qq|<a href="$path" title="$file $size bytes">$type</a> |;
+       }
 
-       my $sync_path = "$media_dir/video.js";
-       write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
-       warn "sync $sync_path ", -s $sync_path, " bytes\n";
+       warn "# html5tv ", dump $html5tv if $debug;
 
        my $html = read_file 'www/tv.html';
        $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
@@ -360,9 +465,16 @@ sub html5tv {
        $html =~ s{media/_editing}{media/$media_part}gs;
        write_file "www/$media_part.html", $html;
 
-       my $carousel_width = $prop->{width} + $slide_width - 80;
-       $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
-       my $carousel_height =   $slide_height + 2;
+       my $sync_path = "$media_dir/video.js";
+       delete $html5tv->{html};
+       write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
+       warn "sync $sync_path ", -s $sync_path, " bytes\n";
+
+       # video + full-sized slide on right
+       my $carousel_width = $prop->{width} + $slide_width;
+       $carousel_width -= $carousel_width % ( $c_slide_width + 6 ); # round to full slide
+       my $carousel_height = $c_slide_height + 2;
+
 
        write_file "$media_dir/video.css", qq|
 
@@ -373,69 +485,32 @@ sub html5tv {
 }
 
 .jcarousel-skin-ie7 .jcarousel-item {
-       width: ${slide_width}px;
-       height: ${slide_height}px;
+       width: ${c_slide_width}px;
+       height: ${c_slide_height}px;
        margin: 0 2px 0 2px;
 }
 
-.active {
-       background-color: #ffc;
-}
-
 div#videoContainer {
-       width: $prop->{width}px;
-       height: $prop->{height}px;
-       font-family: Arial, Helvetica, sans-serif;
+//     width: $prop->{width}px;
+//     height: $prop->{height}px;
        margin: 0 10px 0px 0;
-       position: relative;
-       display: inline;
 }
 
 
-div#subtitle {
-       bottom: 24px;
-       color: white;
-       font-size: 100%;
-       font-weight: bold;
-       height: 22px;
-       line-height: 1em;
-       margin: 0  0 0 0;
-       padding: 1px 10px 5px 10px ;
-       position: absolute;
-       text-align: center;
-       width: $html5tv->{video}->{width}px;
+div#slide_border, div#slide {
+       width: ${slide_width}px;
+       height: ${slide_height}px;
 }
 
 
-
-.jcarousel-skin-ie7 .jcarousel-item img:hover {
-//     border-color: #555 !important;
+div#subtitle {
+       width: $html5tv->{video}->{width}px;
 }
 
-.jcarousel-skin-ie7 .jcarousel-item:hover div.thumbnailOverlay {
-       visibility: visible !important;
-}
 
-.jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
-       background: black;
-       bottom: 1px;
-       color: #00EEFF;
-       visibility: hidden;
-       font-size: 10px;
-       font-family: Arial, Verdana;
-       font-weight: bold;
-       line-height: 0.9em;
-       opacity: 0.5;
-       position: absolute;
-       text-align: center;
-       z-index: 10;
-       padding: 2px 0 2px 0;
-       width: ${slide_width}px;
-}
 
-.seek_video {
-       text-align: right;
-       font-family: monospace;
+.jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
+       width: ${c_slide_width}px;
 }
 
        |;
@@ -455,6 +530,8 @@ sub t_srt {
        return $srt;
 }
 
+my @to_mplayer;
+
 sub save_subtitles {
 
        html5tv || return;
@@ -472,11 +549,15 @@ sub save_subtitles {
        write_file $subtitles, $srt;
        YAML::DumpFile "$subtitles.yaml", sort { $a->[0] <=> $b->[0] } @subtitles;
 
-       print $to_mplayer "sub_remove\n";
-       print $to_mplayer qq|sub_load "$subtitles"\n|;
-       print $to_mplayer "sub_select 1\n";
+       push @to_mplayer
+               , "sub_remove\n"
+               , qq|sub_load "$subtitles"\n|
+               , "sub_select 1\n"
+               ;
 }
 
+sub annotate_subtitles;
+
 sub load_subtitles {
        if ( ! -e "$subtitles.yaml" ) {
                warn "no subtitles $subtitles to load\n";
@@ -484,28 +565,88 @@ sub load_subtitles {
        }
        @subtitles = YAML::LoadFile "$subtitles.yaml";
        warn "subtitles ", dump @subtitles;
+       annotate_subtitles;
        save_subtitles;
 }
 
 sub edit_subtitles {
        print $to_mplayer qq|pause\n|;
        focus_term;
-       system( qq|vi "$subtitles.yaml"| ) == 0 and load_subtitles;
+       my $line = 1;
+       my $jump_to = 1;
+       open( my $fh, '<', "$subtitles.yaml" ) || die $1;
+       while(<$fh>) {
+               last if /^-\s([\d\.]+)/ && $1 > $pos;
+               $jump_to = $line - 1 if /^---/;
+               $line++;
+       }
+       close($fh);
+       system( qq|vi +$jump_to "$subtitles.yaml"| ) == 0 and load_subtitles;
        focus_mplayer;
 }
 
+
+my @slide_titles;
+if ( -e "$media_dir/presentation.txt" ) {
+       my $slides = read_file "$media_dir/presentation.txt";
+       my @s = ( map { [ split(/[\n\r]+/, $_) ] } split(/\f/, $slides) );
+
+       my $slide_line = 0;
+       $slide_line++ if $s[1]->[$slide_line] eq $s[2]->[$slide_line]; # skip header
+
+       foreach my $s ( @s ) {
+               push @slide_titles, $s->[$slide_line];
+       }
+
+       warn "# slides titles ", dump(@slide_titles);
+}
+
+sub annotate_subtitles {
+       return unless @slide_titles;
+       foreach my $s ( @subtitles ) {
+               if ( $s->[2] =~ m{^\[(\d+)\]$} ) {
+                       if ( my $title = $slide_titles[ $1 - 1 ] ) {
+                               $s->[2] = "[$1] " . substr($title,0,40);
+                               warn "annotated [$1] $title\n";
+                       }
+               }
+       }
+}
+
+
 sub add_subtitle {
+
+       my $last_slide;
+       foreach ( 0 .. $#subtitles ) {
+               my $i = $#subtitles - $_;
+               $last_slide = $1 if $subtitles[$i]->[2] =~ m/\[(\d+)\]/;
+               last if $last_slide;
+       }
+
+       if ( $last_slide && $subtitles[ $#subtitles ]->[2] ne '-' || ! @subtitles ) {
+
+               # quick add next slide for Takahashi method presentations
+               # with a lot of transitions
+               my $nr = $last_slide + 1;
+               my $text = "[$nr]";
+               $text .= ' ' . $slide_titles[ $nr - 1 ] if defined $slide_titles[ $nr - 1 ];
+               warn "add slide $text";
+               push @subtitles, [ $pos, $pos + 1, $text ];
+               save_subtitles;
+               return;
+
+       }
+
        print $to_mplayer qq|pause\n|;
 
+       warn "subtitles ", dump( @subtitles ), "\nnext: [", $last_slide + 1, "]\n";
+
        focus_term;
 
-       warn "subtitles ", dump( @subtitles );
        print "## ";
        my $line = <STDIN>;
        $subtitles[ $#subtitles ]->[2] = $line if defined $line;
 
-       save_subtitles;
-
        focus_mplayer;
 
        preroll $subtitles[ $#subtitles ]->[0], $line;
@@ -513,9 +654,10 @@ sub add_subtitle {
 
 sub time_pos {
        print $to_mplayer qq|get_property time_pos\n|;
-       my $pos = <$from_mplayer>;
-       if ( $pos =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
+       my $line = <$from_mplayer>;
+       if ( $line =~ m{^ANS_time_pos=(\d+\.\d+)} ) {
                warn "# time_pos $1\n";
+               $pos = $1;
                return $1;
        }
 }
@@ -526,15 +668,13 @@ sub sub_fmt {
 }
 
 sub prev_subtitle {
-       my $pos = time_pos;
-       my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
+       my $s = ( grep { $_->[0] < $pos } @subtitles )[-1] || return;
        warn "<<<< subtitle ", sub_fmt $s;
        preroll $s->[0], $s->[2];
 #      print $to_mplayer "set_property time_pos $s->[0]\n";
 }
 
 sub next_subtitle {
-       my $pos = time_pos + $preroll;
        my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
        warn ">>>> subtitle ", sub_fmt $s;
        preroll $s->[0], $s->[2];
@@ -543,7 +683,6 @@ sub next_subtitle {
 
 sub current_subtitle {
        my $callback = shift;
-       my $pos = time_pos;
        my $visible;
        foreach my $nr ( 0 .. $#subtitles ) {
                my $s = $subtitles[$nr];
@@ -574,95 +713,122 @@ sub move_subtitle {
 
 load_movie;
 
-while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
-
-       warn "no events" unless $events;
-
-       foreach my $e ( @$events ) {
-#              warn "# event: ", dump($e), $/;
+my $slides = HTML5TV::Slides->new(
+       sub {
+               my $t = shift || return;
+               my $nr = 0;
+               foreach my $s ( @subtitles ) {
+                       $nr = $1 if $s->[2] =~ m{\[(\d+)\]} && $s->[0] < $t;
+               }
+               return $nr;
+       }
+);
 
-               my $fileno = $e->[0];
 
-               if ( $fileno == fileno STDIN ) {
-                       my $chr;
-                       sysread STDIN, $chr, 1;
-                       print $chr;
-               } elsif ( $fileno == fileno $from_mplayer ) {
-                       my $chr;
-                       sysread $from_mplayer, $chr, 1;
-                       print $chr;
+sub from_mplayer {
+       my $line = shift;
+
+       if ( $line =~ m{V:\s*(\d+\.\d+)\s+} ) {
+               $pos = $1;
+               print "$pos\r";
+#              $pos = $1 if $1 > 0.2; # mplayer demuxer report fake position while seeking
+       } elsif ( $line =~ m{Exiting} ) {
+               exit;
+       } elsif ( $line =~ m{ANS_(\w+)=(\S+)} ) {
+               $prop->{$1} = $2;
+               warn "prop $1 = $2\n";
+       } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
+
+               # XXX keyboard shortcuts
+
+                 $1 eq 'c'  ? repl
+               : $1 eq ','  ? add_subtitle
+               : $1 eq 'F1' ? prev_subtitle
+               : $1 eq 'F2' ? move_subtitle( -0.3 )
+               : $1 eq 'F3' ? move_subtitle( +0.3 )
+               : $1 eq 'F4' ? next_subtitle
+               : $1 eq 'F5' ? save_subtitles
+               : $1 eq 'F9' ? add_subtitle
+               : $1 eq 'F12' ? edit_subtitles
+               : warn "CUSTOM $1\n"
+               ;
 
-                       if ( $chr =~ m{[\n\r]} ) {
+       } elsif ( $line =~ m{EDL}i ) {
 
-                               exit if $line =~ m{Exiting};
+               print $to_mplayer qq|osd_show_text "$line"\n|;
 
-                               if ( $line =~ m{ANS_(\w+)=(\S+)} ) {
-                                       $prop->{$1} = $2;
-                                       warn "prop $1 = $2\n";
-                               } elsif ( $line =~ m{No bind found for key '(.+)'} ) {
+               if ( $line =~ m{start}i ) {
+                       push @subtitles, [ $pos, $pos, '-' ];
+               } else {
+                       $subtitles[ $#subtitles ]->[1] = $pos;
+               }
+       } elsif ( $line =~ m{(shot\d+.png)} ) {
+               my $shot = $1;
+               warn "shot $pos $shot\n";
 
-                                       # XXX keyboard shortcuts
+               my @existing_slides = glob("$media_dir/s/hires/*");
+               my $nr = $#existing_slides + 2;
 
-                                         $1 eq 'c'  ? repl
-                                       : $1 eq ','  ? add_subtitle
-                                       : $1 eq 'F1' ? prev_subtitle
-                                       : $1 eq 'F2' ? move_subtitle( -0.3 )
-                                       : $1 eq 'F3' ? move_subtitle( +0.3 )
-                                       : $1 eq 'F4' ? next_subtitle
-                                       : $1 eq 'F5' ? save_subtitles
-                                       : $1 eq 'F9' ? add_subtitle
-                                       : $1 eq 'F12' ? edit_subtitles
-                                       : warn "CUSTOM $1\n"
-                                       ;
+               push @subtitles, [ $pos, $pos, "slide:$nr shot:$pos" ];
 
-                               } elsif ( $line =~ m{EDL}i ) {
+               warn "slide $nr from video $pos file $shot\n";
+               save_subtitles;
+       } elsif ( $line =~ m{File not found} ) {
+               die $line;
+       } else {
+               warn "IGNORE $line\n"; # if $debug;
+       }
 
-                                       print $to_mplayer qq|osd_show_text "$line"\n|;
+}
 
-                                       if ( my $pos = time_pos ) {
-                                               if ( $line =~ m{start}i ) {
-                                                       push @subtitles, [ $pos, $pos, '-' ];
-                                               } else {
-                                                       $subtitles[ $#subtitles ]->[1] = $pos;
-                                               }
-                                       }
-                               } elsif ( $line =~ m{(shot\d+.png)} ) {
-                                       my $shot = $1;
-                                       my $t = time_pos;
-                                       warn "shot $t $shot\n";
+my @mplayer_prop = ( qw/metadata video_codec video_bitrate width height fps/ );
+warn "XXX $movie\n";
+if ( my $l = `oggLength $movie` ) {
+       $l = $l / 1000;
+       $prop->{length} = $l;
+       warn "$movie length ", fmt_mmss( $l );
+}
+push @mplayer_prop, 'length' unless $prop->{length};
 
-                                       my $dir = "$media_dir/s/shot";
-                                       if ( ! -e $dir ) {
-                                               mkdir $dir;
-                                               warn "created $dir\n";
-                                       }
+push @to_mplayer, "get_property $_\n" foreach grep { ! $prop->{$_} } @mplayer_prop;
 
-                                       my $hires = "$media_dir/s/hires";
-                                       mkdir $hires unless -e $hires;
+my $t = time;
+my $line;
 
-                                       my $shot_path = "$dir/$t.png";
-                                       rename $1, $shot_path;
-                                       my $nr = scalar glob("$hires/*") + 1;
+push @to_mplayer, 'pause' if $generate;
 
-                                       push @subtitles, [ $t, $t, "slide:$nr shot:$t" ];
+while ( 1 ) {
 
-                                       symlink $shot_path, sprintf("../hires/s%03d.png", $nr);
-                                       warn "slide $nr from video $t file $shot\n";
-                                       save_subtitles;
-                               }
+       my $dt = time - $t;
+       if ( abs($dt) > 0.2 ) {
+#warn "dt $dt\n";
+               $slides->show( $pos, $prop->{length}, @subtitles ) if $prop->{length};
+               $t = time;
+       }
 
+       foreach my $fd ( $select->can_read(0.1) ) {
+               if ( $fd == $from_mplayer ) {
+                       my $ch;
+                       sysread $from_mplayer, $ch, 1;
+                       if ( $ch =~ m{[\n\r]} ) {
+                               from_mplayer $line;
                                $line = '';
                        } else {
-                               $line .= $chr;
+                               $line .= $ch;
                        }
-
-
-               } elsif ( $fileno == fileno $to_mplayer ) {
-#                      warn "command";
                } else {
-                       die "invalid fileno $fileno";
+                       warn "unknown fd $fd";
                }
        }
 
+       if ( my $cmd = shift @to_mplayer ) {
+               warn ">>>> $cmd\n";
+               print $to_mplayer $cmd;
+       }
+
+       if ( $generate && html5tv() ) {
+               warn "generated\n";
+               exit;
+       }
 }