aboutsummaryrefslogtreecommitdiff
path: root/scripts/.scripts/2gif.sh
blob: ec325dcd7cb98301199a399f8633960e9fc7c8a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh

# source: https://github.com/Drakirus/dotfiles/blob/master/bin/2gif

$(ffmpeg --help 2> /dev/null > /dev/null)

if [[ $? -ne 0 ]]; then
  echo "ffmpeg is required!"
  exit 1
fi

# default values
FPS=12

# read the options
TEMP=`getopt -o c:r:f:t:s:d:hl:: --long resolution:,fps:,start:,to:,duration:,help,loop:: -n $0 -- "$@"`
eval set -- "$TEMP"

# extract options and their arguments into variables.
while true ; do
  case "$1" in
    -h|--help)
      echo -e "Usage: $0 [options] <input video file> <output gif name>\n"
      echo "Options:"
      echo "  -h, --help            Show this help"
      echo "  -r, --resolution      Set the pixels wide (preserving the aspect ratio)"
      echo "  -f, --fps             Set the gif frame rate"
      echo "                          default 12"
      echo "  -s, --start           Skip the first x seconds"
      echo "  -t, --to              Capture to x seconds"
      echo "  -d, --duration        Set the duration"
      echo "  -l, --loop            Number of times to loop the output"
      echo "                          default: no loop"
      echo "                          -l      output loop infinitely"
      echo "                          -l10    output loop 10 times"
      echo "Examples:"
      echo "  $0 input.mp4 out.gif"
      echo "  $0 input.mp4 out.gif -s"00:01" -d5 -r450"
      echo "  $0 input.mp4 out.gif -s"20:00" -d5 -l --fps30"
      exit;;
    -r|--resolution)
      case "$2" in
        "") shift 2 ;;
        *) RESOLUTION="$2"; shift 2 ;;
      esac ;;
    -t|--to)
      case "$2" in
        "") shift 2 ;;
        *) TO="-t $2"; shift 2 ;;
      esac ;;
    -l|--loop)
      case "$2" in
        "") LOOP="-loop=0" ; shift 2 ;;
        *) LOOP="-loop=$2" ; shift 2 ;;
      esac ;;
    -f|--fps)
      case "$2" in
        "") shift 2 ;;
        *) FPS="$2"; shift 2 ;;
      esac ;;
    -s|--start)
      case "$2" in
        "") shift 2 ;;
        *) START="-ss $2"; shift 2 ;;
      esac ;;
    -d|--duration)
      case "$2" in
        "") shift 2 ;;
        *) DURATION="-t $2"; shift 2 ;;
      esac ;;
    --) shift ; break ;;
    *) echo "Internal error!" ; exit 1 ;;
  esac
done

if [[ "$#" -ne 2 && "$#" -ne 1  ]]; then
  echo -e "Usage: $0 [options] <input video file> <output gif name>\n"
  echo -e "Illegal number of parameters or parameters.\nFor more informations:\n $0 --help "
  exit 1
fi

OUTPUT="$2"

if [[ "$#" -eq 1  ]]; then
  filename=$(basename "$1")
  filename="${filename%.*}.gif"
  OUTPUT=$filename
fi

if [[ "$RESOLUTION" -eq ""  ]]; then
  eval $(ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=width "$1")
  RESOLUTION=${streams_stream_0_width}
fi

palette=$(mktemp /tmp/palette_XXXX.png)

filters="fps=$FPS,scale=$RESOLUTION:-1:flags=lanczos"

# echo $filters

# http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html#usage

# create global color palette
ffmpeg -v warning $START $TO $DURATION -i "$1" -vf "$filters,palettegen" -y "$palette"

# create GIF
ffmpeg -v warning $START $TO $DURATION -i "$1" -i "$palette" -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$OUTPUT"

rm $palette