目录(有多层子文件夹)中所有音频文件(各种格式)的总长度(Linux)

问题描述 投票:0回答:1

我有一个很大的目录,其中包含各种音频文件,mp3、ogg、opus、m4a 以及可能的其他文件。

我想获取所有这些文件的总长度。如果只是少量文件,我会将它们全部复制到另一个文件夹中并使用

mp3info
,但它有几百 GB,所以这不是一个选择。

我认为我不能在我的用例中使用

mp3info
,因为我找不到递归搜索的选项。
mp3info2
似乎有一个带有
-R
的递归选项,但我找不到很多关于
mp3info2
的信息,所以我不知道如何使用它。

我试过这个

tot=0; while read -r i; do tmp=0;  tmp=`ffprobe "$i" -show_format 2>/dev/null | grep "^duration" | cut -d '=' -f 2 | cut -d '.' -f 1`; if [ -n "$tmp" ]; then let tot+=$tmp; fi;    done < <(find . -type f -iname "*[.mp3,.wav,.m3u,.m4a,.m4b,.mpga,.opus,.opus]"); echo "Total duration: $(($tot/60)) minutes"

但是得到

bash: let: tot+=N/A: division by 0 (error token is "A")
bash: let: tot+=N/A: division by 0 (error token is "A")
bash: let: tot+=N/A: division by 0 (error token is "A")
bash: let: tot+=N/A: division by 0 (error token is "A")

重复。

我尝试过

soxi -D *.mp3
,然后会执行不同的文件类型,但得到这个

soxi FAIL formats: can't open input file `*.mp3': No such file or directory

目录格式为信件/作者/书名,例如:

K/King, Stephen/The Stand/The Stand.mp3

作为额外问题:我如何对视频文件(在不同的目录中)做同样的事情

谢谢

bash audio
1个回答
0
投票

这很可能是因为您的

find
会发现很多不是多媒体文件的文件。当遇到这样的文件时,
ffprobe
可能会输出这样的内容:

[FORMAT]
filename=./pong.cpp
nb_streams=1
nb_programs=0
format_name=xbm_pipe
format_long_name=piped xbm sequence
start_time=N/A
duration=N/A
size=17182
bit_rate=N/A
probe_score=99
[/FORMAT]

如您所见,

duration
在这里
N/A

  • 首先,修复
    find
    命令。可以使用多个
    -iname
    ,中间有
    -o
    (或),或者使用
    -regex
    而不是
    iname
    (如果您的
    find
    支持)。
  • 即使您找到了文件名正确的文件,它也可能已损坏,并且持续时间可能仍会返回为
    N/A
    ,因此通过检查您是否获得了号码来使检查更安全。

示例:

#!/bin/bash

tot=0
while read -r i; do
    tmp=0
    echo "testing $i"
    tmp=$(ffprobe "$i" -show_format 2>/dev/null | grep "^duration" | cut -d '=' -f 2 | cut -d '.' -f 1)
    if [[ $tmp =~ ^[0-9.]+$ ]]; then
        (( tot+=tmp ))
    fi;
done < <(find . -type f -iname '*.mp3' -o -iname '*.wav' -o -iname '*.m3u' -o -iname '*..m4a' -o -iname '*.m4b' -o -iname '*..mpga' -o -iname '*..opus' -o -iname '*.opus')

echo "Total duration: $((tot/60)) minutes"
© www.soinside.com 2019 - 2023. All rights reserved.