您好 我正在尝试获取视频时长并使用视频插入数据库。 但现在我正在使用ffmpeg提取视频时长。 我正在研究过去一周。 我能够在linux中完成这项工作。 但在Windows中它只是无法打印持续时间。 这是代码。
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'}. "\n\n";
}
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 $ffmpeg\n";
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工作正常,显示比特率,编解码器等等(这里我已删除它,因为我只需要持续时间)但它的jus不打印任何持续时间,它只是打印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输出
答案 0 :(得分:2)
你匹配:
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