[MPlayer-users] How to find out "timestamps" of a mpeg stream?
Josef Wolf
jw at raven.inka.de
Mon Feb 10 21:45:17 CET 2003
Hallo!
I need some way to find out how long (in seconds) a mpeg stream
lasts. My first try (in perl) was:
my $f = "stream.mpg";
my $start=undef;
my $end=undef;
open(L,"mplayer -nosound -vo null -ao null -speed 100 $f|") or die;
while (my $l=<L>) {
for my $l1 (split (/\015/, $l)) {
if ($l1=~/^V:\s*([\d,]+)/) {
$end = $1;
$end =~ s/\,/./;
$start = $end if $end ne "0.0" && !defined $start;
}
}
}
close L;
print "Length: ", $end-$start, "\n";
This code does the job, but it is somewhat slooow because it needs
to decode the whole stream. So I came up with the following:
my $f = "stream.mpg";
my $pos = (-s $f) - 1024*1024;
$pos=0 if $pos<0;
my $start = &frametime ("-frames 1 $f");
my $end = &frametime ("-sb $pos $f");
print "Length: ", $end-$start, "\n";
sub frametime {
my ($par) = @_;
my $sec=undef;
open(L,"mplayer -nosound -vo null -ao null -speed 100 $par|") or die;
while (my $l=<L>) {
for my $l1 (split (/\015/, $l)) {
if ($l1=~/^V:\s*([\d,]+)/) {
$sec = $1;
$sec =~ s/\,/./;
}
}
}
close L;
return $sec;
}
This too does the job. In addition, it is much faster. But starting
at 1MB before the end of file seems to be strange. What would be a
good choice to guarantee that at least one complete frame would be
"played"?
Anyone an idea?
--
-- Josef Wolf -- jw at raven.inka.de --
More information about the MPlayer-users
mailing list