bash ffmpeg在文件名中查找和空格

时间:2017-10-27 01:18:29

标签: bash ffmpeg

我的ffmpeg和文件夹/文件名中的空格有问题。我的代码如下所示:

cd /volume1/PUBLIC/Musik/AutoConvert
find . -type d -exec mkdir -p -- /volume1/PUBLIC/Musik/Converted/{} \;
find -type f |sed 's|./||' |while read NEWNAME; do
ffmpeg -i ""/volume1/PUBLIC/Musik/AutoConvert/"$NEWNAME""" -codec:a libmp3lame -qscale:a 2 "/volume1/PUBLIC/Musik/Converted/"${NEWNAME%.[Ff][Ll][Aa][Cc]}".mp3"
echo done with file: "$NEWNAME"
done

但是如果在任何地方都有空格,则ffmpeg会失败。我的文件夹结构如下所示:

user@NAS:/volume1/PUBLIC/Musik/AutoConvert$ find -type f |sed 's|./||'
A B/C.mp3
A B/C D.mp3
A/C D.mp3
A/C.mp3

/Converted/A/C.mp3确实有效,但这是唯一的。所有其他文件夹中的空格或文件名中的空格都失败。

[NULL @ 0x20bb3e0] Unable to find a suitable output format for '/volume1/PUBLIC/Musik/Converted/A/C'
/volume1/PUBLIC/Musik/Converted/A/C: Invalid argument
done with file: A/C D.mp3

[NULL @ 0x11bf280] Unable to find a suitable output format for '/volume1/PUBLIC/Musik/Converted/A'
/volume1/PUBLIC/Musik/Converted/A: Invalid argument
done with file: A B/C D.mp3

[NULL @ 0x11f93e0] Unable to find a suitable output format for '/volume1/PUBLIC/Musik/Converted/A'
/volume1/PUBLIC/Musik/Converted/A: Invalid argument
done with file: A B/C.mp3

如果我不使用双“on ffmpeg -input我得到错误:

/volume1/PUBLIC/Musik/AutoConvert/A: Is a directory
done with file: A B/C.mp3

/volume1/PUBLIC/Musik/AutoConvert/A: Is a directory
done with file: A B/C D.mp3

/volume1/PUBLIC/Musik/AutoConvert/A/C: No such file or directory
done with file: A/C D.mp3

看起来我走在正确的轨道上...离我的目标太远了。

有人能提供一些帮助吗?

1 个答案:

答案 0 :(得分:1)

也许这段代码可以帮到你

#!/bin/bash
cd /volume1/PUBLIC/Musik/AutoConvert 2>/dev/null || { echo "The folder is unavailable."; exit 1; }
find . -type d -exec mkdir -p -- /volume1/PUBLIC/Musik/Converted/{} \;
while IFS= read -r -d $'\0' NEWNAME; do
    ffmpeg -i "/volume1/PUBLIC/Musik/AutoConvert/$NEWNAME" -codec:a libmp3lame -qscale:a 2 "/volume1/PUBLIC/Musik/Converted/${NEWNAME%.*}.mp3"
    echo "done with file: $NEWNAME"
done < <(find . -iname '*.flac' -printf '%P\0')