linux - 在 perl 中插入大量数据后,我的数组显示为空

标签 linux perl

#!/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);

最佳答案

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

应该是

$File::Find::name

附加 $/ 没有任何意义。


您不会将文件名转换为 shell 文字。

`ffmpeg -i $fileName`

应该是

use String::ShellQuote qw( shell_quote );

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

这将处理文件名中的空格等问题。


您不检查反引号是否成功。 $ 的值是多少??如果是 -1,那么 $! 的值是多少?

关于linux - 在 perl 中插入大量数据后,我的数组显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950240/

相关文章:

linux - 使用编排工具将我的应用程序部署到多个虚拟机的集群

linux - 如何使用给定模式 tail -f 最新日志文件

linux - df --mac 的总命令?

mysql - 根据给定的词群,根据 OCR 错误自动构建可能词的输出列表

perl - 无法对未定义的值调用方法 X

c++ - 是否可以调试包含 XS 部分的 Perl 以查看程序流程?

python-3.x - 从 Github 安装 Yocto Bitbake podman-py setuptools 失败

c++ - 在 Linux 上读取系统调用永远占用

perl - 使用 WWW::Mechanize 重定向登录网站

perl - 如何从 Nagios-Report Perl 模块打印出 Nagios 服务 UP 时间百分比