Browse code

tools: add plotframes script

The script is ported from ffprobe/SourceForge and updated to the current
ffprobe version.

Stefano Sabatini authored on 2013/01/09 07:27:37
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,164 @@
0
+#!/usr/bin/env perl
1
+
2
+# Copyright (c) 2007-2013 Stefano Sabatini
3
+#
4
+# This file is part of FFmpeg.
5
+#
6
+# FFmpeg is free software; you can redistribute it and/or
7
+# modify it under the terms of the GNU Lesser General Public
8
+# License as published by the Free Software Foundation; either
9
+# version 2.1 of the License, or (at your option) any later version.
10
+#
11
+# FFmpeg is distributed in the hope that it will be useful,
12
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
+# See the GNU Lesser General Public License for more details.
15
+#
16
+# You should have received a copy of the GNU Lesser General Public License
17
+# along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+
20
+=head1 NAME
21
+
22
+plotframes - Plot video frame sizes using ffprobe and gnuplot
23
+
24
+=head1 SYNOPSIS
25
+
26
+plotframes [I<options>] [I<input>]
27
+
28
+=head1 DESCRIPTION
29
+
30
+plotframes reads a multimedia files with ffprobe, and plots the
31
+collected video sizes with gnuplot.
32
+
33
+=head1 OPTIONS
34
+
35
+=over 4
36
+
37
+=item B<--input|-i> I<infile>
38
+
39
+Specify multimedia file to read. This is the file passed to the
40
+ffprobe command. If not specified it is the first argument passed to
41
+the script.
42
+
43
+=item B<--help|--usage|-h|-?>
44
+
45
+Print a brief help message and exit.
46
+
47
+=item B<--manpage|-m>
48
+
49
+Print the man page.
50
+
51
+=item B<--output|-o> I<outfile>
52
+
53
+Set the name of the output used by gnuplot. If not specified no output
54
+is created. Must be used in conjunction with the B<terminal> option.
55
+
56
+=item B<--stream|--s> I<stream_specifier>
57
+
58
+Specify stream. The value must be a string containing a stream
59
+specifier. Default value is "v".
60
+
61
+=item B<--terminal|-t> I<terminal>
62
+
63
+Set the name of the terminal used by gnuplot. By default it is
64
+"x11". Must be used in conjunction with the B<output> option. Check
65
+the gnuplot manual for the valid values.
66
+
67
+=back
68
+
69
+=cut
70
+
71
+=head1 SEE ALSO
72
+
73
+ffprobe(1), gnuplot(1)
74
+
75
+=cut
76
+
77
+use warnings;
78
+use strict;
79
+
80
+use File::Temp;
81
+use JSON -support_by_pp;
82
+use Getopt::Long;
83
+use Pod::Usage;
84
+
85
+my $input = $ARGV[0];
86
+my $stream_specifier = "v";
87
+my $gnuplot_terminal = "x11";
88
+my $gnuplot_output;
89
+
90
+GetOptions (
91
+    'input|i=s'      => \$input,
92
+    'help|usage|?|h' => sub { pod2usage ( { -verbose => 1, -exitval => 0 }) },
93
+    'manpage|m'      => sub { pod2usage ( { -verbose => 2, -exitval => 0 }) },
94
+    'stream|s=s'     => \$stream_specifier,
95
+    'terminal|t=s'   => \$gnuplot_terminal,
96
+    'output|o=s'     => \$gnuplot_output,
97
+    ) or pod2usage( { -message=> "Parsing error", -verbose => 1, -exitval => 1 });
98
+
99
+die "You must specify an input file\n" unless $input;
100
+
101
+# fetch data
102
+my @cmd = (qw{ffprobe -show_entries frame -select_streams}, $stream_specifier, "-of", "json", $input);
103
+print STDERR "Executing command: @cmd\n";
104
+my $json_struct;
105
+{
106
+    open(FH, "-|", @cmd) or die "ffprobe command failed: $!\n";
107
+    local $/;
108
+    my $json_text = <FH>;
109
+    close FH;
110
+    die "ffprobe command failed" if $?;
111
+    eval { $json_struct = decode_json($json_text); };
112
+    die "JSON parsing error: $@\n" if $@;
113
+}
114
+
115
+# collect and print frame statistics per pict_type
116
+my %stats;
117
+my $frames = $json_struct->{frames};
118
+my $frame_count = 0;
119
+foreach my $frame (@{$frames}) {
120
+    my $type = $frame->{pict_type};
121
+    $frame->{count} = $frame_count++;
122
+    if (not $stats{$type}) {
123
+        $stats{$type}->{tmpfile} = File::Temp->new(SUFFIX => '.dat');
124
+        my $fn = $stats{$type}->{tmpfile}->filename;
125
+        open($stats{$type}->{fh}, ">", $fn) or die "Can't open $fn";
126
+    }
127
+
128
+    print { $stats{$type}->{fh} }
129
+        "$frame->{count} ", $frame->{pkt_size} * 8 / 1000, "\n";
130
+}
131
+foreach (keys %stats) { close $stats{$_}->{fh}; }
132
+
133
+# write gnuplot script
134
+my %type_color_map = (
135
+    "I" => "red",
136
+    "P" => "green",
137
+    "B" => "blue"
138
+    );
139
+
140
+my $gnuplot_script_tmpfile = File::Temp->new(SUFFIX => '.gnuplot');
141
+my $fn = $gnuplot_script_tmpfile->filename;
142
+open(FH, ">", $fn) or die "Couldn't open $fn: $!";
143
+print FH << "EOF";
144
+set title "video frame sizes"
145
+set xlabel "frame time"
146
+set ylabel "frame size (Kbits)"
147
+set grid
148
+set terminal "$gnuplot_terminal"
149
+EOF
150
+
151
+print FH "set output \"$gnuplot_output\"\n" if $gnuplot_output;
152
+print FH "plot";
153
+my $sep = "";
154
+foreach my $type (keys %stats) {
155
+    my $fn = $stats{$type}->{tmpfile}->filename;
156
+    print FH "$sep\"$fn\" title \"$type frames\" with impulses";
157
+    print FH " linecolor rgb \"$type_color_map{$type}\"" if $type_color_map{$type};
158
+    $sep = ", ";
159
+}
160
+close FH;
161
+
162
+# launch gnuplot with the generated script
163
+system ("gnuplot", "--persist", $gnuplot_script_tmpfile->filename);