source: palm/trunk/SCRIPTS/img2video/img2video @ 795

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

added Id tag to img2video

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