source: palm/trunk/SCRIPTS/img2video @ 981

Last change on this file since 981 was 981, checked in by maronga, 9 years ago

removed typos, update in img2video, added new tool img2stereo

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