Chris Paget, Karsten Nohl GSM: SRSLY? 26C3, Day 1 - 2009-12-27
[HTML5TV.git] / bin / mplayer.pl
index 6eb4a12..160769f 100755 (executable)
@@ -3,6 +3,8 @@
 use warnings;
 use strict;
 
+use autodie;
+
 use IPC::Open3 qw(open3);
 use IO::Select;
 use Data::Dump qw(dump);
@@ -30,10 +32,10 @@ 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{\.og[vg]$}i ) {
+} elsif ( -f $movie && $movie =~ m{\.og[vg]\b}i ) {
        my $movie_master = $movie;
        $movie = base_dir($movie) . '/video.ogv';
-       unlink $movie;
+       unlink $movie if -e $movie;
        symlink base_name($movie_master), $movie;
        warn "symlink video.ogv -> $movie_master";
 } else {
@@ -154,6 +156,15 @@ sub fmt_mmss {
        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} ) {
@@ -218,21 +229,6 @@ sub html5tv {
                push @slide_t, $s->[0];
        }
 
-       if ( @frames ) {
-               rmdir $_ foreach glob "$media_dir/s/[124]";
-               my $hires = "$media_dir/s/hires";
-               mkdir $hires unless -e $hires;
-
-               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 ( 0 .. $#slide_t ) {
                my $slide_nr = $_ + 1;
                push @{ $sync->{htmlEvents}->{'#slide'} }, {
@@ -242,16 +238,44 @@ sub html5tv {
                };
        }
 
-       my @slides_hires = glob "$media_dir/s/hires/*";
 
-       my $path = "$media_dir/s";
+       my $max_slide_height = 480; # XXX
+
+       my $path  = "$media_dir/s";
+       my $hires = "$media_dir/s/hires";
 
        if ( ! -d $path ) {
-               warn "create slides images in $path";
+               warn "create slides in $path";
                mkdir $path;
+               mkdir $hires;
+
+               my $path = "$media_dir/presentation.pdf";
+               $path = $media_dir . '/' . readlink($path) if -l $path;
+
+               if ( -e $path ) {
+                       warn "render pdf slides from $path\n";
+                       system "pdftoppm -png -r 100 $path $hires/p";
+               }
        }
 
-       my $max_slide_height = 480; # XXX
+
+       if ( @frames ) {
+               warn "create slides from video frames";
+               rmdir $_ foreach glob "$media_dir/s/[124]";
+
+               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: $!";
+               }
+
+       }
+
+
+       my @slides_hires = glob "$hires/*";
+
 
        foreach my $hires ( @slides_hires ) {
 
@@ -321,7 +345,8 @@ sub html5tv {
        }
 
 
-       my ($slide_width, $slide_height, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) );
+       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,
@@ -330,6 +355,10 @@ sub html5tv {
                        width => $slide_width,
                        height => $slide_height,
                },
+               carousel => {
+                       width => $c_slide_width,
+                       height => $c_slide_height,
+               },
        };
 
        $html5tv->{html}->{video_tags} =
@@ -347,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 => '',
                                        },
                                };
@@ -390,7 +419,7 @@ sub html5tv {
                                my $s = $_->{startTime};
                                my $e = $_->{endTime};
                                my $i = $_->{index};
-                               my $t = $_->{args}->{title};
+                               my $t = html_escape( $_->{args}->{title} );
                                my $slide = '';
                                $slide = $1 if $t =~ s{\s*\[(\d+)\]\s*}{};
 
@@ -442,9 +471,10 @@ sub html5tv {
        warn "sync $sync_path ", -s $sync_path, " bytes\n";
 
        # video + full-sized slide on right
-       my $carousel_width = $prop->{width} + ( $slide_width * $slide_factor );
-       $carousel_width -= $carousel_width % ( $slide_width + 6 ); # round to full slide
-       my $carousel_height =   $slide_height + 2;
+       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|
 
@@ -455,75 +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;
-}
-
-.slide {
-       text-align: right;
-       color: #888;
-       font-family: monospace;
-}
 
-.seek_video {
-       text-align: right;
-       font-family: monospace;
+.jcarousel-skin-ie7 .jcarousel-item div.thumbnailOverlay {
+       width: ${c_slide_width}px;
 }
 
        |;
@@ -585,7 +572,16 @@ sub load_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;
 }
 
@@ -785,7 +781,16 @@ sub from_mplayer {
 
 }
 
-push @to_mplayer, "get_property $_\n" foreach grep { ! $prop->{$_} } ( qw/metadata video_codec video_bitrate width height fps length/ );
+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};
+
+push @to_mplayer, "get_property $_\n" foreach grep { ! $prop->{$_} } @mplayer_prop;
 
 my $t = time;
 my $line;