plotframes 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env perl
  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. =head1 NAME
  20. plotframes - Plot video frame sizes using ffprobe and gnuplot
  21. =head1 SYNOPSIS
  22. plotframes [I<options>] [I<input>]
  23. =head1 DESCRIPTION
  24. plotframes reads a multimedia files with ffprobe, and plots the
  25. collected video sizes with gnuplot.
  26. =head1 OPTIONS
  27. =over 4
  28. =item B<--input|-i> I<infile>
  29. Specify multimedia file to read. This is the file passed to the
  30. ffprobe command. If not specified it is the first argument passed to
  31. the script.
  32. =item B<--help|--usage|-h|-?>
  33. Print a brief help message and exit.
  34. =item B<--manpage|-m>
  35. Print the man page.
  36. =item B<--output|-o> I<outfile>
  37. Set the name of the output used by gnuplot. If not specified no output
  38. is created. Must be used in conjunction with the B<terminal> option.
  39. =item B<--stream|--s> I<stream_specifier>
  40. Specify stream. The value must be a string containing a stream
  41. specifier. Default value is "v".
  42. =item B<--terminal|-t> I<terminal>
  43. Set the name of the terminal used by gnuplot. By default it is
  44. "x11". Must be used in conjunction with the B<output> option. Check
  45. the gnuplot manual for the valid values.
  46. =back
  47. =cut
  48. =head1 SEE ALSO
  49. ffprobe(1), gnuplot(1)
  50. =cut
  51. use warnings;
  52. use strict;
  53. use File::Temp;
  54. use JSON -support_by_pp;
  55. use Getopt::Long;
  56. use Pod::Usage;
  57. my $input = $ARGV[0];
  58. my $stream_specifier = "v";
  59. my $gnuplot_terminal = "x11";
  60. my $gnuplot_output;
  61. GetOptions (
  62. 'input|i=s' => \$input,
  63. 'help|usage|?|h' => sub { pod2usage ( { -verbose => 1, -exitval => 0 }) },
  64. 'manpage|m' => sub { pod2usage ( { -verbose => 2, -exitval => 0 }) },
  65. 'stream|s=s' => \$stream_specifier,
  66. 'terminal|t=s' => \$gnuplot_terminal,
  67. 'output|o=s' => \$gnuplot_output,
  68. ) or pod2usage( { -message=> "Parsing error", -verbose => 1, -exitval => 1 });
  69. die "You must specify an input file\n" unless $input;
  70. # fetch data
  71. my @cmd = (qw{ffprobe -show_entries frame -select_streams}, $stream_specifier, "-of", "json", $input);
  72. print STDERR "Executing command: @cmd\n";
  73. my $json_struct;
  74. {
  75. open(FH, "-|", @cmd) or die "ffprobe command failed: $!\n";
  76. local $/;
  77. my $json_text = <FH>;
  78. close FH;
  79. die "ffprobe command failed" if $?;
  80. eval { $json_struct = decode_json($json_text); };
  81. die "JSON parsing error: $@\n" if $@;
  82. }
  83. # collect and print frame statistics per pict_type
  84. my %stats;
  85. my $frames = $json_struct->{frames};
  86. my $frame_count = 0;
  87. foreach my $frame (@{$frames}) {
  88. my $type = $frame->{pict_type};
  89. $frame->{count} = $frame_count++;
  90. if (not $stats{$type}) {
  91. $stats{$type}->{tmpfile} = File::Temp->new(SUFFIX => '.dat');
  92. my $fn = $stats{$type}->{tmpfile}->filename;
  93. open($stats{$type}->{fh}, ">", $fn) or die "Can't open $fn";
  94. }
  95. print { $stats{$type}->{fh} }
  96. "$frame->{count} ", $frame->{pkt_size} * 8 / 1000, "\n";
  97. }
  98. foreach (keys %stats) { close $stats{$_}->{fh}; }
  99. # write gnuplot script
  100. my %type_color_map = (
  101. "I" => "red",
  102. "P" => "green",
  103. "B" => "blue"
  104. );
  105. my $gnuplot_script_tmpfile = File::Temp->new(SUFFIX => '.gnuplot');
  106. my $fn = $gnuplot_script_tmpfile->filename;
  107. open(FH, ">", $fn) or die "Couldn't open $fn: $!";
  108. print FH << "EOF";
  109. set title "video frame sizes"
  110. set xlabel "frame time"
  111. set ylabel "frame size (Kbits)"
  112. set grid
  113. set terminal "$gnuplot_terminal"
  114. EOF
  115. print FH "set output \"$gnuplot_output\"\n" if $gnuplot_output;
  116. print FH "plot";
  117. my $sep = "";
  118. foreach my $type (keys %stats) {
  119. my $fn = $stats{$type}->{tmpfile}->filename;
  120. print FH "$sep\"$fn\" title \"$type frames\" with impulses";
  121. print FH " linecolor rgb \"$type_color_map{$type}\"" if $type_color_map{$type};
  122. $sep = ", ";
  123. }
  124. close FH;
  125. # launch gnuplot with the generated script
  126. system ("gnuplot", "--persist", $gnuplot_script_tmpfile->filename);