annotate ffmpeg/tools/plotframes @ 13:844d341cf643 tip

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