嗨我正在尝试获取视频持续时间,并将其插入到带有视频的数据库中。但现在我正在使用ffmpeg提取视频持续时间。在过去的一周里,我一直在做这件事。我能够在linux中实现这一点。但在窗口中,它无法打印持续时间。这是代码。
use strict;
use warnings;
use IPC::Open3;
my @list = glob('*.mp3 *.mp4 *.mpg *.aac *.midi *m3u *.mpa *.dat *.avi');
foreach my $song (@list){
print "$song n";
my $filename = $song;
my %videoInfo = videoInfo($filename);
print "duration: " . $videoInfo{'duration'}. "n";
print "durationsecs: " . $videoInfo{'durationsecs'}. "nn";
}
sub videoInfo {
# ffmpeg command
my $ffmpeg = 'C:ffmpeg.exe';
my %finfo = (
'duration' => "00:00:00.00",
'durationsecs' => "0"
);
my $file = shift;
# escaping characters
$file =~ s/(W)/$1/g;
open3( "nul", "nul", *ERPH, "$ffmpeg -i $file" ) or die "can't run $ffmpegn";
my @res = <ERPH>;
foreach (@res) {
#duration
if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
$finfo{'duration'} = $1;
}
}
my $tenths = substr( $finfo{'duration'}, 9, 2 );
my $seconds = substr( $finfo{'duration'}, 6, 2 );
my $minutes = substr( $finfo{'duration'}, 3, 2 );
my $hours = substr( $finfo{'duration'}, 0, 2 );
$finfo{'durationsecs'} = ( $tenths * .01 ) + $seconds + ( $minutes * 60 ) + ( $hours * 360 );
return %finfo;
}
我在C:\中安装了ffmpeg,并给出了路径。
ffmpeg工作得很好,正在显示比特率、编解码器等(这里我已经删除了它,因为我只需要持续时间(但它不打印任何持续时间,它只打印00:00:00.00。
我怀疑是数组中的搜索问题。
请帮我渡过难关。。
这是我从命令提示符运行ffmpeg时的输出
C:>ffmpeg.exe -i cisco1.flv
FFmpeg version Sherpya-r10707, Copyright (c) 2000-2007 Fabrice Bellard, et al.
libavutil version: 49.5.0
libavcodec version: 51.45.0
libavformat version: 51.14.0
built on Oct 11 2007 06:25:25, gcc: 4.2.1 [Sherpya]
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cisco1.flv':
Duration: 00:08:37.0, start: 0.000000, bitrate: 421 kb/s
Stream #0.0(und): Audio: mpeg4aac, 44100 Hz, stereo
Stream #0.1(und): Video: h264, yuv420p, 960x720, 29.97 fps(r)
Must supply at least one output file
在这里你可以看到持续时间很好。
如果我添加
print;
行下方的上述代码中的声明
foreach (@res) {
它将显示相同的ffmpeg输出
您匹配:
if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
但输出是:
Duration: 00:08:37.0, start: 0.000000, bitrate: 421 kb/s
请注意,.
后面只有一个数字。
尝试:
if (m!Duration:s+([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]?)!) {
如果.
后面有一个或两个数字,它将匹配(您应该在正则表达式中转义.
,否则它将匹配任何单个字符(。
$ cat input
FFmpeg version Sherpya-r10707, Copyright (c) 2000-2007 Fabrice Bellard, et al.
libavutil version: 49.5.0
libavcodec version: 51.45.0
libavformat version: 51.14.0
built on Oct 11 2007 06:25:25, gcc: 4.2.1 [Sherpya]
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cisco1.flv':
Duration: 00:08:37.0, start: 0.000000, bitrate: 421 kb/s
Stream #0.0(und): Audio: mpeg4aac, 44100 Hz, stereo
Stream #0.1(und): Video: h264, yuv420p, 960x720, 29.97 fps(r)
Must supply at least one output file
$ cat input | perl -ne 'if (m!Duration:s+([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]?)!) { print $1, "n"; }'
00:08:37.0