make_chlayout_test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env perl
  2. # Copyright (c) 2012 Nicolas George
  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. make_chlayout_test - produce a multichannel test file with the channels
  21. clearly identified
  22. =head1 SYNOPSIS
  23. tools/make_chlayout_test I<channels> I<out_options>
  24. =head1 DESCRIPTION
  25. This script uses B<ffmpeg> and B<libflite> to produce a file with audio
  26. channels clearly identified by their name. The resulting file can be used to
  27. check that the layout and order of channels is correctly handled by a piece
  28. of software, either a part of B<FFmpeg> or not.
  29. I<channels> is a list of channels or channel layouts, separated by '+'.
  30. I<out_options> is a list of valid ffmpeg outout options, including the
  31. output file.
  32. Note that some output codecs or formats can not handle arbitrary channel
  33. layout.
  34. This script requires a B<ffmpeg> binary, either in the source tree or in the
  35. search path; it must have the flite audio source enabled.
  36. =head1 EXAMPLES
  37. Check that the speakers are correctly plugged:
  38. tools/make_chlayout_test FL+FR -f alsa default
  39. Produce a 5.1 FLAC file:
  40. tools/make_chlayout_test 5.1 surround.flac
  41. =cut
  42. use strict;
  43. use warnings;
  44. use Getopt::Long ":config" => "require_order";
  45. use Pod::Usage;
  46. GetOptions (
  47. "help|usage|?|h" => sub { pod2usage({ -verbose => 1, -exitval => 0 }) },
  48. "manpage|m" => sub { pod2usage({ -verbose => 2, -exitval => 0 }) },
  49. ) and @ARGV >= 2 or pod2usage({ -verbose => 1, -exitval => 1 });
  50. my $channels = shift @ARGV;
  51. my @out_options = @ARGV;
  52. my $ffmpeg = exists $ENV{FFMPEG} ? $ENV{FFMPEG} :
  53. $0 =~ /(.*)\// && -e "$1/../ffmpeg" ? "$1/../ffmpeg" :
  54. "ffmpeg";
  55. my %channel_label_to_descr;
  56. my %layout_to_channels;
  57. {
  58. open my $stderr, ">&STDERR";
  59. open STDERR, ">", "/dev/null";
  60. open my $f, "-|", $ffmpeg, "-layouts" or die "$ffmpeg: $!\n";
  61. open STDERR, ">&", $stderr;
  62. while (<$f>) {
  63. chomp;
  64. next if /^NAME/ or /:$/ or /^$/; # skip headings
  65. my ($name, $descr) = split " ", $_, 2;
  66. next unless $descr;
  67. if ($descr =~ /^[[:upper:]]+(?:\+[[:upper:]]+)*$/) {
  68. $layout_to_channels{$name} = [ split /\+/, $descr ];
  69. } else {
  70. $channel_label_to_descr{$name} = $descr;
  71. }
  72. }
  73. }
  74. my @channels = map { @{$layout_to_channels{$_} // [$_]} } split /\+/, $channels;
  75. my $layout = join "+", @channels;
  76. my $graph = "";
  77. my $concat_in = "";
  78. for my $i (0 .. $#channels) {
  79. my $label = $channels[$i];
  80. my $descr = $channel_label_to_descr{$label}
  81. or die "Channel $label not found\n";
  82. $graph .= "flite=text='${descr}', aformat=channel_layouts=mono, " .
  83. "pan=${layout}:${label}=c0 [ch$i] ;\n";
  84. $concat_in .= "[ch$i] ";
  85. }
  86. $graph .= "${concat_in}concat=v=0:a=1:n=" . scalar(@channels);
  87. exec $ffmpeg, "-f", "lavfi", "-i", $graph, @out_options
  88. or die "$ffmpeg: $!\n";