bash - shell - 带有多个变量的循环命令

标签 bash shell ffmpeg

我正在从一个脚本运行一个 FFmpeg 命令,该脚本需要几个参数并将它们传递给命令。

#!/bin/bash

while getopts "i:b:" flag
do
  case "$flag" in
    i) input="$OPTARG";;
    b) IFS=, read -a bitrate <<< "$OPTARG";;
  esac
done

for rate in "${bitrate[@]}";
  do
    ffmpeg -i $input -video_size 100x100 -b:v $bitrate -y output.mp4
  done

exit

我用 getopts取变量和for循环运行命令。
要运行脚本,我使用 script.sh -i input.mov -b 1000,2000,3000 FFmpeg 运行 3 次,每次都有不同的 $bitrate值(value)。

如果我想将另一个 var - scale 传递给命令并像这样运行它怎么办:
script.sh -b 1000,2000,3000 -s 100x100,200x200,300x300所以第一次运行将使用 -b 1000 和 -s 100x100,第二次运行使用 -b 2000 和 -s 200x200 等等。
这很可能吗?如果可能的话,我想继续使用 getopts。

最佳答案

假设两个数组具有相同的索引:

for i in "${!bitrate[@]}"; do   # i iterates over the indices of bitrate
    ffmpeg -i "$input" -video_size "${scale[i]}" -b:v "${bitrate[i]}" -y output.mp4
done

关于bash - shell - 带有多个变量的循环命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742252/

相关文章:

shell - 在 Jenkins 中构建后执行 Shell 脚本

c++ - FFMPEG 帧到 DirectX 表面

java - Linux shell 脚本执行挂起

php - shell命令错误语法错误: "done" unexpected (expecting "do")

linux - shell 脚本 telnet 在第一点跳过 while 循环

c++ - system() 调用使用不当?

c# - 处理来自标准输出的 jpgs 流

linux - 为什么 fork() 不立即给出正确的 pid?

bash:对于文件是否存在?

linux - 如何将shell变量传递给shell脚本中的awk命令