rename to _editing
[HTML5TV.git] / bin / mplayer.pl
index 2fe18b5..de4e1fa 100755 (executable)
@@ -9,11 +9,38 @@ use Data::Dump qw(dump);
 use File::Slurp;
 use YAML;
 use JSON;
+use HTML::TreeBuilder;
+use Imager;
+
+
+my $movie = shift @ARGV;
+
+sub base_dir { $1 if $_[0] =~ m{^(.+)/[^/]+$} }
+
+if ( ! $movie && -e 'media/_editing' ) {
+       $movie = 'media/' . readlink('media/_editing') . '/video.ogv';
+       warn "using media/_editing -> $movie\n";
+} elsif ( -d $movie && $movie =~ m{media/} ) {
+       $movie .= '/video.ogv';
+} elsif ( -f $movie && $movie !~ m{video\.ogv} ) {
+       my $movie_master = $movie;
+       my $to = $movie = base_dir($movie) . '/video.ogv';
+       $to =~ s{media/}{};
+       symlink $movie_master, $to;
+       warn "symlink $to -> $movie\n";
+} elsif ( -f $movie ) {
+       warn "using video $movie";
+} else {
+       die "Usage: $0 media/conference-Title_of_talk[/video.ogv'\n";
+}
+
+my $media_part = my $media_dir = base_dir($movie);
+$media_part =~ s{media/}{};
 
+unlink 'media/_editing';
+symlink $media_part, 'media/_editing';
 
-my $movie = shift @ARGV
-       || 'media/lpc-2009-network-namespaces/Pavel Emelyanov.ogg';
-#      || die "usage: $0 path/to/movie.ogv\n";
+warn "# media_part $media_part\n";
 
 my $edl = "/dev/shm/edl";
 my $subtitles = $movie;
@@ -21,6 +48,8 @@ $subtitles =~ s{\.\w+$}{.srt};
 
 my $preroll = 3;
 
+my $slide_factor = 4; # 1/4 size of video
+
 our $to_mplayer;
 our $from_mplayer;
 our $err_mplayer;
@@ -32,6 +61,7 @@ my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
                '-quiet',
                '-edlout', $edl,
                '-osdlevel', 3,
+               '-vf' => 'screenshot',
 );
 
 my $epfd = epoll_create(10);
@@ -40,10 +70,13 @@ 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 $!;
 
+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/ );
+       print $to_mplayer "get_property $_\n" foreach ( qw/metadata video_codec video_bitrate width height fps length/ );
+       load_subtitles;
 }
 
 
@@ -91,57 +124,294 @@ sub repl {
 our @subtitles;
 
 sub html5tv {
+
+       if ( ! $prop->{width} || ! $prop->{height} ) {
+               warn "SKIP no size yet\n";
+               return;
+       }
+
+       if ( ! @subtitles ) {
+               warn "SKIP no subtitles yet\n";
+               return;
+       }
+
+
        my $sync;
 
+       my @slide_t;
+
+       my @videos;
+
        foreach my $s ( @subtitles ) {
                push @{ $sync->{htmlEvents}->{'#subtitle'} }, {
                        startTime => $s->[0],
-                       endTime => $s->[1],
-                       html => $s->[2],
+                       endTime   => $s->[1],
+                       html      => $s->[2],
                };
+
+               if ( $s->[2] =~ m{video:(.+)} ) {
+                       my $video = $1;
+                       my $path = "$media_dir/$video";
+                       if ( ! -e $path ) {
+                               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"
+                                       if ! -e $frame_dir;
+                               push @videos, [ @$s, $video ];
+                       }
+               }
+
                next unless $s->[2] =~ m{\[(\d+)\]};
 
-               my $res = ( $prop->{width} / 4 ) . 'x' . ( $prop->{height} /4 );
 
                push @{ $sync->{customEvents} }, {
                        startTime => $s->[0],
-                       endTime => $s->[1],
-                       action => 'chapterChange',
+                       endTime   => $s->[1],
+                       action    => 'chapterChange',
                        args => {
                                carousel => 'theCarousel',
                                id => "chapter$1",
-                               index => $1,
                                title => $s->[2],
                                description => $s->[2],
-                               src => sprintf('s/%s/p%08d.jpg', $res, $1),
+                               src => sprintf('%s/s/%dx%d/p%03d.jpg', $media_dir, $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
                                href => '',
                        },
-               }
+               };
+
+               push @slide_t, $s->[0];
        }
 
-       warn "# sync ", dump $sync;
+       my $res = $prop->{width} . 'x' . $prop->{height};
 
-       warn "# prop ", dump $prop;
+       foreach ( 0 .. $#slide_t ) {
+               push @{ $sync->{htmlEvents}->{'#slide'} }, {
+                       startTime => $slide_t[$_],
+                       endTime   => $slide_t[$_ + 1] || $prop->{length},
+                       html      => sprintf( '<img src=%s/s/%s/p%03d.jpg>', $media_dir, $res, $_ + 1 ),
+               };
+       }
 
-       my $html5tv = $prop;
-       $html5tv->{sync} = $sync;
+       my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
+       @slides_hires    = glob 'shot*.png' unless @slides_hires;
+       my $factor_s_path;
 
-       my $sync_path = 'www/video.js';
-       write_file $sync_path, "var html5tv = " . to_json($html5tv) . " ;\n";
-       warn "sync $sync_path ", -s $sync_path, " bytes\n";
+       foreach my $factor ( 4, 2, 1 ) {
+               my $w = $prop->{width}  / $factor;
+               my $h = $prop->{height} / $factor;
+
+               my $path = "$media_dir/s/${w}x${h}";
+               $factor_s_path->{$factor} = $path;
 
-       if ( $prop->{width} && $prop->{height} ) {
-               foreach my $factor ( 1, 2, 4 ) {
-                       my $w = $prop->{width}  / $factor;
-                       my $h = $prop->{height} / $factor;
-                       my $path = "www/s/${w}x${h}";
-                       if ( ! -d $path ) {
-                               mkdir $path;
-                               warn "created $path\n";
+               if ( ! -d $path ) {
+                       mkdir $path;
+                       warn "created $path\n";
+
+                       foreach my $hires ( @slides_hires ) {
+
+                               my $file = $hires;
+                               $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
+
+                               my $im = Imager->new( file => $hires );
+                               $im->scale( xpixels => $w, ypixels => $h, type => 'min' )->write( file => $file );
+                               warn "resized $file ", -s $file, " bytes\n";
                        }
                }
+
+       }
+
+       my ( $slide_width, $slide_height );
+
+       my $im = Imager->new( file => $factor_s_path->{ 1 } . '/p001.jpg"')
+                       || Imager->new( file => "shot0001.png" ) # from mplayer [s]
+                       ;
+
+       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 $html5tv = {
+               sync => $sync,
+               video => $prop,
+               slide => {
+                       width => $slide_width,
+                       height => $slide_height,
+               },
+       };
+
+       $html5tv->{video_tags} =
+               join("\n",
+                       map {
+                               my $s = $_;
+                               my $id = $s->[3];
+                               $id =~ s{\W+}{_}g;
+
+                               push @{ $html5tv->{sync}->{customEvents} }, {
+                                       startTime => $s->[0],
+                                       endTime   => $s->[1],
+                                       action    => 'additional_video',
+                                       args => {
+                                               id => $id,
+                                               title => $s->[2],
+                                               description => $s->[2],
+                                               src => "$media_dir/s/$s->[3]/00000001.jpg",
+                                               href => '',
+                                       },
+                               };
+
+                               qq|
+                                       <video id="$id" style="display: none" controls="controls" width="$html5tv->{video}->{width}px" height="$html5tv->{video}->{height}px">
+                                       <source src="$media_dir/$_->[3]" />
+                                       </video>
+                               |
+                       } @videos
+               )
+       ;
+
+       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;
+               }
+
+               sort { $a->{startTime} <=> $b->{startTime} }
+               @{ $html5tv->{sync}->{customEvents} }
        }
 
+       my $index = 1;
+
+       $_->{args}->{index} = $index++ foreach customEvents_sorted;
+
+       warn "last customEvent $index\n";
+
+       $html5tv->{subtitles_table}
+               = qq|<table id="subtitles">|
+               . join("\n",
+                       map { qq|
+                               <tr id="sub_$_->{index}">
+                                       <td class="seek_video">$_->{startTime}</td>
+                                       <td class="seek_video">$_->{endTime}</td>
+                                       <td>$_->{args}->{title}</td>
+                               </tr>
+                       | }
+                       customEvents_sorted
+               )
+               . qq|</table><a href="$media_dir/video.srt">download subtitles</a>|
+               ;
+
+       my $hcalendar = '<div style="color: red">Create <tt>hcalendar.html</tt> to fill this space</div>';
+       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;
+               }
+       }
+
+       warn "# html5tv ", dump $html5tv;
+
+       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";
+
+       my $html = read_file 'www/tv.html';
+       $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
+               warn "no interpolation in template!";
+
+       write_file "www/editing.html", $html;
+       $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;
+
+       write_file "$media_dir/video.css", qq|
+
+.jcarousel-skin-ie7 .jcarousel-container-horizontal,
+.jcarousel-skin-ie7 .jcarousel-clip-horizontal {
+       width: ${carousel_width}px;
+       height: ${carousel_height}px;
+}
+
+.jcarousel-skin-ie7 .jcarousel-item {
+       width: ${slide_width}px;
+       height: ${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;
+       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;
+}
+
+
+
+.jcarousel-skin-ie7 .jcarousel-item img:hover {
+//     border-color: #555 !important;
+}
+
+.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;
+}
+
+       |;
+
+       return 1;
 }
 
 
@@ -157,6 +427,9 @@ sub t_srt {
 }
 
 sub save_subtitles {
+
+       html5tv || return;
+
        my $nr = 0;
        my $srt = "\n";
        foreach my $s ( @subtitles ) {
@@ -168,23 +441,23 @@ sub save_subtitles {
        warn $srt;
 
        write_file $subtitles, $srt;
-       YAML::DumpFile "$subtitles.yaml", @subtitles;
+       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";
-
-       html5tv;
 }
 
 sub load_subtitles {
-       @subtitles = sort { $a->[0] <=> $b->[0] } YAML::LoadFile "$subtitles.yaml";
+       if ( ! -e "$subtitles.yaml" ) {
+               warn "no subtitles $subtitles to load\n";
+               return;
+       }
+       @subtitles = YAML::LoadFile "$subtitles.yaml";
        warn "subtitles ", dump @subtitles;
        save_subtitles;
 }
 
-load_subtitles if -e "$subtitles.yaml";
-
 sub edit_subtitles {
        print $to_mplayer qq|pause\n|;
        focus_term;
@@ -225,7 +498,7 @@ sub sub_fmt {
 
 sub prev_subtitle {
        my $pos = time_pos;
-       my $s = ( grep { $_->[0] < $pos } @subtitles )[0];
+       my $s = ( grep { $_->[0] < $pos } @subtitles )[0] || return;
        warn "<<<< subtitle ", sub_fmt $s;
        preroll $s->[0], $s->[2];
 #      print $to_mplayer "set_property time_pos $s->[0]\n";
@@ -233,7 +506,7 @@ sub prev_subtitle {
 
 sub next_subtitle {
        my $pos = time_pos + $preroll;
-       my $s = ( grep { $_->[0] > $pos } @subtitles )[0];
+       my $s = ( grep { $_->[0] > $pos } @subtitles )[0] || return;
        warn ">>>> subtitle ", sub_fmt $s;
        preroll $s->[0], $s->[2];
 #      print $to_mplayer "set_property time_pos $s->[0]\n";
@@ -299,14 +572,15 @@ while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1
                                        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 'F2' ? move_subtitle( -0.3 )
-                                       : $1 eq 'F3' ? move_subtitle( +0.3 )
+                                       : $1 eq 'F5' ? save_subtitles
                                        : $1 eq 'F9' ? add_subtitle
                                        : $1 eq 'F12' ? edit_subtitles
                                        : warn "CUSTOM $1\n"