3 # Copyright (c) 2007-2013 Stefano Sabatini
5 # This file is part of FFmpeg.
7 # FFmpeg is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # FFmpeg is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 # See the GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 plotframes - Plot video frame sizes using ffprobe and gnuplot
27 plotframes [I<options>] [I<input>]
31 plotframes reads a multimedia files with ffprobe, and plots the
32 collected video sizes with gnuplot.
38 =item B<--input|-i> I<infile>
40 Specify multimedia file to read. This is the file passed to the
41 ffprobe command. If not specified it is the first argument passed to
44 =item B<--help|--usage|-h|-?>
46 Print a brief help message and exit.
52 =item B<--output|-o> I<outfile>
54 Set the name of the output used by gnuplot. If not specified no output
55 is created. Must be used in conjunction with the B<terminal> option.
57 =item B<--stream|--s> I<stream_specifier>
59 Specify stream. The value must be a string containing a stream
60 specifier. Default value is "v".
62 =item B<--terminal|-t> I<terminal>
64 Set the name of the terminal used by gnuplot. By default it is
65 "x11". Must be used in conjunction with the B<output> option. Check
66 the gnuplot manual for the valid values.
74 ffprobe(1), gnuplot(1)
82 use JSON -support_by_pp;
87 my $stream_specifier = "v";
88 my $gnuplot_terminal = "x11";
92 'input|i=s' => \$input,
93 'help|usage|?|h' => sub { pod2usage ( { -verbose => 1, -exitval => 0 }) },
94 'manpage|m' => sub { pod2usage ( { -verbose => 2, -exitval => 0 }) },
95 'stream|s=s' => \$stream_specifier,
96 'terminal|t=s' => \$gnuplot_terminal,
97 'output|o=s' => \$gnuplot_output,
98 ) or pod2usage( { -message=> "Parsing error", -verbose => 1, -exitval => 1 });
100 die "You must specify an input file\n" unless $input;
103 my @cmd = (qw{ffprobe -show_entries frame -select_streams}, $stream_specifier, "-of", "json", $input);
104 print STDERR "Executing command: @cmd\n";
107 open(FH, "-|", @cmd) or die "ffprobe command failed: $!\n";
109 my $json_text = <FH>;
111 die "ffprobe command failed" if $?;
112 eval { $json_struct = decode_json($json_text); };
113 die "JSON parsing error: $@\n" if $@;
116 # collect and print frame statistics per pict_type
118 my $frames = $json_struct->{frames};
120 foreach my $frame (@{$frames}) {
121 my $type = $frame->{pict_type};
122 $frame->{count} = $frame_count++;
123 if (not $stats{$type}) {
124 $stats{$type}->{tmpfile} = File::Temp->new(SUFFIX => '.dat');
125 my $fn = $stats{$type}->{tmpfile}->filename;
126 open($stats{$type}->{fh}, ">", $fn) or die "Can't open $fn";
129 print { $stats{$type}->{fh} }
130 "$frame->{count} ", $frame->{pkt_size} * 8 / 1000, "\n";
132 foreach (keys %stats) { close $stats{$_}->{fh}; }
134 # write gnuplot script
135 my %type_color_map = (
141 my $gnuplot_script_tmpfile = File::Temp->new(SUFFIX => '.gnuplot');
142 my $fn = $gnuplot_script_tmpfile->filename;
143 open(FH, ">", $fn) or die "Couldn't open $fn: $!";
145 set title "video frame sizes"
146 set xlabel "frame time"
147 set ylabel "frame size (Kbits)"
149 set terminal "$gnuplot_terminal"
152 print FH "set output \"$gnuplot_output\"\n" if $gnuplot_output;
155 foreach my $type (keys %stats) {
156 my $fn = $stats{$type}->{tmpfile}->filename;
157 print FH "$sep\"$fn\" title \"$type frames\" with impulses";
158 print FH " linecolor rgb \"$type_color_map{$type}\"" if $type_color_map{$type};
163 # launch gnuplot with the generated script
164 system ("gnuplot", "--persist", $gnuplot_script_tmpfile->filename);