bash:转换子文件夹中的文件并输出到另一个文件夹



我的文件组织为G:\Songs\Sounds-FLAC%专辑艺术家%%album%\。我有一个bash脚本来修剪音轨开头和结尾的静音,并将其输出为mp3。如果我从G:\Songs\Songs-FLAC运行脚本,它将不会转换子文件夹中的曲目。是否有任何参数可以转换子文件夹中的文件?此外,我想将修剪后的歌曲输出到G:\songs\trimmed。这可能吗?

脚本:

#!/bin/bash
for file in *.flac
do
ffmpeg -i "$file" -af silenceremove=start_periods=1:start_duration=1:start_threshold=-65dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-65dB:detection=peak,aformat=dblp,areverse -q:a 5 "${file%.*}.mp3"
done;

感谢提供的任何帮助

使用globstar

您的匹配将包括任意深度的子目录,因此您的命令逻辑和输出文件名的参数解析甚至不需要更改。

$: shopt -s globstar
$: printf "%sn" **/*.flac
foo/bar.flac
foo/there.flac
here.flac

新名称的参数解析非常简单-

for file in **/*.flac; do
echo "$file -> ${file%.*}.mp3"
done
foo/bar.flac -> foo/bar.mp3
foo/there.flac -> foo/there.mp3
here.flac -> here.mp3

因此,对于您的脚本,假设您的参数是正确的,

shopt -s globstar
for file in **/*.flac do
bare="${file##*/}"
ffmpeg -i "$file" -af silenceremove=start_periods=1:start_duration=1:start_threshold=-65dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-65dB:detection=peak,aformat=dblp,areverse -q:a 5 "/new/location/${bare%.*}.mp3"
done

确保在循环之前包含shopt s globstar,并将我所说的/new/location/更改为实际需要修剪文件的位置。

最新更新