1 | #!/bin/ksh |
---|
2 | # img2video- script for generating video files |
---|
3 | # |
---|
4 | # Required packages: |
---|
5 | # Imagemagick |
---|
6 | # MEncoder (MPlayer) |
---|
7 | # gifsicle |
---|
8 | |
---|
9 | #PRESET OF VARIABLES |
---|
10 | input_folder="." |
---|
11 | output_folder="" |
---|
12 | output_file="" |
---|
13 | name="" |
---|
14 | codec="flash" |
---|
15 | colors=256 |
---|
16 | errors=false |
---|
17 | fps=25 |
---|
18 | hq=false |
---|
19 | img_fmt="jpg" |
---|
20 | keep=true |
---|
21 | preview_string="preview" |
---|
22 | restart=false |
---|
23 | rotate=0 |
---|
24 | scaling=1024 |
---|
25 | speedup=1 |
---|
26 | file_type="tiff" |
---|
27 | watermark="" |
---|
28 | |
---|
29 | #DEFINE STRING LENGTHS |
---|
30 | typeset -L20 col1 |
---|
31 | typeset -L40 col2 |
---|
32 | typeset -L30 version="img2video Rev$Rev: 1 $" |
---|
33 | typeset -R30 calltime |
---|
34 | typeset -L60 text1 |
---|
35 | |
---|
36 | #GET COMMAND LINE PARAMETERS |
---|
37 | while getopts :a:C:c:f:hi:I:kn:o:rR:S:s:t:Xw: option |
---|
38 | do |
---|
39 | case $option in |
---|
40 | (a) fps=$OPTARG;; |
---|
41 | (c) codec=$OPTARG;; |
---|
42 | (C) colors=$OPTARG;; |
---|
43 | (h) hq=true;; |
---|
44 | (i) input_folder=$OPTARG;; |
---|
45 | (k) keep=false;; |
---|
46 | (n) name=$OPTARG;; |
---|
47 | (o) output_folder=$OPTARG;; |
---|
48 | (r) restart=true;; |
---|
49 | (R) rbotate=$OPTARG;; |
---|
50 | (s) scaling=$OPTARG;; |
---|
51 | (S) speedup=$OPTARG;; |
---|
52 | (t) file_type=$OPTARG;; |
---|
53 | (w) watermark=$OPTARG;; |
---|
54 | (X) errors=true;; |
---|
55 | (\?) printf "\n +++ unknown option $OPTARG \n" |
---|
56 | printf "\n --> type \"$0 ?\" for available options \n" |
---|
57 | locat=parameter;exit;; |
---|
58 | esac |
---|
59 | done |
---|
60 | |
---|
61 | shift OPTIND-1 |
---|
62 | |
---|
63 | #PRINT HELP |
---|
64 | if [[ "$1" = "?" ]] |
---|
65 | then |
---|
66 | (printf "\n *** img2video can be called as follows:\n" |
---|
67 | printf "\n img2video -c.. -C.. -i.. -k -n.. -o.. -r -R -s.. -S.. -t.. -X -w\n" |
---|
68 | printf "\n Description of available options:\n" |
---|
69 | printf "\n Option Description Default-Value" |
---|
70 | printf "\n -a fps (frames per second) 25" |
---|
71 | printf "\n -c Video format flash" |
---|
72 | printf "\n Available formats are: flash, mpeg, mpeg4, " |
---|
73 | printf "\n mjpeg, gif, wmv2 " |
---|
74 | printf "\n -C Number of colors (only for gif output) 256" |
---|
75 | printf "\n -i Input path ." |
---|
76 | printf "\n -k Delete image files at the end " |
---|
77 | printf "\n -n Jobname \"\"" |
---|
78 | printf "\n -o Output folder see Input path" |
---|
79 | printf "\n -r Restart with existing image files " |
---|
80 | printf "\n -R Rotate by angle 0" |
---|
81 | printf "\n -s Rescaling to a width of #px 1024" |
---|
82 | printf "\n -S Video speed-up 1" |
---|
83 | printf "\n -t Input file type (tiff/eps) tiff" |
---|
84 | printf "\n -X enable output of additional error messages " |
---|
85 | printf "\n -w Watermark file \"\"" |
---|
86 | printf "\n ? this outline \n\n") | more |
---|
87 | exit |
---|
88 | elif [[ "$1" != "" ]] |
---|
89 | then |
---|
90 | printf "\n +++ Positional parameter $1 unknown \n" |
---|
91 | locat=parameter; exit |
---|
92 | fi |
---|
93 | |
---|
94 | if [[ $name == "" ]] |
---|
95 | then |
---|
96 | printf "\n +++ Parameter -n needed. Type 'img2video ?' for a full list of parameters. \n" |
---|
97 | locat=parameter; exit |
---|
98 | fi |
---|
99 | |
---|
100 | if [[ $input_folder == "." ]] |
---|
101 | then |
---|
102 | printf "\n +++ Parameter -i needed. Type 'img2video ?' for a full list of parameters. \n" |
---|
103 | locat=parameter; exit |
---|
104 | fi |
---|
105 | |
---|
106 | if [[ $output_folder == "" ]] |
---|
107 | then |
---|
108 | output_folder="$input_folder/$name" |
---|
109 | fi |
---|
110 | |
---|
111 | #PRINT HEADER TO INFORM ABOUT THE CURRENT SETTINGS |
---|
112 | calltime=$(date) |
---|
113 | |
---|
114 | printf "\n#--------------------------------------------------------------#" |
---|
115 | printf "\n| $version$calltime |" |
---|
116 | |
---|
117 | col1="" |
---|
118 | col2="" |
---|
119 | printf "\n| $col1$col2 |" |
---|
120 | |
---|
121 | text1="Initialization parameters" |
---|
122 | printf "\n| $text1 |" |
---|
123 | |
---|
124 | col1="Jobname:" |
---|
125 | col2=$name |
---|
126 | printf "\n| $col1$col2 |" |
---|
127 | |
---|
128 | col1="Input path:" |
---|
129 | col2=$input_folder |
---|
130 | printf "\n| $col1$col2 |" |
---|
131 | row=$(echo "$input_folder" | cut -c41-) |
---|
132 | while [[ "$row" != "" ]] |
---|
133 | do |
---|
134 | col1="" |
---|
135 | col2=$row |
---|
136 | printf "\n| $col1$col2 |" |
---|
137 | row=$(echo "$row" | cut -c41-) |
---|
138 | done |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | col1="Output path:" |
---|
145 | col2=$output_folder |
---|
146 | printf "\n| $col1$col2 |" |
---|
147 | row=$(echo "$output_folder" | cut -c41-) |
---|
148 | while [[ "$row" != "" ]] |
---|
149 | do |
---|
150 | col1="" |
---|
151 | col2=$row |
---|
152 | printf "\n| $col1$col2 |" |
---|
153 | row=$(echo "$row" | cut -c41-) |
---|
154 | done |
---|
155 | |
---|
156 | |
---|
157 | if [[ "$codec" = "flash" ]] |
---|
158 | then |
---|
159 | suffix=".flv" |
---|
160 | real_codec="flv" |
---|
161 | |
---|
162 | elif [[ "$codec" = "mpeg" ]] |
---|
163 | then |
---|
164 | suffix=".avi" |
---|
165 | real_codec="msmpeg4v2" |
---|
166 | |
---|
167 | elif [[ "$codec" = "mpeg4" ]] |
---|
168 | then |
---|
169 | suffix=".avi" |
---|
170 | real_codec="mpeg4" |
---|
171 | |
---|
172 | elif [[ "$codec" = "mjpeg" ]] |
---|
173 | then |
---|
174 | suffix=".avi" |
---|
175 | real_codec="mjpeg" |
---|
176 | |
---|
177 | elif [[ "$codec" = "wmv2" ]] |
---|
178 | then |
---|
179 | suffix=".wmv" |
---|
180 | real_codec="wmv2" |
---|
181 | elif [[ "$codec" = "gif" ]] |
---|
182 | then |
---|
183 | suffix=".gif" |
---|
184 | real_codec="gif" |
---|
185 | img_fmt="gif" |
---|
186 | else |
---|
187 | printf "\n +++ Video codec unknown. Aborting..." |
---|
188 | exit |
---|
189 | fi |
---|
190 | |
---|
191 | col1="Video codec:" |
---|
192 | col2="$codec ($real_codec)" |
---|
193 | printf "\n| $col1$col2 |" |
---|
194 | |
---|
195 | if [[ $codec = "gif" ]] |
---|
196 | then |
---|
197 | col1="Colors (gif):" |
---|
198 | col2=$colors |
---|
199 | printf "\n| $col1$col2 |" |
---|
200 | fi |
---|
201 | |
---|
202 | col1="Input file type:" |
---|
203 | col2=$file_type |
---|
204 | printf "\n| $col1$col2 |" |
---|
205 | |
---|
206 | col1="Video speed-up:" |
---|
207 | col2=$speedup |
---|
208 | printf "\n| $col1$col2 |" |
---|
209 | |
---|
210 | col1="fps:" |
---|
211 | col2=$fps |
---|
212 | printf "\n| $col1$col2 |" |
---|
213 | |
---|
214 | if [[ $rotate != 0 ]] |
---|
215 | then |
---|
216 | col1="Rotate video by" |
---|
217 | col2="$rotate°" |
---|
218 | printf "\n| $col1$col2 |" |
---|
219 | fi |
---|
220 | |
---|
221 | col1="Scaling video to" |
---|
222 | col2="${scaling}px width" |
---|
223 | printf "\n| $col1$col2 |" |
---|
224 | |
---|
225 | if [[ $watermark != "" ]] |
---|
226 | then |
---|
227 | col1="Watermark:" |
---|
228 | col2=$watermark |
---|
229 | else |
---|
230 | col1="Watermark " |
---|
231 | col2="disabled" |
---|
232 | fi |
---|
233 | printf "\n| $col1$col2 |" |
---|
234 | |
---|
235 | col1="HQ-options:" |
---|
236 | if [[ $hq = true ]] |
---|
237 | then |
---|
238 | |
---|
239 | if [[ $codec == mjpeg ]] |
---|
240 | then |
---|
241 | hq_text=":mbd=2" |
---|
242 | else |
---|
243 | hq_text=":mbd=2:trell" |
---|
244 | fi |
---|
245 | col2=$hq_text |
---|
246 | else |
---|
247 | hq_text="" |
---|
248 | col2="disabled" |
---|
249 | fi |
---|
250 | printf "\n| $col1$col2 |" |
---|
251 | |
---|
252 | col1="" |
---|
253 | col2="" |
---|
254 | printf "\n| $col1$col2 |" |
---|
255 | |
---|
256 | col1="Additional settings" |
---|
257 | col2="" |
---|
258 | printf "\n| $col1$col2 |" |
---|
259 | |
---|
260 | if [[ $keep == false ]] |
---|
261 | then |
---|
262 | text1="- Image files will be deleted at the end." |
---|
263 | else |
---|
264 | text1="- Image files will be stored at the end." |
---|
265 | fi |
---|
266 | printf "\n| $text1 |" |
---|
267 | |
---|
268 | if [[ $restart == true ]] |
---|
269 | then |
---|
270 | text1="- img2video will restart with existing images." |
---|
271 | else |
---|
272 | text1="- img2video will generate new images." |
---|
273 | fi |
---|
274 | printf "\n| $text1 |" |
---|
275 | |
---|
276 | col1="" |
---|
277 | col2="" |
---|
278 | printf "\n| $col1$col2 |" |
---|
279 | |
---|
280 | if [[ $output_file = "" ]] |
---|
281 | then |
---|
282 | output_file="${name}_${speedup}acc_${codec}" |
---|
283 | fi |
---|
284 | |
---|
285 | #CHECK CYCLE NUMBER |
---|
286 | looping=true |
---|
287 | output_file_new=$output_file |
---|
288 | while [[ $looping = true ]] |
---|
289 | do |
---|
290 | if [ -f "$output_folder/$output_file_new$suffix" ] |
---|
291 | then |
---|
292 | ((run_string += 1)) |
---|
293 | output_file_new="${output_file}_$run_string" |
---|
294 | else |
---|
295 | looping=false |
---|
296 | if (( run_string >= 1 )) |
---|
297 | then |
---|
298 | output_file="${output_file}_$run_string" |
---|
299 | preview_string="preview_$run_string" |
---|
300 | fi |
---|
301 | break; |
---|
302 | fi |
---|
303 | done |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | text1="Video will be saved as $output_file$suffix" |
---|
308 | printf "\n| $text1 |" |
---|
309 | |
---|
310 | printf "\n| |" |
---|
311 | printf "\n#--------------------------------------------------------------#\n" |
---|
312 | |
---|
313 | if [[ $errors = false ]] |
---|
314 | then |
---|
315 | error_opt="-really-quiet" |
---|
316 | error_opt2="-quiet" |
---|
317 | else |
---|
318 | error_opt="" |
---|
319 | error_opt2="" |
---|
320 | fi |
---|
321 | |
---|
322 | |
---|
323 | #STARTING CONVERSION |
---|
324 | printf "\n *** Creating directory: $output_folder/images_${img_fmt}... " |
---|
325 | mkdir -p $output_folder/images_${img_fmt} |
---|
326 | printf "done." |
---|
327 | |
---|
328 | |
---|
329 | if [[ $restart = false ]] |
---|
330 | then |
---|
331 | printf "\n *** Outdated images will be deleted in $output_folder/images_${img_fmt}... " |
---|
332 | rm -rf $output_folder/images_${img_fmt}/* |
---|
333 | printf "done." |
---|
334 | |
---|
335 | printf "\n *** Converting images... " |
---|
336 | if [[ $file_type = "eps" ]] |
---|
337 | then |
---|
338 | |
---|
339 | # CONVERTING IMAGES FROM EPS TO JPG OR GIF |
---|
340 | img_counter=0 |
---|
341 | for i in $input_folder/$name*; |
---|
342 | do |
---|
343 | (( img_counter+=1 )) |
---|
344 | filename=`basename ${i%}` |
---|
345 | if [[ $img_fmt = "gif" ]] |
---|
346 | then |
---|
347 | gs -sDEVICE=jpeg -dJPEGQ=100 -q -dNOPAUSE -dBATCH -dSAFER -r175 -sOutputFile="$output_folder/images_${img_fmt}/${filename}.jpg" "$i" |
---|
348 | convert $error_opt2 -resize "$scaling" "$output_folder/images_${img_fmt}/${filename}.jpg" "$output_folder/images_${img_fmt}/${filename}.${img_fmt}" |
---|
349 | rm "$output_folder/images_${img_fmt}/${filename}.jpg" |
---|
350 | else |
---|
351 | gs -sDEVICE=jpeg -dJPEGQ=100 -q -dNOPAUSE -dBATCH -dSAFER -r175 -sOutputFile="$output_folder/images_${img_fmt}/${filename}.${img_fmt}" "$i" |
---|
352 | fi |
---|
353 | done |
---|
354 | |
---|
355 | else |
---|
356 | |
---|
357 | for i in $input_folder/$name*; |
---|
358 | do |
---|
359 | (( img_counter+=1 )) |
---|
360 | filename=`basename ${i%}` |
---|
361 | convert $error_opt2 "$i" "$output_folder/images_${img_fmt}/${filename}.${img_fmt}" |
---|
362 | done |
---|
363 | fi |
---|
364 | printf "done." |
---|
365 | printf "\n --> Converted $img_counter images." |
---|
366 | |
---|
367 | |
---|
368 | # ROTATE IMAGE IF NECESSARY |
---|
369 | if (( $rotate != 0 )) |
---|
370 | then |
---|
371 | printf "\n *** Rotating images... " |
---|
372 | for i in $output_folder/images_${img_fmt}/$name*; |
---|
373 | do |
---|
374 | convert $error_opt2 -rotate $rotate "$output_folder/images_${img_fmt}/${filename}.$img_fmt" "$output_folder/images_${img_fmt}/${filename}.${img_fmt}" |
---|
375 | done |
---|
376 | printf "done." |
---|
377 | fi |
---|
378 | |
---|
379 | # ADD WATERMARK IF NECESSARY (GIF ONLY) |
---|
380 | if [[ $watermark != "" && $img_fmt = "gif" ]] |
---|
381 | then |
---|
382 | printf "\n *** Adding Watermark (only necessary for gif)... " |
---|
383 | for i in $output_folder/images_${img_fmt}/$name*; |
---|
384 | do |
---|
385 | composite $watermark "$i" "$i" |
---|
386 | done |
---|
387 | printf "done." |
---|
388 | fi |
---|
389 | |
---|
390 | # GENERATE A PREVIEW IMAGE |
---|
391 | cp $output_folder/images_${img_fmt}/${filename}.$img_fmt $output_folder/${name}_${preview_string}.${img_fmt} |
---|
392 | else |
---|
393 | printf "\n *** Restarting with images in ${name}*.$img_fmt in $output_folder/images_${img_fmt}/." |
---|
394 | fi |
---|
395 | |
---|
396 | #ENABLE WATERMARK (not for gif) |
---|
397 | if [[ $watermark != "" && $codec != "gif" ]] |
---|
398 | then |
---|
399 | printf "\n *** Watermark enabled." |
---|
400 | mkfifo watermark.fifo |
---|
401 | convert $watermark watermark.rgba |
---|
402 | echo "RGBA32 400 43 0 0 0 1" > watermark.fifo & |
---|
403 | cat watermark.rgba > watermark.fifo & |
---|
404 | wm_text=",bmovl=0:0:watermark.fifo" |
---|
405 | else |
---|
406 | wm_text="" |
---|
407 | fi |
---|
408 | |
---|
409 | #START ENCODING OF THE VIDEO |
---|
410 | printf "\n *** Encoding video... " |
---|
411 | |
---|
412 | if [[ $codec = "gif" ]] |
---|
413 | then |
---|
414 | # Animated GIF |
---|
415 | j=1 |
---|
416 | if (( speedup != 1 )) |
---|
417 | then |
---|
418 | cp -r $output_folder/images_${img_fmt} $output_folder/images_${img_fmt}_tmp |
---|
419 | fi |
---|
420 | |
---|
421 | for i in $output_folder/images_${img_fmt}/$name*; |
---|
422 | do |
---|
423 | if (( $j == $speedup )) |
---|
424 | then |
---|
425 | j=1 |
---|
426 | else |
---|
427 | rm $i |
---|
428 | (( j += 1 )) |
---|
429 | fi |
---|
430 | done |
---|
431 | |
---|
432 | (( speedup = ( 1.0 / $fps ) * 100.0 )) |
---|
433 | gifsicle --delay=$speedup --colors $colors --loop $output_folder/images_${img_fmt}/*.${img_fmt} > $output_folder/${output_file}$suffix |
---|
434 | |
---|
435 | if (( speedup != 1 )) |
---|
436 | then |
---|
437 | cp $output_folder/images_${img_fmt}_tmp/* $output_folder/images_${img_fmt}/ |
---|
438 | rm -r $output_folder/images_${img_fmt}_tmp |
---|
439 | fi |
---|
440 | else |
---|
441 | mencoder mf://$output_folder/images_${img_fmt}/*$img_fmt -vf "scale=$scaling:-2$wm_text,filmdint=io=$speedup:1" -of lavf -mf "fps=$fps" -ovc lavc -lavcopts "vcodec=$real_codec$hq_text" -nosound -o $output_folder/${output_file}$suffix $error_opt |
---|
442 | fi |
---|
443 | |
---|
444 | printf "done." |
---|
445 | |
---|
446 | #CLEANING UP |
---|
447 | |
---|
448 | #REMOVE IMAGES |
---|
449 | if [[ $keep = false ]] |
---|
450 | then |
---|
451 | rm -rf $output_folder/images_${img_fmt} |
---|
452 | fi |
---|
453 | |
---|
454 | #REMOVE WATERMARK FILES |
---|
455 | if [[ $watermark != "" && $codec != "gif" ]] |
---|
456 | then |
---|
457 | rm watermark.fifo |
---|
458 | rm watermark.rgba |
---|
459 | fi |
---|
460 | |
---|
461 | printf "\n --> All actions finished. Video saved as $output_folder/${output_file}$suffix.\n" |
---|
462 | exit |
---|