扩展脚本来遍历多个文件,并生成输出名称

编程入门 行业动态 更新时间:2024-10-24 22:18:04
本文介绍了扩展脚本来遍历多个文件,并生成输出名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下脚本(名为vid2gif.sh)将视频文件转换为GIF:

#! /斌/庆典ffmpeg的-i $ 1 /tmp/gif/out%04d.gifgifsicle --delay = 10 --loop /tmp/gif/*.gif> $ 2

我可以使用命令转换文件:

vid2gif.sh myvid.mp4 myvid.gif

我怎样才能让所有的M​​P4文件转换的文件夹中?也就是说,如何才能让下面的命令工作:

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 > $2

    I can convert a file using command:

    vid2gif.sh myvid.mp4 myvid.gif

    How can I make it to convert all mp4 files in a folder? That is, how can I make following command work:

    vid2gif.sh *.mp4

    The 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" done

    Let's go over how this works:

  • Iteration

    for f; do

    is 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.

  • 更多推荐

    扩展脚本来遍历多个文件,并生成输出名称

    本文发布于:2023-11-16 21:12:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1607476.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   遍历   脚本   名称   文件

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!