在perl中使用ffmpeg打印视频时长信息
你好 我正在尝试获取视频持续时间并将视频插入到数据库中。 但现在我正在使用 ffmpeg 提取视频持续时间。 过去 1 周我一直在研究这个问题。 我可以在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;
}
我将 ffmpeg 安装在 C:\ 并给出了路径。
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 输出
Hi
I am trying to get a video duration and insert in into the database with the video.
But now I am working with extracting video duration using ffmpeg.
I am working on this for past 1 week.
I am able to make this work in linux.
But in windows its just not able to print the duration.
Here is the code.
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;
}
I installed ffmpeg in C:\ and given the path.
ffmpeg is working fine and is displaying bitrate, codec etc etc ( Here I have removed it since I only need Duration) But its jus not printing any duration, Its just printing 00:00:00.00.
I suspect its the search problem in the array.
Please help me in getting through this..
Here is the ouput when I run ffmpeg from command prompt
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
Here you can see the duration is fine.
If I add a
print;
Statement in the above code below line
foreach (@res) {
It will display the same ffmpeg output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您匹配:
但输出为:
请注意,
.
后面只有一位数字。尝试:
如果
.
后面有一位或两位数字,则匹配(并且您应该在正则表达式中转义.
,否则它匹配任何单个字符)。You match with:
But the output is:
Notice that there is only one digit after the
.
.Try:
It will match if there is one or two digits after the
.
(and you should escape.
in the regular expression, otherwise it matches any single character).