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

时间:2014-06-17 17:44:30

标签: bash shell ffmpeg

我正在使用jclem's Gifify bash脚本作为制作文档GIF的快捷方式。它运行在ffmpegImageMagick上,我试图找到一种方法来添加变量以缩放生成的GIF,因此我不必再返回并重新添加它。 我以为我正确添加了d(resize)变量,但是脚本失败了,只打印了帮助内容。它没有在帮助读数中显示我添加的变量。有什么想法吗?

更新

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

convert: invalid argument for option `-scale': -vf @ error/convert.c/ConvertImageCommand/2513.

这是因为我的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

1 个答案:

答案 0 :(得分:2)

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

scale in ffmpeg-vf scale)。

或使用convert to scale