source: palm/trunk/SCRIPTS/img2video @ 1037

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

last commit documented

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