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