在命令行上检查音频文件的持续时间
我需要检查一组音频文件的持续时间。 有没有一种简单的方法可以在 unix 命令行上执行此操作?
> duration *
我有一个令人惊叹的 SoX 应用程序,它有一个名为“统计”的选项,可以生成一组包括持续时间的音频信息。我正在寻找一种只获得持续时间的方法。 我对输出格式很灵活,可以是任何样本长度、hh:mm:ss 或秒。 后者是我的偏好。
I need to check the duration of a group of audio files.
Is there a simple way to do this on the unix command-line?
> duration *
I have the amazing SoX app which has an option called stats that generates a set of audio info including duration. I am looking for a way to get only duration.
I am flexible on output format, could be any of sample length, hh:mm:ss, or seconds.
The latter would be my preference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
Soxi查询音频文件的元数据; D 是持续时间选项。它支持通配符。 Soxi 的老大哥 sox 负责命令行音频处理。
Soxi queries metadata of audio files; D is the duration option. It supports globbing. Soxi's big brother sox does command-line audio processing.
以
mm:ss
格式给出指定文件的长度(mm 可以大于 59)。对于文件中的总秒数,您可以使用:要获取所有 mp3 文件的总长度(以秒为单位),AWK 可以提供帮助:
gives you the length of the specified file in
mm:ss
format (mm can be greater than 59). For just the total number of seconds in the file, you'd use:To get the total length of all the mp3 files in seconds, AWK can help:
mediainfo
将返回音频文件的毫秒数。假设当前目录只有音频文件,下面要计算本地目录下所有音频的时长,这个要点将帮助:
mediainfo
will return to you the milliseconds of an audio file. Assuming the current directory only has audio files, the followingTo calculate the duration of all audio in the local directory, this gist will help:
使用 ffmpeg 的
ffprobe
可以高精度获得以秒为单位的原始持续时间,如下所示:输出格式如下: 在此基础
上扩展 ,易于在进一步的脚本编写中使用,以下将测量当前目录中所有 .mp3 文件的总持续时间(以秒为单位):
并且为了测量多个扩展名的音频文件的总长度,可以附加另一个通配符:
The raw duration in seconds can be obtained with a high degree of precision with the use of
ffprobe
of ffmpeg, as follows:The output, easy to use in further scripting, is formatted like this:
Extending upon that, the following will measure the total duration in seconds of all .mp3 files in the current directory:
And to measure the total length of audio files of several extensions, another wildcard may be appended:
在 OSX
打印当前目录中每个音频文件的长度:
包括文件路径:
on OSX
Print the length of each audio file in the current dir:
Include the filepath:
与
ffprobe
输出如下所示:
with
ffprobe
The output looks like this:
除了 cdosborn 的 答案 之外,还要递归计算子文件夹中所有
.mp3
文件的长度当前目录并以天:小时:分钟:秒
格式显示总和结果:在zsh中:
在bash或sh中:
结果是这样的(7天, 5小时6分58秒):
In addition to cdosborn's answer, to calculate the length of all
.mp3
files recursively in subfolders of current directory and show the total sum result indays:hours:minutes:seconds
format:In zsh:
In bash or sh:
The result is like this (7 days, 5 hours, 6 minutes, 58 seconds):
如果您有兴趣使用 soxi 和 python 查找目录中 wav 文件的总持续时间,您可以使用这个:
根据您的输入目录更改
input_dir
。如果您想查找所有 wav 文件之间的最大/最小持续时间,请随意将
sum
更改为max
或min
。If you are interested in finding total duration of wav files in a directory using soxi and python you can use this:
change
input_dir
according to your input directory.If you want to find max/min duration between all wav files feel free to change
sum
tomax
ormin
.基于 命令行 作者:syssyphus 适用于音频和视频文件:
输出示例:
00:00:37
A solution based on mplayer from commandlinefu by syssyphus that works with both audio and video files:
Example of output:
00:00:37
我在这里扩展了 Navid Naderi 的答案,并创建了一个 bash 函数,该函数将为您提供每个文件的运行时间以及一些时间的摘要各种格式的总计(秒、分钟或总运行时间)。
输出将如下所示:
I expanded on Navid Naderi's answer here, and created a bash function that will give you a summary of every file's running time, plus some time totals in various formats (seconds, minutes, or total running time).
Output will look like:
另一个基于 soxi 的答案,包括文件名和小时、分钟和秒格式的持续时间。
Another soxi based answer including the file names and duration in hours, minutes and seconds format.
mediainfo 可以做到这一点,但是 mediainfo 是那些有用的工具之一,但它的文档记录非常糟糕,以至于您几乎需要知道如何使用它才能学习如何使用它(在 Linux 世界中经常发生)。
经过几个小时的试验和反复阅读,我终于让它生成一个以逗号分隔的文件名及其持续时间(以毫秒为单位)的递归列表。
cd 到起始目录并发出以下命令:
结果将输出到list.txt。
mediainfo can do this, but mediainfo is one of those useful tools that's so badly documented that you almost need to know how to use it in order to learn how to use it (happens a lot in the linux world).
After hours of trials and reading high and low, I finally got it to generate a recursive comma-separated list of file names and their duration in milliseconds.
cd to the starting directory and issue the following command:
The results will be output to list.txt.
对于 MP3 有很多选择。
说到 OGG,TinyTag 是我迄今为止找到的最好的。
Shell
或 Python
读取音频文件的标签、长度和封面图像
支持的格式
For MP3 there are many options.
When it comes to OGG, TinyTag is the best I found so far.
Shell
Or Python
Read tags, length and cover images of audio files
supported formats
Debian/Ubuntu 上的 FLAC 非常简单的单行代码,对几乎所有音频容器文件和 SoX 都很有用:
也
可以使用它,但随后您将需要
sed
或awk
和 grep 将输出转换为紧凑形式。Very simple single-liner for FLAC on Debian/Ubuntu, useful for almost any audio container files and SoX:
Also
could be used instead, but then you will need
sed
orawk
andgrep
to turn the output in the compact form.(当你没有
afinfo
可供使用时)我使用find命令递归地获取所有文件,将总秒数放入csv文件中(转到包含例如mp3文件的目录首先)
然后在 LibreOffice 中打开它并在底部总结(以获得小时数)
(When you don't have
afinfo
at your disposal) I got it recursively for all my fileswith the find command, put the total seconds to a csv file (go to the directory with your e.g. mp3 files first)
Then open it in e.g. in LibreOffice and sum it up at the bottom (to get the hours) with
使用
ffprobe
获取大多数音视频文件时长的方式,也可以配置为列表(这里以BBcode列表为例):The way to obtain durations for most audio and video files using
ffprobe
, it is also configurable to make a list (the example here is for BBcode listing):也许有点晚了,但这是我的版本。
如果你不想冗长,只需在里面注释 printf 即可获得总数
Maybe a little bit late, but this is my version.
if you don't wanna verbose, just comment printf inside to get only total
扩展 kevin meinert 的答案,我已将脚本修改为:
-r
标志进行搜索递归地snare/*.wav soundtracks/starwars.wav
(与之前的行为相同)我还将脚本添加到
./.local/bin
而不是让它成为一个函数,因此我可以通过运行wavdur
来在任何地方调用它示例:
修改后的脚本:
Expanding on kevin meinert's answer, I have modified the script to:
-r
flag to search recursivelysnare/*.wav soundtracks/starwars.wav
(same as previous behavior)I also added the script to
./.local/bin
rather than having it be a function, so I can call it anywhere by just runningwavdur
Example:
The modified script: