X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=bin%2Fmplayer.pl;h=806820df5baaafc34077f9fd9de616da5e12a631;hb=62e6f8bd5eecb9d08338df1a5b18b2862c765646;hp=7e98ff2b22b5f2a71d1f1f87ff8c73092e8016b4;hpb=fe330a33e54266aa3aecce0a44ef2b00d0494451;p=HTML5TV.git diff --git a/bin/mplayer.pl b/bin/mplayer.pl index 7e98ff2..806820d 100755 --- a/bin/mplayer.pl +++ b/bin/mplayer.pl @@ -3,21 +3,24 @@ use warnings; use strict; +use autodie; + use IPC::Open3 qw(open3); use IO::Select; use Data::Dump qw(dump); use File::Slurp; use YAML; use JSON; -use HTML::TreeBuilder; 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; @@ -29,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"; } @@ -68,7 +66,7 @@ our $from_mplayer; our $err_mplayer; our $prop = {}; -my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer, +my @mplayer_command = ( 'mplayer', '-slave', '-idle', # '-quiet', @@ -78,6 +76,10 @@ my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer, '-vf' => 'screenshot', ); +push @mplayer_command, qw/ -vo null -ao null / if $generate; + +my $pid = open3( $to_mplayer, $from_mplayer, $err_mplayer, @mplayer_command ); + my $select = IO::Select->new(); #$select->add( \*STDIN ); $select->add( $from_mplayer ); @@ -154,10 +156,19 @@ sub fmt_mmss { return sprintf('%02d:%02d', int($t/60), int($t%60)); } +my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); +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; } @@ -194,7 +205,7 @@ sub html5tv { push @videos, [ @$s, $video ]; } } elsif ( $s->[2] =~ m{slide:(\d+)\s+shot:(\d+\.\d+)} ) { - push @frames, [ $2, $1 ]; + push @frames, [ $2, $1 ] unless -e "$media_dir/s/hires/f$1.jpg"; next; } @@ -218,10 +229,39 @@ sub html5tv { push @slide_t, $s->[0]; } + foreach ( 0 .. $#slide_t ) { + my $slide_nr = $_ + 1; + push @{ $sync->{htmlEvents}->{'#slide'} }, { + startTime => $slide_t[$_], + endTime => $slide_t[$_ + 1] || $prop->{length}, + html => '', + }; + } + + + 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; + + if ( -e $path ) { + warn "render pdf slides from $path\n"; + system "pdftoppm -png -r 100 $path $hires/p"; + } + } + + if ( @frames ) { + warn "create slides from video 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; @@ -233,23 +273,9 @@ sub html5tv { } - foreach ( 0 .. $#slide_t ) { - my $slide_nr = $_ + 1; - push @{ $sync->{htmlEvents}->{'#slide'} }, { - startTime => $slide_t[$_], - endTime => $slide_t[$_ + 1] || $prop->{length}, - html => '', - }; - } - - my @slides_hires = glob "$media_dir/s/hires/*"; - my $path = "$media_dir/s"; + my @slides_hires = glob "$hires/*"; - if ( ! -d $path ) { - warn "create slides images in $path"; - mkdir $path; - } foreach my $hires ( @slides_hires ) { @@ -268,10 +294,8 @@ sub html5tv { my $file = slide_jpg( $factor => $nr ); next if -e $file; - $min_slide_height = $prop->{height} if $prop->{height} > $min_slide_height; - - my $w = $min_slide_height / $factor * $slide_aspect; - my $h = $min_slide_height / $factor; + 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"; @@ -290,6 +314,9 @@ sub html5tv { 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 ); @@ -298,12 +325,28 @@ sub html5tv { $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, $size, $format) = Graphics::Magick->new->Ping( slide_jpg( $slide_factor => 1 ) ); + + 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, @@ -312,6 +355,10 @@ sub html5tv { width => $slide_width, height => $slide_height, }, + carousel => { + width => $c_slide_width, + height => $c_slide_height, + }, }; $html5tv->{html}->{video_tags} = @@ -329,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 => '', }, }; @@ -363,40 +410,52 @@ sub html5tv { warn "last customEvent $index\n"; $html5tv->{html}->{subtitles_table} - = qq|| + = qq| +
+ + | . join("\n", map { - my $s = fmt_mmss( $_->{startTime} ); - my $e = fmt_mmss( $_->{endTime} ); + 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| - - - - + + + + + | } @customEvents_sorted ) - . qq|
#slide
$s$e$_->{args}->{title}
$s_f$e_f$slide$t
download subtitles| + . qq|| ; my $hCalendar = '
Create hCalendar.html to fill this space
'; 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 if $debug; + 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|$type |; + } - 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 || @@ -406,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| @@ -419,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; } |; @@ -527,6 +556,8 @@ sub save_subtitles { ; } +sub annotate_subtitles; + sub load_subtitles { if ( ! -e "$subtitles.yaml" ) { warn "no subtitles $subtitles to load\n"; @@ -534,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 = ; $subtitles[ $#subtitles ]->[2] = $line if defined $line; - save_subtitles; - focus_mplayer; preroll $subtitles[ $#subtitles ]->[0], $line; @@ -624,7 +715,7 @@ load_movie; my $slides = HTML5TV::Slides->new( sub { - my $t = shift; + my $t = shift || return; my $nr = 0; foreach my $s ( @subtitles ) { $nr = $1 if $s->[2] =~ m{\[(\d+)\]} && $s->[0] < $t; @@ -682,23 +773,36 @@ sub from_mplayer { warn "slide $nr from video $pos file $shot\n"; save_subtitles; + } elsif ( $line =~ m{File not found} ) { + die $line; } else { - warn "IGNORE $line"; + warn "IGNORE $line\n"; # if $debug; } } -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; +push @to_mplayer, 'pause' if $generate; + while ( 1 ) { my $dt = time - $t; if ( abs($dt) > 0.2 ) { -warn "dt $dt\n"; - $slides->show( $pos ); +#warn "dt $dt\n"; + $slides->show( $pos, $prop->{length}, @subtitles ) if $prop->{length}; $t = time; } @@ -722,5 +826,9 @@ warn "dt $dt\n"; print $to_mplayer $cmd; } + if ( $generate && html5tv() ) { + warn "generated\n"; + exit; + } }