source: palm/trunk/SCRIPTS/img2video @ 2699

Last change on this file since 2699 was 2696, checked in by kanani, 7 years ago

Merge of branch palm4u into trunk

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 14.8 KB
Line 
1#!/bin/ksh
2
3#--------------------------------------------------------------------------------#
4# This file is part of the PALM model system.
5#
6# PALM is free software: you can redistribute it and/or modify it under the terms
7# of the GNU General Public License as published by the Free Software Foundation,
8# either version 3 of the License, or (at your option) any later version.
9#
10# PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along with
15# PALM. If not, see <http://www.gnu.org/licenses/>.
16#
17# Copyright 1997-2017  Leibniz Universitaet Hannover
18#--------------------------------------------------------------------------------#
19#
20# Current revisions:
21# -----------------
22#
23#
24# Former revisions:
25# -----------------
26# $Id: img2video 2696 2017-12-14 17:12:51Z kanani $
27#
28# 1046 2012-11-09 14:38:45Z maronga
29# code put under GPL (PALM 3.9)
30#
31# 981 2012-08-09 14:57:44Z maronga
32# Added new codecs xvid, ffv1 and two-pass encoding
33#
34# 795 2011-12-12 15:21:03Z maronga
35# Added Id tag, initial Revision
36#
37# Description:
38# ------------
39# img2video converts image files (e.g. from NCL, DVRP, VAPOR) and creates
40# video files with several codecs. Required packages on the local host are:
41# Imagemagick, MEncoder (MPlayer) and gifsicle
42#------------------------------------------------------------------------------!
43
44#PRESET OF VARIABLES
45 input_folder="."
46 output_folder=""
47 output_file=""
48 name=""
49 codec="mpeg4"
50 colors=256
51 errors=false
52 fps=25
53 hq=false
54 img_fmt="png"
55 keep=true
56 pass=1
57 preview_string="preview"
58 restart=false
59 rotate=0
60 scaling=1024
61 speedup=1
62 file_type="tiff"
63 watermark=""
64
65#DEFINE STRING LENGTHS
66 typeset  -L20 col1
67 typeset  -L40 col2
68 typeset  -L30 version="img2video  Rev$Rev: 1 $"
69 typeset  -R30 calltime
70 typeset  -L60 text1
71
72#GET COMMAND LINE PARAMETERS
73 while  getopts  :a:C:c:f:hi:I:kn:o:rR:S:s:t:Xw: option
74 do
75   case  $option  in
76       (a)   fps=$OPTARG;;
77       (c)   codec=$OPTARG;;
78       (C)   colors=$OPTARG;;
79       (f)   output_file=$OPTARG;;
80       (h)   hq=true;;
81       (i)   input_folder=$OPTARG;;
82       (k)   keep=false;;
83       (n)   name=$OPTARG;;
84       (o)   output_folder=$OPTARG;;
85       (r)   restart=true;;
86       (R)   rotate=$OPTARG;;
87       (s)   scaling=$OPTARG;;
88       (S)   speedup=$OPTARG;;
89       (t)   file_type=$OPTARG;;
90       (w)   watermark=$OPTARG;;
91       (X)   errors=true;;
92       (\?)  printf "\n  +++ unknown option $OPTARG \n"
93             printf "\n  --> type \"$0 ?\" for available options \n"
94             locat=parameter;exit;;
95   esac
96 done
97
98 shift OPTIND-1
99
100#PRINT HELP
101 if [[ "$1" = "?" ]]
102 then
103   (printf "\n  *** img2video can be called as follows:\n"
104    printf "\n      img2video  -c.. -C.. -h -i.. -k -n.. -o.. -r -R.. -s.. -S.. -t.. -X -w\n"
105    printf "\n      Description of available options:\n"
106    printf "\n      Option      Description                                 Default-Value"
107    printf "\n        -a        fps (frames per second)                                25"
108    printf "\n        -c        Video format                                        flash"
109    printf "\n                  Available formats are: flash, mpeg, mpeg4,               "
110    printf "\n                  mjpeg, gif, wmv2, xvid, ffv1                             "
111    printf "\n        -C        Number of colors (only for gif output)                256"
112    printf "\n        -f        Output file name                                     auto"
113    printf "\n        -h        enable high quality                                  \"\""
114    printf "\n        -i        Input path                                              ."
115    printf "\n        -k        Delete image files at the end                            "
116    printf "\n        -n        Jobname                                                \"\""
117    printf "\n        -o        Output folder                              see Input path"
118    printf "\n        -r        Restart with existing image files                        "
119    printf "\n        -R        Rotate by angle                                         0"
120    printf "\n        -s        Rescaling to a width of #px                          1024"
121    printf "\n        -S        Video speed-up                                          1"
122    printf "\n        -t        Input file type (tiff/eps)                           tiff"
123    printf "\n        -X        enable output of additional error messages               "
124    printf "\n        -w        Watermark file                                         \"\""
125    printf "\n         ?        this outline \n\n") | more
126    exit
127 elif [[ "$1" != "" ]]
128 then
129    printf "\n  +++ Positional parameter $1 unknown \n"
130    locat=parameter; exit
131 fi
132
133 if  [[ $name == "" ]]
134 then
135     printf "\n  +++ Parameter -n needed. Type 'img2video ?' for a full list of parameters. \n"
136    locat=parameter; exit
137 fi
138
139 if  [[ $input_folder == "." ]]
140 then
141     printf "\n  +++ Parameter -i needed. Type 'img2video ?' for a full list of parameters. \n"
142    locat=parameter; exit
143 fi
144
145 if  [[ $output_folder == "" ]]
146 then
147    output_folder="$input_folder/$name"
148 fi
149
150#PRINT HEADER TO INFORM ABOUT THE CURRENT SETTINGS
151 calltime=$(date)
152
153 printf "\n#--------------------------------------------------------------#"
154 printf "\n| $version$calltime |"
155
156 col1=""
157 col2=""
158 printf "\n| $col1$col2 |"
159 
160 text1="Initialization parameters"
161 printf "\n| $text1 |"
162 
163 col1="Jobname:"
164 col2=$name
165 printf "\n| $col1$col2 |"
166
167 col1="Input path:"
168 col2=$input_folder
169 printf "\n| $col1$col2 |"
170 row=$(echo "$input_folder" | cut -c41-)
171 while [[ "$row" != "" ]]
172 do
173    col1=""
174    col2=$row
175    printf "\n| $col1$col2 |"
176    row=$(echo "$row" | cut -c41-)
177 done
178
179
180
181
182
183 col1="Output path:"
184 col2=$output_folder
185 printf "\n| $col1$col2 |"
186 row=$(echo "$output_folder" | cut -c41-)
187 while [[ "$row" != "" ]]
188 do
189    col1=""
190    col2=$row
191    printf "\n| $col1$col2 |"
192    row=$(echo "$row" | cut -c41-)
193 done
194
195
196 if [[ "$codec" = "flash" ]]
197 then
198    suffix=".flv"
199    real_codec="flv"
200
201 elif [[ "$codec" = "mpeg" ]]
202 then
203    suffix=".avi"
204    real_codec="msmpeg4v2"
205    pass=2
206 elif [[ "$codec" = "mpeg4" ]]
207 then
208    suffix=".avi"
209    real_codec="mpeg4"
210    pass=2
211 elif [[ "$codec" = "mjpeg" ]]
212 then
213    suffix=".avi"
214    real_codec="mjpeg"
215
216 elif [[ "$codec" = "wmv2" ]]
217 then
218    suffix=".wmv"
219    real_codec="wmv2"
220 elif [[ "$codec" = "xvid" ]]
221 then
222    suffix=".avi"
223    real_codec="xvid"
224    pass=2
225 elif [[ "$codec" = "ffv1" ]]
226 then
227    suffix=".avi"
228    real_codec="ffv1"
229    pass=1
230 elif [[ "$codec" = "gif" ]]
231 then
232    suffix=".gif"
233    real_codec="gif"
234    img_fmt="gif"
235 else
236    printf "\n +++ Video codec unknown. Aborting..."
237    exit
238 fi
239
240 col1="Video codec:"
241 col2="$codec ($real_codec)"
242 printf "\n| $col1$col2 |"
243
244 if [[ $codec = "gif" ]]
245 then
246    col1="Colors (gif):"
247    col2=$colors
248    printf "\n| $col1$col2 |"
249 fi
250
251 col1="Input file type:"
252 col2=$file_type
253 printf "\n| $col1$col2 |"
254
255 col1="Video speed-up:"
256 col2=$speedup
257 printf "\n| $col1$col2 |"
258
259 col1="fps:"
260 col2=$fps
261 printf "\n| $col1$col2 |"
262
263 if [[ $rotate != 0 ]]
264 then
265    col1="Rotate video by"
266    col2="${rotate}°"
267    printf "\n| $col1$col2 |"
268 fi
269
270 col1="Scaling video to"
271 col2="${scaling}px width"
272 printf "\n| $col1$col2 |"
273
274 if [[ $watermark != "" ]]
275 then
276    col1="Watermark:"
277    col2=$watermark
278 else
279    col1="Watermark "
280    col2="disabled"
281 fi
282 printf "\n| $col1$col2 |"
283
284 col1="HQ-options:"
285 if [[ $hq = true ]]
286 then
287    if [[ $codec == mjpeg ]]
288    then
289       hq_text=":mbd=2"
290    fi
291    if [[ $codec == mpeg4 ]]
292    then
293       hq_text=":mbd=2:trell:vbitrate=2160000:keyint=132"
294    fi
295    col2=$hq_text
296 else
297    hq_text=""
298    col2="disabled"
299 fi
300 printf "\n| $col1$col2 |"
301
302 col1=""
303 col2=""
304 printf "\n| $col1$col2 |"
305
306 col1="Additional settings"
307 col2=""
308 printf "\n| $col1$col2 |"
309
310 if [[ $keep == false ]]
311 then
312    text1="- Image files will be deleted at the end."
313 else
314    text1="- Image files will be stored at the end."
315 fi
316 printf "\n| $text1 |"
317
318 if [[ $restart == true ]]
319 then
320    text1="- img2video will restart with existing images."
321 else
322    text1="- img2video will generate new images."
323 fi
324 printf "\n| $text1 |"
325
326 col1=""
327 col2=""
328 printf "\n| $col1$col2 |"
329
330 if [[ $output_file = "" ]]
331 then
332    output_file="${name}_${speedup}acc_${codec}"
333 fi
334
335#CHECK CYCLE NUMBER
336 looping=true
337 output_file_new=$output_file
338 while [[ $looping = true ]]
339 do
340    if [ -f "$output_folder/$output_file_new$suffix" ]
341    then
342       ((run_string += 1))
343       output_file_new="${output_file}_$run_string"
344    else
345       looping=false
346       if (( run_string >= 1 ))
347       then
348          output_file="${output_file}_$run_string"
349          preview_string="preview_$run_string"
350       fi
351       break;
352    fi
353 done
354
355
356
357 text1="Video will be saved as $output_file$suffix"
358 printf "\n| $text1 |"
359
360 printf "\n|                                                              |"
361 printf "\n#--------------------------------------------------------------#\n"
362
363 if [[ $errors = false ]]
364 then
365    error_opt="-really-quiet"
366    error_opt2="-quiet"
367 else
368    error_opt=""
369    error_opt2=""
370 fi
371
372
373#STARTING CONVERSION
374 printf "\n *** Creating directory: $output_folder/images_${img_fmt}... "
375 mkdir -p $output_folder/images_${img_fmt}
376 printf "done."
377
378
379 if [[ $restart = false ]] 
380 then
381    printf "\n *** Outdated images will be deleted in $output_folder/images_${img_fmt}... "
382    rm -rf $output_folder/images_${img_fmt}/*
383    printf "done."
384
385    printf "\n *** Converting images... "
386    if [[ $file_type = "eps" ]]
387    then
388
389#      CONVERTING IMAGES FROM EPS TO JPG OR GIF
390       img_counter=0
391       for i in $input_folder/$name*;
392       do
393          (( img_counter+=1 ))
394          filename=`basename ${i%}`
395          if [[ $img_fmt = "gif" ]]
396          then
397             gs -sDEVICE=jpeg -dJPEGQ=100 -q -dNOPAUSE -dBATCH -dSAFER -r175 -sOutputFile="$output_folder/images_${img_fmt}/${filename}.jpg" "$i"
398             convert $error_opt2 -resize "$scaling" "$output_folder/images_${img_fmt}/${filename}.jpg" "$output_folder/images_${img_fmt}/${filename}.${img_fmt}"
399             rm "$output_folder/images_${img_fmt}/${filename}.jpg"
400          else
401             gs -sDEVICE=jpeg -dJPEGQ=100 -q -dNOPAUSE -dBATCH -dSAFER -r175 -sOutputFile="$output_folder/images_${img_fmt}/${filename}.${img_fmt}" "$i"
402          fi
403       done
404
405    else
406
407       for i in $input_folder/$name*;
408       do
409          (( img_counter+=1 ))
410          filename=`basename ${i%}`
411          convert $error_opt2 "$i" "$output_folder/images_${img_fmt}/${filename}.${img_fmt}"
412       done
413    fi
414    printf "done."
415    printf "\n --> Converted $img_counter images."
416
417
418#   ROTATE IMAGE IF NECESSARY
419    if (( $rotate != 0 ))
420    then
421       printf "\n *** Rotating images... "
422       for i in $output_folder/images_${img_fmt}/$name*;
423       do
424          convert $error_opt2 -rotate $rotate $i $i
425       done
426       printf "done."
427    fi
428
429#   ADD WATERMARK IF NECESSARY (GIF ONLY)
430    if [[ $watermark != "" && $img_fmt = "gif" ]]
431    then
432       printf "\n *** Adding Watermark (only necessary for gif)... "
433       for i in $output_folder/images_${img_fmt}/$name*;
434       do
435          composite $watermark "$i" "$i"
436       done
437       printf "done."
438    fi
439
440#   GENERATE A PREVIEW IMAGE
441    cp $output_folder/images_${img_fmt}/${filename}.$img_fmt $output_folder/${name}_${preview_string}.${img_fmt}
442 else
443    printf "\n *** Restarting with images in ${name}*.$img_fmt in $output_folder/images_${img_fmt}/."
444 fi
445
446#ENABLE WATERMARK (not for gif)
447 if [[ $watermark != "" && $codec != "gif" ]]
448 then
449    printf "\n *** Watermark enabled."
450    mkfifo watermark.fifo
451    convert $watermark watermark.rgba
452    echo "RGBA32 400 43 0 0 0 1" > watermark.fifo &
453    cat watermark.rgba > watermark.fifo &
454    wm_text=",bmovl=0:0:watermark.fifo"
455 else
456    wm_text=""
457 fi
458
459#START ENCODING OF THE VIDEO
460 printf "\n *** Encoding video... "
461
462 if [[ $codec = "gif" ]]
463 then
464#   Animated GIF
465    j=1
466    if (( speedup != 1 ))
467    then
468       cp -r $output_folder/images_${img_fmt} $output_folder/images_${img_fmt}_tmp
469    fi
470
471    for i in $output_folder/images_${img_fmt}/$name*;
472    do
473       if (( $j == $speedup ))
474       then
475          j=1
476       else
477          rm $i
478          (( j += 1 ))
479       fi
480    done
481
482    (( speedup = ( 1.0 / $fps ) * 100.0 ))
483    gifsicle --delay=$speedup --colors $colors --loop $output_folder/images_${img_fmt}/*.${img_fmt} > $output_folder/${output_file}$suffix
484
485    if (( speedup != 1 ))
486    then
487       cp $output_folder/images_${img_fmt}_tmp/* $output_folder/images_${img_fmt}/
488       rm -r $output_folder/images_${img_fmt}_tmp
489    fi
490 else
491    if [[ $real_codec = "xvid" ]]
492    then
493       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 xvid -xvidencopts "pass=1:autoaspect" -nosound -o $output_folder/${output_file}$suffix $error_opt
494       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 xvid -xvidencopts "pass=2:autoaspect:bitrate=1800" -nosound -o $output_folder/${output_file}$suffix $error_opt
495    elif [[ $pass = 2 ]]
496    then
497        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 "vpass=1:vcodec=$real_codec$hq_text" -nosound -o $output_folder/${output_file}$suffix $error_opt
498        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 "vpass=2:vcodec=$real_codec$hq_text" -nosound -o $output_folder/${output_file}$suffix $error_opt     
499    else
500       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
501    fi
502
503#
504
505
506# 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:vpass=2:mbd=2:mv0:trell:vbitrate=infinity" -nosound -o $output_folder/${output_file}$suffix $error_opt
507# 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:vpass=3:mbd=2:mv0:trell:vbitrate=infinity" -nosound -o $output_folder/${output_file}$suffix $error_opt
508 fi
509
510 printf "done."
511
512#CLEANING UP
513
514#REMOVE IMAGES
515 if [[ $keep = false ]]
516 then
517    rm -rf $output_folder/images_${img_fmt}
518 fi
519
520#REMOVE WATERMARK FILES
521 if [[ $watermark != "" && $codec != "gif" ]]
522 then
523    rm watermark.fifo
524    rm watermark.rgba
525 fi
526
527 printf "\n --> All actions finished. Video saved as $output_folder/${output_file}$suffix.\n"
528 exit
Note: See TracBrowser for help on using the repository browser.