source: palm/tags/release-3.8/SCRIPTS/img2video/img2video @ 4343

Last change on this file since 4343 was 597, checked in by maronga, 13 years ago

bugfixes in img2video

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