move screenshots to s/shot/time path
[HTML5TV.git] / bin / mplayer.pl
index a92bda0..3fceaff 100755 (executable)
@@ -13,9 +13,34 @@ use HTML::TreeBuilder;
 use Imager;
 
 
-my $movie = shift @ARGV
-       || 'www/media/video.ogv';
-#      || die "usage: $0 path/to/movie.ogv\n";
+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';
+
+warn "# media_part $media_part\n";
 
 my $edl = "/dev/shm/edl";
 my $subtitles = $movie;
@@ -36,6 +61,7 @@ my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer,
                '-quiet',
                '-edlout', $edl,
                '-osdlevel', 3,
+               '-vf' => 'screenshot',
 );
 
 my $epfd = epoll_create(10);
@@ -44,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 length/ );
+       load_subtitles;
 }
 
 
@@ -94,6 +123,10 @@ sub repl {
 
 our @subtitles;
 
+sub slide_jpg {
+       sprintf "%s/s/%d/%03d.jpg", $media_dir, @_;
+}
+
 sub html5tv {
 
        if ( ! $prop->{width} || ! $prop->{height} ) {
@@ -101,6 +134,11 @@ sub html5tv {
                return;
        }
 
+       if ( ! @subtitles ) {
+               warn "SKIP no subtitles yet\n";
+               return;
+       }
+
 
        my $sync;
 
@@ -117,12 +155,12 @@ sub html5tv {
 
                if ( $s->[2] =~ m{video:(.+)} ) {
                        my $video = $1;
-                       my $path = "www/media/$video";
+                       my $path = "$media_dir/$video";
                        if ( ! -e $path ) {
                                warn "MISSING $path: $!\n";
                        } else {
-                               my $frame_dir = "www/media/s/$video";
-                               system "mplayer -vo jpeg:outdir=$frame_dir -frames 1 -ss 0 www/media/$video"
+                               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 ];
                        }
@@ -140,7 +178,7 @@ sub html5tv {
                                id => "chapter$1",
                                title => $s->[2],
                                description => $s->[2],
-                               src => sprintf('media/s/%dx%d/p%03d.jpg', $prop->{width} / $slide_factor, $prop->{height} / $slide_factor, $1),
+                               src => slide_jpg( 4 => $1 ),
                                href => '',
                        },
                };
@@ -148,47 +186,66 @@ sub html5tv {
                push @slide_t, $s->[0];
        }
 
-       my $res = $prop->{width} . 'x' . $prop->{height};
-
        foreach ( 0 .. $#slide_t ) {
+               my $nr = $_ + 1;
                push @{ $sync->{htmlEvents}->{'#slide'} }, {
                        startTime => $slide_t[$_],
                        endTime   => $slide_t[$_ + 1] || $prop->{length},
-                       html      => sprintf( '<img src=media/s/%s/p%03d.jpg>', $res, $_ + 1 ),
+                       html      => '<img src=' . slide_jpg( 1 => $_ + 1 ) . '>',
                };
        }
 
-       my ( $slide_width, $slide_height );
+       my @slides_hires = glob '$media_dir/s/hires/p*.jpg';
+       @slides_hires    = glob 'shot*.png' unless @slides_hires;
 
        foreach my $factor ( 4, 2, 1 ) {
                my $w = $prop->{width}  / $factor;
                my $h = $prop->{height} / $factor;
 
-               my $path = "www/media/s/${w}x${h}";
+               my $path = "$media_dir/s";
+               if ( ! -d $path ) {
+                       warn "create slides imaes in $path";
+                       mkdir $path;
+               }
+
+               $path .= '/' . $factor;
 
                if ( ! -d $path ) {
                        mkdir $path;
                        warn "created $path\n";
 
-                       foreach my $hires ( glob 'www/media/s/hires/p*.jpg' ) {
+               }
 
-                               my $file = $hires;
-                               $file =~ s{^.+/(p\d+\.\w)}{$path/$1};
+               foreach my $hires ( @slides_hires ) {
 
-                               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 $nr = $1 if $hires =~ m{^.+(\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 ( $factor == $slide_factor ) {
-                       my $im = Imager->new( file => "$path/p001.jpg" );
-                       $slide_width  = $im->getwidth;
-                       $slide_height = $im->getheight;
+                       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 => slide_jpg( 1 => 1 ) )
+                       || 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,
@@ -198,7 +255,7 @@ sub html5tv {
                },
        };
 
-       $html5tv->{video_tags} =
+       $html5tv->{html}->{video_tags} =
                join("\n",
                        map {
                                my $s = $_;
@@ -213,49 +270,57 @@ sub html5tv {
                                                id => $id,
                                                title => $s->[2],
                                                description => $s->[2],
-                                               src => "media/s/$s->[3]/00000001.jpg",
+                                               src => "$media_dir/s/1/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/$_->[3]" />
+                                       <source src="$media_dir/$_->[3]" />
                                        </video>
                                |
                        } @videos
                )
        ;
 
-       my $index = 1;
+       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;
+               }
 
-       foreach my $e (
                sort { $a->{startTime} <=> $b->{startTime} }
                @{ $html5tv->{sync}->{customEvents} }
-       ) {
-               $e->{args}->{index} = $index++;
-               warn "e = ", dump $e;
        }
 
-       warn "last customEvent $index\n";
+       my $index = 1;
 
-       my $nr = 0;
+       $_->{args}->{index} = $index++ foreach customEvents_sorted;
+
+       warn "last customEvent $index\n";
 
-       $html5tv->{subtitles_table}
+       $html5tv->{html}->{subtitles_table}
                = qq|<table id="subtitles">|
                . join("\n",
-                       map {
-                               $nr++;
-                               qq|<tr id="sub_$nr"><td align="right">$_->[0]</td><td align="right">$_->[1]</td><td>$_->[2]</td></tr>|
-                       } @subtitles
+                       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>|
+               . 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 = 'www/media/hcalendar.html';
+       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;
+               $html5tv->{hCalendar} = read_file $hcal_path;
                my $tree = HTML::TreeBuilder->new;
                $tree->parse_file($hcal_path);
                if ( my $vevent = $tree->look_down( class => 'vevent' ) ) {
@@ -263,9 +328,9 @@ sub html5tv {
                }
        }
 
-       warn "html5tv ", dump $html5tv;
+       warn "html5tv ", dump $html5tv;
 
-       my $sync_path = 'www/media/video.js';
+       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";
 
@@ -273,13 +338,15 @@ sub html5tv {
        $html =~ s|{([^}]+)}|my $n = $1; $n =~ s(\.)(}->{)g; eval "\$html5tv->{$n}"|egs ||
                warn "no interpolation in template!";
 
-       write_file 'www/media.html', $html;
+       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} + Imager->new( file => "www/media/s/$res/p001.jpg" )->getwidth - 80;
+       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 'www/media/video.css', qq|
+       write_file "$media_dir/video.css", qq|
 
 .jcarousel-skin-ie7 .jcarousel-container-horizontal,
 .jcarousel-skin-ie7 .jcarousel-clip-horizontal {
@@ -348,6 +415,10 @@ div#subtitle {
        width: ${slide_width}px;
 }
 
+.seek_video {
+       text-align: right;
+       font-family: monospace;
+}
 
        |;
 
@@ -389,13 +460,15 @@ sub save_subtitles {
 }
 
 sub load_subtitles {
+       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;
@@ -436,7 +509,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";
@@ -444,7 +517,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";
@@ -535,6 +608,23 @@ while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1
                                                        $subtitles[ $#subtitles ]->[1] = $pos;
                                                }
                                        }
+                               } elsif ( $line =~ m{(shot\d+.png)} ) {
+                                       my $shot = $1;
+                                       my $t = time_pos;
+                                       warn "shot $t $shot\n";
+                                       my $dir = "$media_dir/s/shot";
+                                       if ( ! -e $dir ) {
+                                               mkdir $dir;
+                                               warn "created $dir\n";
+                                       }
+                                       rename $1, "$dir/$t.png";
+
+                                       my $hires = "media_dir/s/hires";
+                                       mkdir $hires unless -e $hires;
+                                       my $max_slide = scalar( glob("$hires/*") ) || 0;
+                                       symlink "$dir/$t.png", "$hires/s$max_slide.png";
+                                       push @subtitles, [ $t, $t, "[$max_slide] video time $t slide $max_slide" ];
+                                       warn "created slide $max_slide at $t from $shot\n";
                                }
 
                                $line = '';