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