如何使用 sox 获取音频文件总和的长度?
How to get the length of the sum of audio files using sox?
我有一个脚本如下
DIR=
ls |sort -R |tail - |while read file; do
soxi -d $file
done
它给了我一个像
这样的输出
00:40:35.18
00:43:41.23
01:04:19.64
00:59:41.92
00:51:16.32
如您所见,程序从文件夹中随机选择一个文件(音频)并检查每个文件的时间。但是,我需要所有音频的总长度。
如何获取音频文件总数?
- 使用
soxi
的 -D
选项获取以(小数)秒为单位的持续时间
- 使用
bc
将秒数加到总数中
- Don't use
ls
to list files
- Don't pipe into a while loop,因为需要设置变量,循环后会丢失
举个例子,在某个目录下随机选择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"
(为简单起见,第二个示例没有随机选择)
您可以组合 -D
和 -T
来获得以秒为单位的总长度,例如
soxi -DT *.wav
来源:
我有一个脚本如下
DIR=
ls |sort -R |tail - |while read file; do
soxi -d $file
done
它给了我一个像
这样的输出00:40:35.18
00:43:41.23
01:04:19.64
00:59:41.92
00:51:16.32
如您所见,程序从文件夹中随机选择一个文件(音频)并检查每个文件的时间。但是,我需要所有音频的总长度。
如何获取音频文件总数?
- 使用
soxi
的-D
选项获取以(小数)秒为单位的持续时间 - 使用
bc
将秒数加到总数中 - Don't use
ls
to list files - Don't pipe into a while loop,因为需要设置变量,循环后会丢失
举个例子,在某个目录下随机选择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"
(为简单起见,第二个示例没有随机选择)
您可以组合 -D
和 -T
来获得以秒为单位的总长度,例如
soxi -DT *.wav
来源: