[FFmpeg-devel] [RFC] Add plotframes script

Stefano Sabatini stefasab at gmail.com
Fri Jan 11 00:23:19 CET 2013


On date Thursday 2013-01-10 12:41:08 +0100, Nicolas George encoded:
> Le primidi 21 nivôse, an CCXXI, Stefano Sabatini a écrit :
> > The script is ported from ffprobe/SourceForge and updated to the current
> > ffprobe version.
> > ---
> >  tools/plotframes |  134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 134 insertions(+)
> >  create mode 100755 tools/plotframes
> > 
> > diff --git a/tools/plotframes b/tools/plotframes
> > new file mode 100755
> > index 0000000..e24fa75
> > --- /dev/null
> > +++ b/tools/plotframes
> > @@ -0,0 +1,134 @@
> > +#!/usr/bin/env perl
> > +
> > +use warnings;
> > +use strict;
> > +
> > +use File::Temp;
> > +use JSON -support_by_pp;
> > +use Getopt::Long;
> > +use Pod::Usage;
> > +
> > +my $input = $ARGV[0];
> > +my $stream_specifier = "v";
> > +my $gnuplot_terminal = "x11";
> > +my $gnuplot_output;
> > +
> > +GetOptions (
> > +    'input|i=s'      => \$input,
> > +    'help|usage|?|h' => sub { pod2usage ( { -verbose => 1, -exitval => 0 }) },
> > +    'manpage|m'      => sub { pod2usage ( { -verbose => 2, -exitval => 0 }) },
> > +    'stream|s=s'     => \$stream_specifier,
> > +    'terminal|t=s'   => \$gnuplot_terminal,
> > +    'output|o=s'     => \$gnuplot_output,
> > +    ) or pod2usage( { -message=> "Parsing error", -verbose => 1, -exitval => 1 });
> > +
> 
> > +die "You must define an input file\n" unless $input;
> 
> "define"?

Fixed.
 
> > +
> > +$gnuplot_output = "plotframes.$$.$gnuplot_terminal" if not $gnuplot_output;
> 
> Urgh.

Removed, the user is now specified to specify the output (or an empty
x11 file will be created).
 
> > +
> > +# fetch data
> 
> > +my $cmd = "ffprobe -show_entries frame -select_streams $stream_specifier -of json \"$input\"";
> 
> Use a list:
> 
> my @cmd = (qw{ffprobe -show_entries frame -select_streams",
>   $stream_specifier, "-of", "json", $input);
> 
> That will avoid quoting problems with $input.

Fixed.

> > +print STDERR "Executing command: $cmd\n";
> > +my $json_struct;
> > +{
> > +    open(FH, "$cmd|") or die "ffprobe command '$cmd' failed: $!\n";
> 
> open my $fh, "-|", @cmd or die ...
> 
> > +    local $/;
> 
> > +    my $json_text = <FH>;
> 
> A format that allows progressive parsing would be nice to give progress
> information.
> 
> (I can work on -of perl if people think it useful.)

I thought about that and would be easy to implement, but then we would
have people asking for all type of languages.
 
> > +    eval { $json_struct = decode_json($json_text); };
> > +    die "JSON parsing error: $@\n" if $@;
> > +}
> > +
> > +# collect and print frame statistics per pict_type
> > +my %stats;
> > +my $frames = $json_struct->{frames};
> > +foreach my $frame (@{$frames}) {
> > +    my $type = $frame->{pict_type};
> > +    if (not $stats{$type}) {
> > +        $stats{$type}->{tmpfile} = File::Temp->new(SUFFIX => '.dat');
> > +        my $fn = $stats{$type}->{tmpfile}->filename;
> 
> > +        open ($stats{$type}->{fh}, ">$fn") or die "Can't open $fn";
> 
> open ..., ">", $fn

Changed here and everywhere.
 
> > +    }
> > +
> > +    print { $stats{$type}->{fh} }
> > +        "$frame->{pkt_pts_time} ", $frame->{pkt_size} * 8 /1000, "\n";
> > +}
> > +foreach (keys %stats) { close $stats{$_}->{fh}; }
> > +
> > +# write gnuplot script
> 
> > +my $gnuplot_script_tmpfile = File::Temp->new(SUFFIX => '.gnuplot');
> 
> Could be piped to gnuplot.

But I somehow feel it may complicate code/understanding for little gain.
 
> > +{
> > +    my $fn = $gnuplot_script_tmpfile->filename;
> > +    open (FH, ">$fn") or die "Couldn't open $fn: $!";
> 
> open my $fh, ">", $fn;
> 
> > +    print FH << "EOF";
> 
> > +set title "video frame sizes - $input"
> 
> Problem if $input has special characters.

Right, removed since I don't know what's the proper way to escape
strings in the gnuplot language.

[...]
 
> Regards,

Added copyright notice and moved documentation up, consistent with
make_chlayout_test. I still have to think of a way to fix the color
for each type of frames (say red for I-frames, blue for P-frames
etc.), also the script doesn't work fine in case pkt_pts_time is not
always defined.
-- 
FFmpeg = Freak Fierce Merciless Proud Emblematic Gangster
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-tools-add-plotframes-script.patch
Type: text/x-diff
Size: 4951 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130111/ed4f25d3/attachment.bin>


More information about the ffmpeg-devel mailing list