我正在从一个脚本运行一个 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/