在bash脚本中使用ffmpeg缩放图像

在bash脚本中使用ffmpeg缩放图像,bash,shell,ffmpeg,Bash,Shell,Ffmpeg,我正在使用bash脚本作为一种为文档制作gif的快速方法。它在ffmpeg和ImageMagick上运行,我试图找到一种方法来添加一个变量来缩放生成的GIF,这样我就不必返回并再次添加它。我以为我正确地添加了d(resize)变量,但脚本失败,只打印帮助内容。它不会在帮助读数中显示我添加的变量。有什么想法吗 更新 我解决了打印帮助内容而不是运行脚本的问题,但是现在我收到了一个关于-scale参数的错误 convert: invalid argument for option `-scale':

我正在使用bash脚本作为一种为文档制作gif的快速方法。它在
ffmpeg
ImageMagick
上运行,我试图找到一种方法来添加一个变量来缩放生成的GIF,这样我就不必返回并再次添加它。我以为我正确地添加了
d
(resize)变量,但脚本失败,只打印帮助内容。它不会在帮助读数中显示我添加的变量。有什么想法吗

更新 我解决了打印帮助内容而不是运行脚本的问题,但是现在我收到了一个关于
-scale
参数的错误

convert: invalid argument for option `-scale': -vf @ error/convert.c/ConvertImageCommand/2513.
这是因为下面的scale参数使用了我的
if
语句语法吗

#!/bin/bash

function printHelpAndExit {
  echo 'Usage:'
  echo '  gifify -conx filename'
  echo ''
  echo 'Options: (all optional)'
  echo '  c CROP:   The x and y crops, from the top left of the image, i.e. 640:480'
  echo '  o OUTPUT: The basename of the file to be output (default "output")'
  echo '  n:        Do not upload the resulting image to CloudApp'
  echo '  r FPS:    Output at this (frame)rate (default 10)'
  echo '  s SPEED:  Output using this speed modifier (default 1)'
  echo '            NOTE: GIFs max out at 100fps depending on platform. For consistency,'
  echo '            ensure that FPSxSPEED is not > ~60!'
  echo '  x:        Remove the original file and resulting .gif once the script is complete'
  echo '  d SCALE:  Scales GIF image to specified dimensions (default no scale)'
  echo ''
  echo 'Example:'
  echo '  gifify -c 240:80 -o my-gif -x my-movie.mov'
  exit $1
}

noupload=0
fps=10
speed=1

OPTERR=0

while getopts "c:o:r:s:d:nx" opt; do
  case $opt in
    c) crop=$OPTARG;;
    h) printHelpAndExit 0;;
    o) output=$OPTARG;;
    n) noupload=1;;
    r) fps=$OPTARG;;
    s) speed=$OPTARG;;
    x) cleanup=1;;
    d) scale=$OPTARG;;
    *) printHelpAndExit 1;;
  esac
done

shift $(( OPTIND - 1 ))

filename=$1

if [ -z ${output} ]; then
  output=$filename
fi

if [ -z $filename ]; then printHelpAndExit 1; fi

if [ $crop ]; then
  crop="-vf crop=${crop}:0:0"
else
  crop=
fi

if [ $scale ]; then
  scale="-vf scale=${scale}:0:0"
else
  scale=
fi

# -delay uses time per tick (a tick defaults to 1/100 of a second)
# so 60fps == -delay 1.666666 which is rounded to 2 because convert
# apparently stores this as an integer. To animate faster than 60fps,
# you must drop frames, meaning you must specify a lower -r. This is
# due to the GIF format as well as GIF renderers that cap frame delays
# < 3 to 3 or sometimes 10. Source:
# http://humpy77.deviantart.com/journal/Frame-Delay-Times-for-Animated-GIFs-214150546
echo 'Exporting movie...'
delay=$(bc -l <<< "100/$fps/$speed")
temp=$(mktemp /tmp/tempfile.XXXXXXXXX)

ffmpeg -loglevel panic -i $filename $crop -r $fps -f image2pipe -vcodec ppm - >> $temp

echo 'Making gif...'
cat $temp | convert +dither -layers Optimize -delay $delay -scale $scale - ${output}.gif

if [ $noupload -ne 1 ]; then
  open -a Cloud ${output}.gif

  echo `pbpaste`

  if [ $cleanup ]; then
    rm $filename
    rm ${output}.gif
  fi
else
  echo ${output}.gif
fi
#/bin/bash
函数printHelpAndExit{
echo“用法:”
echo“gifify-conx文件名”
回声“
echo“选项:(所有可选)”
echo“c裁剪:图像左上角的x和y裁剪,即640:480”
echo'o OUTPUT:要输出的文件的基本名称(默认为“OUTPUT”)'
echo'n:不将结果图像上载到CloudApp'
echo'r FPS:以此(帧)速率输出(默认为10)'
echo的速度:使用此速度修改器输出(默认值1)'
echo“注意:GIF最大输出速度为100fps,具体取决于平台。为保持一致性,”
echo“确保FPSxSPEED不大于~60!”
echo“x:脚本完成后删除原始文件和生成的.gif”
echo“d比例:将GIF图像缩放到指定的尺寸(默认无比例)”
回声“
echo'示例:'
echo'gifif-c240:80-o我的gif-x我的电影.mov'
退出$1
}
努普洛德=0
fps=10
速度=1
OPTERR=0
而getopts“c:o:r:s:d:nx”opt;做
案例$opt-in
c) 作物=$OPTARG;;
h) printHelpAndExit 0;;
o) 输出=$OPTARG;;
n) 努普洛德=1;;
r) fps=$OPTARG;;
s) 速度=$OPTARG;;
x) 清理=1;;
d) 比例=$OPTARG;;
*)printHelpAndExit 1;;
以撒
完成
班次$((可选项-1))
文件名=$1
if[-z${output}];然后
输出=$filename
fi
如果[-z$filename];然后打印helpandexit 1;fi
如果[$作物];然后
crop=“-vf crop=${crop}:0:0”
其他的
收成=
fi
如果[$scale];然后
scale=“-vf scale=${scale}:0:0”
其他的
鳞片=
fi
#-延迟使用每个刻度的时间(刻度默认为1/100秒)
#所以60fps==-延迟1.666666,因为转换,所以四舍五入为2
#显然,它是以整数形式存储的。要使动画速度超过60帧/秒,
#必须删除帧,这意味着您必须指定较低的-r。这是
#由于GIF格式以及限制帧延迟的GIF渲染器
#<3到3或有时10。资料来源:
# http://humpy77.deviantart.com/journal/Frame-Delay-Times-for-Animated-GIFs-214150546
echo“导出电影…”
延迟=$(bc-l$温度
echo“制作gif…”
cat$temp | convert+抖动-层优化-延迟$delay-缩放$scale-${output}.gif
如果[$noupload-ne 1];则
打开-一个云${output}.gif
回音粘贴`
如果[$cleanup];那么
rm$filename
rm${output}.gif
fi
其他的
echo${output}.gif
fi

您正在使用
$scale=“-vf scale…”
调用
convert
,这实际上是一个ffmpeg视频过滤器定义

或者(使用
-vf标度


或者使用。

实际的解析错误消息是什么?@konsolebox我发现,
r
变量已经赋值,因此添加第二个变量会导致脚本失败。问题已在上面更新。听起来您运行的是原始文件,而不是更新的副本。如果编辑原始文件,我不需要重新映射但是,对吗?@BrianBennett我在让脚本解析
-d
选项方面没有问题。我的猜测是,如果这不是实际的命令语法问题,那么您可能正在用一个不是bash的shell运行脚本。不如
bash yourscript.sh…
?错误实际上是在
ffmpeg
调用中,而不是在t中他可能会说,但你的回答帮我找到了。谢谢。