我想将所有* .flac转换为特定文件夹中的* .mp3。
这是我尝试过的,但不起作用:
# change to the home directory
cd ~/music
# convert all *.flac files
ffmpeg -i *.flac -acodec libmp3lame *.mp3
# (optional: check whether there are any errors printed on the terminal)
sleep 60
如何达到目标?
我想将所有* .flac转换为特定文件夹中的* .mp3。
这是我尝试过的,但不起作用:
# change to the home directory
cd ~/music
# convert all *.flac files
ffmpeg -i *.flac -acodec libmp3lame *.mp3
# (optional: check whether there are any errors printed on the terminal)
sleep 60
如何达到目标?
Answers:
尝试这个:
for i in *.flac ; do
ffmpeg -i "$i" -acodec libmp3lame "$(basename "${i/.flac}")".mp3
sleep 60
done
$(basename "${i/.flac}")以处理空格?
for环:IFS = $ '\ n'
$(basename "${i/.flac}").mp3为"$(basename "${i/.flac}").mp3"(用引号引起来)解决了空间问题
一个简单的1班轮解决方案:
find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 128k {}.mp3 \;
http://lewisdiamond.blogspot.ca/2012/01/converting-flac-to-mp3.html
注意,这将在给定目录中递归应用。即,如果您从“音乐”文件夹运行此文件,它将转换子文件夹中的所有标记,并在其旁边生成一个.mp3。您也可以通过直接使用flac和lame(即读取w / flac,将管道传递给lame,输出到文件.mp3)来实现,而无需ffmpeg,如链接所示。
find -name "*.flac" -exec bash -c 'ffmpeg -i "{}" -y -acodec libmp3lame -ab 128k "${0/.flac}.mp3"' {} \;
我把在这里(也许在其他一些网站上)找到的所有东西都用了,创建了一个小工具,不仅可以递归创建flac的mp3,还可以保留相对路径以在多线程支持下在其他地方创建它们。
哦,是的,我知道,在那种情况下,我没有使用ffmpeg,因为我的OSMC没有提供ffmpeg的软件包,只有avconv,但由于您已经在这里,我想您知道,这基本上是同样-至少对于最重要的部分。只需将命令“ avconv”替换为“ ffmpeg”即可。我的第一次运行是使用ffmpeg bin和完全相同的选项。
我绝不是bash黑客,但我做到了,因为我的第一个bashscript具有给定的要求,也许有人会受益。我愿意接受您的任何建议,但到目前为止它对我有用。
我的脚本启动了四个实例,每个核心一个,如下所示:
#!/bin/bash
# this should be quite self-explanatory
for i in {1..4}
do
echo "started instance no: $i"
/home/osmc/transform.sh . &
# sleeping time can be shorter, this is just so, that
# not all 4 processes will want to start with the same
# file, during runtime collisions should not become an issue
sleep 5
done
echo "all instances started"
像这样的工作脚本:
#!/bin/bash
# take care of spaces
IFS=$'\n'
# my music folders, remote is the source, local the target dir
remote=/mnt/music/FLAC
local=/mnt/1tb/mp3
# for all flac files start loop
for i in $(find $remote -type f -iname '*.flac' );
do
## SET VARIABLES for PATHS and FILENAMES
## this might be able to be super short with sed and complex one-liner,
## but I s*ck at regex
fullfile=$i
# strip extension
filename="${i##*/}"
# add new extension
filename="${filename%.*}.mp3"
# get full dirname from inputfile
fulldir=$(dirname "${i}")
# strip leading dirs from full input dir
# count the dirs, add two, then you're good.
reldir="$(echo $fulldir | cut -d'/' -f5-)"
# some subdirs in my collection even have a flac subdir, you might
# ignore this, it strips only if it exists
reldir=${reldir//flac}
# combine target dir and relative dir
outdir="$local/$reldir"
# generate the full output filename for conversion
outfile="$outdir/$filename"
# create whole target directory - yes, I need it only once, but hey,
# it works, didn't want to start a if not exist statement... should I?
mkdir -p "$outdir"
# run conversion - finally... you may want or need to replace
# "avconv" with "ffmpeg"
avconv -n -nostats -loglevel info -i "$fullfile" -codec:a libmp3lame -qscale:a 0 "$outfile"
done
可以在https://github.com/erdnuesse/flac-to-mp3上找到
问候,凯
GNU Parallel用于以下任务:
# change to the home directory
cd ~/music
# convert all *.flac files
parallel ffmpeg -i {} -acodec libmp3lame {.}.mp3 ::: *.flac
# (optional: check whether there are any errors printed on the terminal)
sleep 60
它将并行运行作业(每个CPU内核一个作业),并确保终端上的输出没有混合在一起。
要了解更多信息,请花15分钟阅读第1 + 2章:https : //zenodo.org/record/1146014
{.}.mp3 :::结构的作用吗?还是参考解释它的手册部分?
并行FTW(毫无疑问,您有多个核心-为什么不使用它们?):
ls *flac | while read f; do ffmpeg -i "$f" -acodec libmp3lame $f.mp3 & done
如果它可以帮助...。我已经写了一个小的bash脚本来执行此操作。...您需要安装ffmpeg / flac。
怎么运行的:
它需要2个参数:
它产生:
#!/bin/bash
FLAC_PATH=$1
CONV_PATH=$2
DEBUG=0;
function usage {
echo "";
echo " This script convert all flac files from a folder to mp3 files to a second folder";
echo "";
echo " Usage :";
echo " ./conv.sh {Source Folder} {Destination Folder}";
echo " note : booth folder must exist before starting this script";
echo " files other than flac are copied to the destination folder";
echo "";
}
if [ ! -d "$2" ]; then
echo "";
echo " ERROR : [$2] is not a directory.";
usage
exit 1
fi;
if [ ! -d "$2" ]; then
echo "";
echo " ERROR : [$2] is not a directory.";
usage
exit 1
fi;
COMMANDS="run.sh"
echo "" > run.sh
echo " convert from $FLAC_PATH to $CONV_PATH ";
find "${FLAC_PATH}" -type f |while read myFile; do
SRC_DIR=${myFile%/*}
SRC_FILE=${myFile##*/}
DST_DIR=$CONV_PATH/$SRC_DIR
mkdir -p "${DST_DIR}"
# TEST if the file is a flac ....
metaflac --show-md5sum "${myFile}" 2>/dev/null 1>/dev/null
if [ $? -eq 0 ]; then
echo -n " *** $myFile [FLAC !] : "
DST_FILE=${myFile%.*}
OUT_PATH="${DST_DIR}/$( echo $SRC_FILE | sed -e 's/.flac$/.mp3/')"
if [ $DEBUG == 1 ]; then
echo " SRC = $myFile";
echo " OUT = $OUT_PATH"
fi;
if [ -f "$OUT_PATH" ]; then
echo " exist, do nothing !";
else
echo " add to compress list !";
echo "ffmpeg -y -i \"${myFile}\" -codec:a libmp3lame -q:a 0 -map_metadata 0 -id3v2_version 3 \"${OUT_PATH}\" " >> $COMMANDS
fi;
else
echo -n " *** $SRC_FILE [NOT FLAC] : "
if [ -f "${CONV_PATH}/${myFile}" ]; then
echo " exist, do nothing !"
else
echo " copy."
cp "${myFile}" "${CONV_PATH}/${myFile}"
fi
fi
done;
echo " And now, CONVERT THE FLAC's!!! "
sh run.sh
要在mp3中递归转换嵌套文件夹中的所有flac或wav文件,我使用了以下命令:
find '~/Music/' -iname '*.flac' , -iname '*.wav' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/mp3/"; ffmpeg -i "{}" -ab 320k -map_metadata 0 -id3v2_version 3 -acodec libmp3lame "$D/mp3/${B%.*}.mp3"' \;
它将在其中包含flac或wav文件的文件夹中创建一个名为“ mp3”的文件夹,并且在mp3文件夹中,它将以320kbps的比特率保存相对的mp3文件,而无需在名称中保留旧文件扩展名。
文件名是“ wav2mp3”
#!/bin/sh
# put the script to /usr/local/sbin
EXT_IN=$1
QUALITY=$2
if [ "${EXT_IN}" = "" -o "${QUALITY}" = "" ]; then
printf "Usage: wav2mp3 \<in_file_ext\> \<quality\> \ne.g. wav2mp3 wav 2\n"
exit 1
fi
#String delimeter
SAVEIFS=${IFS}
IFS='
'
# List of the files in directory lower than current to array
FILES_LIST=`find . -type f -name "*.${EXT_IN}"`
for FILE in ${FILES_LIST}; do
echo $FILE
PREFIX=`echo $FILE | awk -F \. 'OFS="."{ $NF="" }1' | sed 's/.$//'`
echo $PREFIX
ffmpeg -i $FILE -codec:a libmp3lame -qscale:a $QUALITY $PREFIX.mp3
done
我意识到这来得很晚,但是为了记忆起见,请参见sourceforge上的脚本“ batchaudiocvt”。它是一个(很大的)shell脚本,旨在在多种格式之间高效地批量转换音频文件。特别是,它会尽力转换常用标签。