source: palm/trunk/SCRIPTS/img2video @ 2716

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

Correction of "Former revisions" section

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