我試圖使用avconv
命令列將一些 .ape 檔案轉換為 .flac 檔案;顯然,avconv
這不是重點;它的語法非常簡單,avconv -i inputApeFile.ape outputFlacFile.flac
.
重點是文件嵌套在更多的子資料夾中;即,我有 Artist 資料夾,然後是各種 CD 子資料夾,每個子資料夾都包含不同的 .ape 檔。如何轉換所有文件,然後將它們保存在原始文件的相同資料夾中,但副檔名為 .flac?
如果可能的話,我想在一行中只使用 shell 命令而不使用腳本。我認為應該是這樣的
avconv -i 'ls -R | grep ape' '???'
但我堅持第二部分(也許使用sed
??!?)
答案1
您需要的命令是:
find /path/to/MainDir/ -type f -name "*.ape" -execdir sh -c ' avconv -i "$1" "${1%.ape}.flac" ' _ {} \;
這將找到每個具有.ape
後綴的文件,然後使用具有.flac
後綴的相同文件名將其轉換到與原始文件所在的位置相同的位置。
{}
是目前找到的文件的路徑。
請參閱來自的測試這裡
答案2
下面的(python)腳本應該可以完成這項工作。將其複製到空文件中,另存為convert.py
,將目錄設定為腳本頭部分的檔案(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
答案3
現在 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 "$@"' _ "{}"
答案4
您正在尋找的一行命令:
find -type f -name "*.ape" -print0 | xargs -0 avconv -i
find
命令將僅提供以以下結尾的文件。
該find
命令將給出命令的相對路徑avconv
,以便它可以轉換這些檔案並將它們保存在與輸入檔案(即 .ape)相同的資料夾中。
find
命令將查找該目錄中的所有文件,無論它們在子目錄中的保存深度如何