将所有.ape文件转换为不同子文件夹中的.flac文件

我试图在命令行上使用avconv将一些.ape文件转换为.flac文件; 显然, avconv不是这里的重点; 它的语法非常简单, avconv -i inputApeFile.ape outputFlacFile.flac

关键是文件嵌套在更多的子文件夹中; 即,我有Artist文件夹,然后是各种CD子文件夹,每个子文件夹包含不同的.ape文件。 如何转换所有文件,然后将它们保存在原始文件的同一文件夹中,但扩展名为.flac?

如果可能的话,我想在一行上只使用shell命令而不使用脚本。 我认为它应该是那样的

 avconv -i 'ls -R | grep ape' '???' 

但我坚持第二部分(也许使用sed ??!?)

您需要的命令是:

 find /path/to/MainDir/ -type f -name "*.ape" -execdir sh -c ' avconv -i "$1" "${1%.ape}.flac" ' _ {} \; 

这将找到每个具有.ape后缀的文件,然后将其与.flac后缀相同的文件名转换为与原始文件所在的位置相同的位置。

{}是当前找到的文件的路径。
从这里看测试

下面的(python)脚本应该完成这项工作。 将其复制到一个空文件中,将其另存为convert.py ,将目录设置为脚本head部分中的文件( convert_dir = )并通过以下命令运行:

 python3 /path/to/convert.py 

剧本

 #!/usr/bin/env python3 convert_dir = "/path/to/folder/tobeconverted" import os import subprocess for root, dirs, files in os.walk(convert_dir): for name in files: if name.endswith(".ape"): # filepath+name file = root+"/"+name # to use in other (convert) commands: replace the "avconv -i" by your command, and; # replace (".ape", ".flac") by the input / output extensions of your conversion command = "avconv -i"+" "+file+" "+file.replace(".ape", ".flac") subprocess.Popen(["/bin/bash", "-c", command]) else: pass 

现在ffmpeg再次优于avconv,并且有许多廉价的核心计算机(8核XU4为60美元)我认为以下是最有效的;

 #!/bin/bash # # ape2flac.sh # function f2m(){ FILE=$(echo "$1" | perl -p -e 's/.ape$//g'); if [ ! -f "$FILE".flac ] ; then ffmpeg -v quiet -i "$FILE.ape" "$FILE.flac" fi } export -f f2m find "$FOLDER" -name '*.ape' | xargs -I {} -P $(nproc) bash -c 'f2m "$@"' _ "{}" 

你正在寻找的一行命令:
find -type f -name "*.ape" -print0 | xargs -0 avconv -i
find命令只提供以.ape结尾的文件
find命令将给出avconv命令的相对路径,以便它可以转换这些文件并将它们保存在与输入文件相同的文件夹中(即.ape)。
find命令将查找该目录中的所有文件,而不管它们在子目录中保存的深度