【问题标题】:How to get the length of the sum of audio files using sox?如何使用 sox 获取音频文件总和的长度?
【发布时间】:2020-11-16 13:05:41
【问题描述】:

我有如下脚本

DIR=$1
ls $1 |sort -R |tail -$5 |while read file; do
    soxi -d $1$file
done

它给了我这样的输出

00:40:35.18
00:43:41.23
01:04:19.64
00:59:41.92
00:51:16.32

如您所见,程序从文件夹中随机选择一个文件(音频)并检查每个文件的时间。但是,我需要所有音频长度的总和。

如何获取音频文件的总数?

【问题讨论】:

  • 使用soxi -D 以秒为单位显示持续时间,因此很容易添加。

标签: bash shell audio sox


【解决方案1】:

所以这里是一个例子,在某个目录下随机选择3个“*.wav”文件

#!/bin/bash
total=0
dir=/some/dir
while read -r f; do
    d=$(soxi -D "$f")
    echo "$d s. in $f"
    total=$(echo "$total + $d" | bc)
done < <(find "$dir" -iname "*.wav" | sort -R | head -3)

echo "Total : $total seconds"

如果您可以使用Mediainfo 代替sox,您可以获得以毫秒为单位的持续时间,并使用标准shell 算法进行加法运算。它还支持更多的音频格式。

#!/bin/bash
total=0
dir=/some/dir
while read -r f; do
    d=$(mediainfo --Output='General;%Duration%' "$f")
    ((total += d))
done < <(find "$dir" -iname "*.wav")

echo "Total : $total ms. or $((total/1000)) seconds"

(为简单起见,第二个示例没有随机选择)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2019-08-02
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多