File: mapimg2anim

package info (click to toggle)
freeciv 3.2.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 286,492 kB
  • sloc: ansic: 484,452; cpp: 37,766; sh: 10,374; makefile: 7,425; python: 2,938; xml: 652; sed: 11
file content (196 lines) | stat: -rwxr-xr-x 5,110 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
#/***********************************************************************
# Freeciv - Copyright (C) 2010-2025
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#***********************************************************************/

# Convert map images to animations
#
# Requirements:
# - convert (from ImageMagick)
# - ffmpeg (for avi and flv)

# Default values
animation="gif"
civgame="freeciv"
mapstr=""
mapext="gif"
loop=1

scriptname=$0
basename=`basename ${scriptname}`

# Check for required tools (convert)
have_convert=TRUE
prog_convert=`command -v convert`
RES=$?
if [ $RES -ne 0 ]; then
  # Without this tool nothing can be done
  echo "ERROR: The required program 'convert' from ImageMagick is missing."
  exit 1
fi

# Check for required tools (ffmpeg)
have_ffmpeg=TRUE
command -v ffmpeg > /dev/null
RES=$?
if [ $RES -ne 0 ]; then
  have_ffmpeg=FALSE
fi

# Print usage
function usage()
{
  echo ""
  echo "USAGE:"
  echo "    ${basename} -h [-c <civgame>] [-l <nr>] -m <mapstr> [-e <mapext>] [-a <gif|mpg]|avi|flv|ALL>]"
  echo ""
  echo "OPTIONS:"
  echo "    -a  animation format (default: gif)"
  echo "    -c  basename for the save files (default: freeciv)"
  echo "    -e  map image extension (default: gif)"
  echo "    -h  this help"
  echo "    -l  number of loops (only for the gif file; default: 1)"
  echo "        (FIXME: has no effect?)"
  echo "    -m  map image definition string"
  echo ""
  echo "EXAMPLE:"
  echo "    ${basename} -m M-bcfktuZ2P000each -e png -a mpg"
  echo ""
}

# ffmpeg is needed
function check_ffmpeg()
{
  if [ "$have_ffmpeg" == "FALSE" ]; then
    # ffmpeg is missing
    echo "ERROR: The required program 'ffmpeg' is missing."
    exit 1
  fi
}

# Process arguments
while getopts "a:c:e:hl:m:" option
do
  case $option in
    h )
      usage
      exit 0
      ;;
    c )
      civgame=$OPTARG
      ;;
    m )
      mapstr=$OPTARG
      ;;
    e )
      mapext=$OPTARG
      ;;
    a )
      animation=$OPTARG
      ;;
    l )
      loop=$OPTARG
      ;;
    * )
      echo ""
      echo "Unimplemented option chosen (${option})."
      usage
      exit 1
      ;;
  esac
done

# File definitions
FILES="${civgame}-T*-Y*-${mapstr}.map.${mapext}"

# Check for files
CHECK=`ls ${FILES} 2> /dev/null | wc -l`
if [ $CHECK -eq 0 ]; then
  echo "ERROR: no map images found ($FILES)"
  usage
  exit 1
fi

# Get first and last image
FILE_A=`ls $FILES | head -n 1`
FILE_Z=`ls $FILES | tail -n 1`

# Output file names
ANIM_GIF="${civgame}-${mapstr}.anim.gif"
ANIM_AVI="${civgame}-${mapstr}.anim.avi"
ANIM_FLV="${civgame}-${mapstr}.anim.flv"
ANIM_MPG="${civgame}-${mapstr}.anim.mpg"

# Main part
case $animation in
  ALL )
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a gif
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a mpg
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a avi_from_mpg
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a flv_from_mpg
    ;;
  gif )
    echo -e "generating animated gif (${ANIM_GIF}) ..."
    [ -e ${ANIM_GIF} ] && rm ${ANIM_GIF}
    convert -delay 100 -loop ${loop} \
            ${FILES} ${FILE_Z} \
            ${ANIM_GIF}
    ;;
  mpg )
    echo -e "generating mpg file ($ANIM_MPG) ..."
    [ -e ${ANIM_MPG} ] && rm ${ANIM_MPG}
    convert -delay 5 -quality 100 \
            ${FILES} ${FILE_Z} \
            ${ANIM_MPG}
    ;;
  avi )
    echo -e "generating avi file ($ANIM_AVI) ..."
    check_ffmpeg
    # Need to first generate the mpg file
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a mpg
    [ -e ${ANIM_AVI} ] && rm ${ANIM_AVI}
    ffmpeg -i ${ANIM_MPG} -f avi ${ANIM_AVI}
    echo $animation
    ;;
  avi_from_mpg )
    # This is an internal tag; it assumes that the mpg file exists
    echo -e "generating avi file ($ANIM_AVI) ..."
    check_ffmpeg
    [ ! -e ${ANIM_MPG} ] && echo "ERROR: ${ANIM_MPG} missing ..." && exit 1
    [ -e ${ANIM_AVI} ] && rm ${ANIM_AVI}
    ffmpeg -i ${ANIM_MPG} -f avi ${ANIM_AVI}
    echo $animation
    ;;
  flv )
    echo -e "generating flv file (${ANIM_FLV}) ..."
    check_ffmpeg
    # Need to first generate the mpg file
    ${0} -c ${civgame} -m ${mapstr} -e ${mapext} -a mpg
    [ -e ${ANIM_FLV} ] && rm ${ANIM_FLV}
    ffmpeg -i ${ANIM_MPG} -f flv ${ANIM_FLV}
    ;;
  flv_from_mpg )
    # This is an internal tag; it assumes that the mpg file exists
    echo -e "generating flv file (${ANIM_FLV}) ..."
    check_ffmpeg
    [ ! -e ${ANIM_MPG} ] && echo "ERROR: ${ANIM_MPG} missing ..." && exit 1
    [ -e ${ANIM_FLV} ] && rm ${ANIM_FLV}
    ffmpeg -i ${ANIM_MPG} -f flv ${ANIM_FLV}
    ;;
  * )
    echo ""
    echo "Unimplemented value for -a: '${animation}'"
    usage
    exit 1
    ;;
esac