examples/image_convert.sh.example
author nathan
Sun, 12 Dec 2010 12:15:17 +0100
branchtrunk
changeset 40 31537cd8ec5e
parent 0 474a1293c3c0
permissions -rw-r--r--
fixed image_convert.sh for newer mjpegtools
     1 #!/bin/bash
     2 #
     3 # requires: ...topnm, pnmscale, pnmcomp, ppmntsc, ppmtoy4m, mpeg2enc
     4 #
     5 
     6 # video format. pal or ntsc
     7 FORMAT=pal
     8 
     9 # target image width/height (taking into account visible screen area)
    10 if [ "$FORMAT" = "ntsc" ]; then
    11   TW=600
    12   TH=420
    13 else
    14   TW=632
    15   TH=512
    16 fi
    17 
    18 TMP=/tmp/image_convert.$$.pnm
    19 IMG=$1
    20 MPG=$2
    21 
    22 DIR=`dirname "$MPG"`
    23 if [ ! -d "$DIR" ]; then
    24   mkdir -p "$DIR"
    25 fi
    26 #
    27 # get the file type and set the according converter to PNM
    28 #
    29 FILE_TYPE=`file -i -L -b "$IMG" 2>/dev/null | cut -f2 -d/`
    30 case "$FILE_TYPE" in
    31   jpg | jpeg)
    32   TO_PNM=jpegtopnm
    33   ;;
    34   tiff)
    35   TO_PNM=tifftopnm
    36   ;;
    37   bmp | x-bmp)
    38   TO_PNM=bmptoppm
    39   ;;
    40   png | x-png)
    41   TO_PNM=pngtopnm
    42   ;;
    43   Netpbm | pnm | x-portable-pixmap)
    44   TO_PNM=cat
    45   ;;
    46   gif)
    47   TO_PNM=giftopnm
    48   ;;
    49   *)
    50   echo "filetype '$FILE_TYPE' is not supported"
    51   exit 1
    52   ;;
    53 esac
    54 #
    55 # 'chroma subsampling mode' mjpegtools >= 1.8.0
    56 #
    57 SUBSAMPLINGMODE=""
    58 if ppmtoy4m -h 2>&1 | egrep -q "'420mpeg2'"; then
    59     SUBSAMPLINGMODE="-S 420mpeg2"
    60 fi
    61 #
    62 # extract the image size & compute scale value
    63 #
    64 LANG=C # get the decimal point right
    65 $TO_PNM "$IMG" >$TMP 2>/dev/null
    66 S=`pnmfile $TMP | awk '{ printf "%d %d ",$4,$6 }'`
    67 S=`echo $S $TW $TH | awk '{ sw=$3/$1; sh=$4/$2; s=(sw<sh)?sw:sh; printf "%.4f\n",(s>1)?1.0:s; }'`
    68 #
    69 # now run the conversion
    70 #
    71 if [ "$FORMAT" = "ntsc" ]; then
    72   pnmscale $S $TMP | \
    73     pnmpad -black -width 704 -height 480 | \
    74     ppmntsc | \
    75     ppmtoy4m -v 0 -n 1 -r -F 30000:1001 $SUBSAMPLINGMODE | \
    76     mpeg2enc -f 7 -T 90 -F 4 -nn -a 2 -v 0 -o "$MPG"
    77 else
    78   pnmscale $S $TMP | \
    79     pnmpad -black -width 704 -height 576 | \
    80     ppmntsc --pal | \
    81     ppmtoy4m -v 0 -n 1 -r -F 25:1 $SUBSAMPLINGMODE | \
    82     mpeg2enc -f 7 -T 90 -F 3 -np -a 2 -v 0 -o "$MPG"
    83 fi
    84 #
    85 # cleanup
    86 #
    87 rm $TMP