使用find -exec时如何使用原始名称?

时间:2015-10-28 00:01:41

标签: ffmpeg find

我想要转换.mp4个文件。

$ find . -name '*.mp4'
./01.mp4
./02.mp4
./03.mp4

我希望使用此命令输出文件copy-01.mp4copy-02.mp4copy-03.mp4

find . -name '*.mp4' -exec ffmpeg -i {} -c copy -aspect 16:9 copy-{} ";"

但它失败了,错误为Unable to find a suitable output format for 'copy-'。 我认为{}代表文件名,不是吗?

使用-exec的{​​{1}}选项时,如何使用原始文件名?

find版本是:

find

这是$ find --version find (GNU findutils) 4.4.2 Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Eric B. Decker, James Youngman, and Kevin Dalley. Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b Features enabled: O_NOFOLLOW(disabled) LEAF_OPTIMISATION FTS() CBO(level=0) 版本。

xargs

ffmpeg的结果

xargs --version
xargs (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b

3 个答案:

答案 0 :(得分:3)

正确的方法是制作一个bash脚本。问题是当前目录前缀./

如果你真的必须这样做:

find * -type f -name '*.mp4' -exec ffmpeg -i {} -c copy -aspect 16:9 copy-{} ";"

或者如果你有xargs

find . -name '*.mp4' | xargs -L1 basename | xargs -L1 -I{} ffmpeg -i {} -c copy -aspect 16:9 copy-{}

答案 1 :(得分:1)

要使ffmpeg访问文件,即使它们是当前目录后面的几个目录级别,您需要将整个源路径作为-i参数传递,但只需将basename作为目标文件名构建块传递。

如果您希望将它们复制到当前目录(展平),

find . -type f -name '*.mp4' -exec ffmpeg -i {} -c copy -aspect 16:9 copy-"`basename {}`" ";"

如果它们应与源相同的目录:

find . -type f -name '*.mp4' -exec ffmpeg -i {} -c copy -aspect 16:9 "`dirname {}`"/copy-"`basename {}`" ";"

答案 2 :(得分:1)

这不是一个单行班的工作。试试一个剧本:

#!/bin/bash
while IFS= read -r -d '' FILE
do
    BASE=$(basename "$FILE")
    COPYFILE=${FILE/$BASE/copy-$BASE}
    ffmpeg -i "$FILE" -c copy -aspect 16:9 "$COPYFILE"
done < <(find . -name '*.mp4' -print0)

(当然,如果你真的想要一些分号,你也可以把它放在一行!)

相关问题