source: palm/trunk/SCRIPTS/img2video @ 1046

Last change on this file since 1046 was 1046, checked in by maronga, 12 years ago

put scripts and utilities under GPL

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