gpt4 book ai didi

linux - 按比特率对 mp3 文件进行排序的 Bash 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:16 24 4
gpt4 key购买 nike

我已经把这个脚本 https://www.linuxquestions.org/questions/linux-newbie-8/finding-corrupt-mp3s-in-library-632053/最初似乎工作正常,并将其修改为这个以列出低于 320 kbps 的轨道,但我遇到了意外的文件结束错误。不过,仅这些命令似乎就可以正常工作。

find . -maxdepth 1 -iname '*.mp3' -exec sh -c 'curfname="{}";\
bitrate=`mp3info -r a -p "%f %r\n" "$curfname" | awk 'NF>1{print $NF}'`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi ' \; | tee ./lowbitrate.log

哪位好心人能指出我错在哪里?

编辑:

这是我得到的

~/bin/mp3bitrate.sh ~/Music
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 1: unexpected EOF while looking for matching ``'
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 2: syntax error: unexpected end of file
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 1: unexpected EOF while looking for matching ``'
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 2: syntax error: unexpected end of file
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 1: unexpected EOF while looking for matching ``'
}`;\
if [ $bitrate -lt 320 ];\
then \
echo $curfname has $bitrate bitrate; \
fi : -c: line 2: syntax error: unexpected end of file

好的,我已经将第二行更改为:

bitrate=`mp3info -r a -p "%r\n" "$curfname"`;\

这样就不需要 awk,因为它会导致问题。但是现在我比较了一个 float 和整数,用

改变了 if 语句
if [ $(echo "$bitrate < 320" | bc ) -e 1 ]

返回:第 2 行:[: -e: 预期的二元运算符

但单独运行命令似乎可行:

echo "234.54444444<320"|公元前

返回1,我想知道现在出了什么问题!

最佳答案

尝试:

mp3info -r a -p "%f has %r bitrate\n" ./*.[Mm][Pp]3 | awk '$(NF-1)<320'

工作原理:

  • -p "%f has %r bitrate\n"以您想要的格式打印信息。

  • awk '$(NF-1)<320'仅选择比特率小于 320 的那些行。

改进

如果文件名本身包含换行符,上述操作将失败。处理这个问题的通常方法是使用 NUL 分隔的输出,但据我所知,mp3info不支持这个。在我们的例子中,输出永远不会包含 / .这是因为,虽然 /可以是路径的一部分,它绝不是 %f 中文件名的一部分.因此我们可以使用:

mp3info -r a -p "%f has %r bitrate/" ./*.[Mm][Pp]3 | awk -v RS=/ '$(NF-1)<320'

示例输出

$ mp3info -r a -p "%f has %r bitrate/" ./*.[Mm][Pp]3 | awk -v RS=/ '$(NF-1)<320'
gd77-05-08d1t01.mp3 has 186.720474 bitrate
gd77-05-08d1t02.mp3 has 183.336975 bitrate
gd77-05-08d1t03.mp3 has 183.020111 bitrate
...

递归到子目录(安全版本)

要遍历当前目录及其所有子目录以查找具有 mp3 扩展名的文件,我们可以使用 find .最安全的方法是使用 find-execdir :

find . -iname '*.mp3' -execdir mp3info -r a -p "%f has %r bitrate/" {} +  | awk -v RS=/ '$(NF-1)<320'

例如:

$ find . -iname '*.mp3' -execdir mp3info -r a -p "%f has %r bitrate/" {} +  | awk -v RS=/ '$(NF-1)<320'
gd77-05-08d1t05.mp3 has 184.018967 bitrate
gd77-05-08d3t05.mp3 has 189.467697 bitrate
gd77-05-08d1t08.mp3 has 183.625366 bitrate
gd77-05-08d1t09.mp3 has 183.826721 bitrate
...

递归到子目录(不太安全的版本)

如果您的 PATH是“不安全的”,然后使用 find-exec :

find . -iname '*.mp3' -exec mp3info -r a -p "%f has %r bitrate/" {} +  | awk -v RS=/ '$(NF-1)<320'

这会产生相同的输出:

$ find . -iname '*.mp3' -exec mp3info -r a -p "%f has %r bitrate/" {} +  | awk -v RS=/ '$(NF-1)<320'
gd77-05-08d1t05.mp3 has 184.018967 bitrate
gd77-05-08d3t05.mp3 has 189.467697 bitrate
gd77-05-08d1t08.mp3 has 183.625366 bitrate
gd77-05-08d1t09.mp3 has 183.826721 bitrate
...

关于linux - 按比特率对 mp3 文件进行排序的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56209192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com