xargs:引号未终止

4
我正在尝试将一些.flac文件转换为.mp3格式,以便可以导入到iTunes中。我尝试使用find、xargs和ffmpeg,但是xargs会给我一个未终止引号的错误(因为我的文件名中有引号)。
这是我的命令行:
MacKassner:Geto Boys kassner$ find . -type f | egrep "\.flac$" | xargs -I {} ffmpeg -i {} -ab 192k -acodec libmp3lame -ac 2 {}.mp3

在文件名为"Talkin' Loud Ain't Saying Nothin'.flac"的地方停止并引发错误。

有一些技巧可以让这个工作吗?

-- 仅使用find解决 --
find . -type f -name "*.flac" -exec ffmpeg -i {} -ab 192k -acodec libmp3lame -ac 2 {}.mp3 \;

3个回答

5

使用GNU Parallel。它是专门为此目的构建的:

MacKassner:Geto Boys kassner$ find . -type f | egrep "\.flac$" | parallel ffmpeg -i {} -ab 192k -acodec libmp3lame -ac 2 {}.mp3

您可能还想使用{.}.mp3来替换.flac格式:

MacKassner:Geto Boys kassner$ find . -type f | egrep "\.flac$" | parallel ffmpeg -i {} -ab 192k -acodec libmp3lame -ac 2 {.}.mp3

观看介绍视频以了解更多: http://www.youtube.com/watch?v=OpaiGYxkSuQ


1
现在不确定2011年是否可以这样做:现在可以从Homebrew获取并行处理:homebrew install parallel。 - Anthony Pulido

2
有些版本的xargs支持自定义分隔符。如果是这种情况,只需添加-d'\n'即可指示使用换行符来分隔项目(通常是有意义的)。在这种情况下,您可以按照以下方式使用它:
# find files_containing_quotes/ | xargs -d'\n' -i{} echo "got item '{}'"

1
很遗憾,OSX上的xargs不支持它。无论如何,谢谢你的提示,我不知道-d参数。 - Rafael Kassner

1

来自egrep手册:

-Z, --null
          Output  a  zero  byte  (the  ASCII NUL character) instead of the character that normally follows a file
          name.  For example, grep -lZ outputs a zero byte after each file name instead  of  the  usual  newline.
          This  option  makes  the  output  unambiguous,  even  in  the presence of file names containing unusual
          characters like newlines.  This option can be used with commands like find -print0, perl -0,  sort  -z,
          and xargs -0 to process arbitrary file names, even those that contain newline characters.

所以在 egrep 中使用 -Z,而在 xargs 中使用 -0


使用带有-Z选项的egrep效果很好,但是使用带有-I和-0选项的xargs有问题。MacKassner:Geto Boys kassner$ find . | egrep -Z ".flac$" | xargs -0 -I {} echo {}这会一次打印出{}。 - Rafael Kassner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接