#!/usr/bin/env perl
#
# name: lastplay, remember last play position of mplayer
# author: Xu Xiaodong
# license: GPL
# created: 2010 Mar 20
# modified: 2011 Apr 13
#
use strict;
use warnings;
use Date::Calc qw/Time_to_Date/;
my $movie = shift;
if ( defined $movie ) {
call( $movie, $$, 'mtime' );
}
else {
usage();
}
sub _read {
my ($file) = @_;
open my ($in_fh), '<', $file or die "Can't read $file: $!\n";
return readline $in_fh;
close $in_fh;
}
sub _write {
my ( $infile, $outfile ) = @_;
local $/ = q{[K
};
my @secs;
open my ($in_fh), '<', $infile or die "Can't read $infile: $!\n";
while (<$in_fh>) { push @secs, $1 if /A:\s*(\d+)/ }
close $in_fh;
unlink $infile;
my $sec = pop @secs;
my $time = join ":", ( Time_to_Date($sec) )[ 3 .. 5 ];
open my ($out_fh), '>', $outfile or die "Can't write $outfile: $!\n";
print $out_fh $time;
close $out_fh;
}
sub call {
my ( $movie, $mplayer_out, $time_out ) = @_;
if ( -e $time_out and -s $time_out ) {
my $time = _read($time_out);
system "mplayer -ss $time \Q$movie\E > $mplayer_out";
}
else {
system "mplayer \Q$movie\E > $mplayer_out";
}
if ( -e $mplayer_out and -s $mplayer_out ) {
_write( $mplayer_out, $time_out );
}
}
sub usage { print "Usage: $0 \n" }
# vim: ai:et:ts=4:sw=4:tw=78:ft=perl