Shell 使用mplayer确定音频/视频文件的长度

Shell 使用mplayer确定音频/视频文件的长度,shell,Shell,以下内容可以很好地确定各种音频/视频文件的长度: mplayer -identify file.ogg 2>/dev/null | grep ID_LENGTH 但是,我想终止mplayer的输出,以便更有效地确定许多文件的长度。如何做到这一点?FFMPEG可以以不同的格式为您提供相同的信息(并且不会尝试播放该文件): ffmpeg-i MPlayer源代码附带一个名为midentify的示例脚本,如下所示: #!/bin/sh # # This is a wrapper around

以下内容可以很好地确定各种音频/视频文件的长度:

mplayer -identify file.ogg 2>/dev/null | grep ID_LENGTH

但是,我想终止mplayer的输出,以便更有效地确定许多文件的长度。如何做到这一点?

FFMPEG可以以不同的格式为您提供相同的信息(并且不会尝试播放该文件):

ffmpeg-i

MPlayer源代码附带一个名为
midentify
的示例脚本,如下所示:

#!/bin/sh
#
# This is a wrapper around the -identify functionality.
# It is supposed to escape the output properly, so it can be easily
# used in shellscripts by 'eval'ing the output of this script.
#
# Written by Tobias Diedrich <ranma+mplayer@tdiedrich.de>
# Licensed under GNU GPL.

if [ -z "$1" ]; then
        echo "Usage: midentify.sh <file> [<file> ...]"
        exit 1
fi

mplayer -vo null -ao null -frames 0 -identify "$@" 2>/dev/null |
        sed -ne '/^ID_/ {
                          s/[]()|&;<>`'"'"'\\!$" []/\\&/g;p
                        }'
#/垃圾箱/垃圾箱
#
#这是一个围绕-标识功能的包装器。
#它应该能够正确地逃逸输出,因此可以轻松地执行
#通过“评估”此脚本的输出在ShellScript中使用。
#
#托比亚斯·迪德里奇写
#根据GNU GPL许可。
如果[-z“$1”];然后
echo“用法:midentify.sh[…]”
出口1
fi
mplayer-vo null-ao null-frames 0-identification“$@”2>/dev/null|
sed-ne'/^ID'/{
s/[]()|和`''\\!$“[]/\\&/g;p
}'

-frames 0
使
mplayer
立即退出,而
-vo null-ao null
阻止它尝试打开任何视频或音频设备。这些选项都记录在
man-mplayer

中,看起来还有一些其他的lib可用,请参见除了@codelogi之外还有另一种FF方式c的方法,该方法不会以错误退出:

ffprobe <file>
ffprobe
并查找持续时间条目

或直接在错误流中对其进行grep:

ffprobe <file> 2> >(grep Duration)
ffprobe 2>>(grep持续时间)

下载.mp3文件,用播放器播放(例如Windows Media Player)玩家将在游戏结束时显示总时间。

请您以问题的形式陈述您的答案,好吗?有没有办法调用它,这样它就不会抱怨没有输出文件?它工作得很好,但如果您试图从另一个程序调用它,非
0
退出代码会很烦人。此外,Wheezy建议我们ing
avconv
而不是
ffmpeg
@Inaimathi:Yes,
ffprobe
会这样做。为什么
ffprobe
中的
error stream
ffmpeg
会产生错误。@Time:
ffprobe
会打印到错误流,尽管它不会以错误代码退出。不确定原因。此操作将阻止用户无法在自动化中运行此类任务,这很可能是他正在尝试做的事情。
ffprobe <file> 2> >(grep Duration)