我有以下脚本(名为vid2gif.sh)将视频文件转换为GIF:
#! /斌/庆典ffmpeg的-i $ 1 /tmp/gif/out%04d.gifgifsicle --delay = 10 --loop /tmp/gif/*.gif> $ 2我可以使用命令转换文件:
vid2gif.sh myvid.mp4 myvid.gif我怎样才能让所有的MP4文件转换的文件夹中?也就是说,如何才能让下面的命令工作:
vid2gif.sh * .MP4脚本应该输出文件如* .gif注意。谢谢你的帮助。
解决方案 #!/ bin / sh的对于f;做 TEMPDIR = $(mktemp的-t -d gifdir.XXXXXX) ffmpeg的-i$ F$ TEMPDIR /输出%04d.gif gifsicle --delay = 10 --loop$ TEMPDIR/ * GIF方式>$ {。F%*} GIF 室射频$ TEMPDIRDONE让我们重温这是如何工作的:
迭代
为F;做等同于在$ @ F;也就是说,它遍历所有命令行参数。相反,如果你在当前目录要遍历所有的mp4文件,这将是为* .MP4 F;做,或循环遍历作为第一个命令行参数传递的目录中名为所有mp4文件,这将是在$ 1/ * MP4楼;做。为支持使用 - 但与第一个去,如果没有目录传递 - 这将是对于f在$ {1: - 。}/ * MP4;做。
临时目录,使用
由于原来的脚本将重用的/ tmp / GIF 的一切,你会得到一个输入源文件被他人使用。这是最好的每个输入文件,其中 mktemp的将自动
创建 .gif注意:名称
$ {F%。*}是一个参数扩展从而消除一切后,最后的。中一份文件;看到 BashFAQ#100 对字符串处理的文件在bash一般包括这种特殊形式。
因此,$ {F%。*}。GIF剥离现有的延伸,并增加了一个 .gif注意:扩展。
I have following script (named vid2gif.sh) to convert a video file to gif:
#! /bin/bash ffmpeg -i $1 /tmp/gif/out%04d.gif gifsicle --delay=10 --loop /tmp/gif/*.gif > $2I can convert a file using command:
vid2gif.sh myvid.mp4 myvid.gifHow can I make it to convert all mp4 files in a folder? That is, how can I make following command work:
vid2gif.sh *.mp4The script should output files as *.gif. Thanks for your help.
解决方案 #!/bin/sh for f; do tempdir=$(mktemp -t -d gifdir.XXXXXX) ffmpeg -i "$f" "$tempdir/out%04d.gif" gifsicle --delay=10 --loop "$tempdir"/*.gif >"${f%.*}.gif" rm -rf "$tempdir" doneLet's go over how this works:
Iteration
for f; dois equivalent to for f in "$@"; that is to say, it loops over all command-line arguments. If instead you wanted to loop over all MP4s in the current directory, this would be for f in *.mp4; do, or to loop over all MP4s named in the directory passed as the first command line argument, it would be for f in "$1"/*.mp4; do. To support either usage -- but go with the first one if no directory is passed -- it would be for f in "${1:-.}"/*.mp4; do.
Temporary directory use
Because the original script would reuse /tmp/gif for everything, you'd get files from one input source being used in others. This is best avoided by creating a new temporary directory for each input file, which mktemp will automate.
Creating the .gif name
"${f%.*}" is a parameter expansion which removes everything after the last . in a file; see BashFAQ #100 for documentation on string manipulation in bash in general, including this particular form.
Thus, "${f%.*}.gif" strips the existing extension, and adds a .gif extension.
更多推荐
扩展脚本来遍历多个文件,并生成输出名称
发布评论