2
#!/usr/bin/perl -w
################################################################################
##Get_Duration.pl
#
#   This is a perl script which is used to parse the audio files
#   present in the device and build's the xml containing all the 
#   track i.e both audio and video files duration
#
#   The xml file is created in the name of ParsedMetadataInformation.xml
#   in <ATAF Path>/tmp/ directory.
#
#
#   CHANGE HISTORY
#   --------------------------------------------------------------------------

use strict;
use warnings;
use Env;
use File::Find;
use XML::TreePP;
use Data::Dumper;

my $data;

if (not defined $ATAF){
print "=====================================================\n";
print "ERROR: ATAF Path is not set.\n";
print "(Example: export ATAF=/home/roopa/ATAF)\n";
print "=====================================================\n";
exit 1;
}

print "Enter the Absolute path for the device to be scanned\n";
print "(Example: /media/RACE_1.6A)\n";
$DB::single=1;
my @metadataInfo = ();
print "Enter Path:";
my $configDir = <STDIN>;
chomp $configDir;

my @configFiles;
find( sub {push @configFiles, "$File::Find::name$/" if (/\.mp3|\.wma|\.wav|\.ogg|       \.flac|    \.m4a|\.mp4|\.avi|\.mpg|\.mpeg|\.mov|\.wmv|\.m4b$/i)}, $configDir);
chomp @configFiles;

if (!@configFiles){
print "=====================================================\n";
print "ERROR: No Files Found!!!\n";
print "=====================================================\n";
exit -1;
}

my $tpp = XML::TreePP->new();
my $metadataHashTree1 = ();

print "=====================================================\n";
print "Extracting the Metadata Information\n";
print "=====================================================\n";
foreach my $file (@configFiles){
   print "Currently in: $file\n";
   (my $fileName = $file) =~ s/^.*\///g;
   $file =~ s/([\!\$\^\*\&\(\)\|\}\{\[\]\:\"\;\'\?\>\<\,\=\`\s])/\\$1/g; 
   @metadataInfo = (`ffmpeg -i $fileName`);

   my $size= scalar (@metadataInfo);
   #chomp @metadataInfo;
   foreach my $eachfile (@metadataInfo){
       if ($eachfile =~ m/^Duration: /i){
          $eachfile =~ m/Duration:(.*?),/;
          $data= $1;

      $metadataHashTree1->{$fileName}->{'Duration'}=$data;

       }
   }
}

print "=====================================================\n";
print "Building XML tree\n";
print "=====================================================\n\n";
my $xml = $tpp->write($metadataHashTree1);
sleep 5;

print "=====================================================================\n";
print "Writing the XML tree in <ATAF Path>/tmp/ParsedMetadataInformation.xml\n";
print "=====================================================================\n\n";
open (FILEHANDLE, ">$ATAF/tmp/ParsedDurationInformation.xml") or die "ERROR: $!\n";
print FILEHANDLE $xml;
close FILEHANDLE;
sleep 5;
print "=====================================================\n";
print "Successfully Completed!!!\n";
print "=====================================================\n\n";

   ########################################################################################

上記のプログラムでは、コマンドを使用してトラックのデュレーションを取得しffmpeg、出力を に保存しようとしています@metadataInfo。しかし、コマンドを使用して印刷しようとすると、配列サイズが 0 と表示されます

$size= scalar (@metadataInfo);
4

1 に答える 1

3
"$File::Find::name$/" 

する必要があります

$File::Find::name

追加$/しても意味がありません。


ファイル名をシェルリテラルに変換しません。

`ffmpeg -i $fileName`

する必要があります

use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote('ffmpeg', '-i', $fileName);
`$cmd`

これにより、ファイル名のスペースなどの問題が処理されます。


バックティックが成功したかどうかはチェックしません。の価値は$?何ですか?そして、それが-1の場合、の値は$!何ですか?

于 2012-12-19T11:20:29.690 に答える