pepomp4ctl

pepomp4ctlとはRaspberry Piに接続したWEBカメラやModuleカメラで動画や写真を取得するちょっとしたプログラムです

  1. #!/bin/sh
  2. # licence GPLv2 ; this scripts designed by IZAMUKARERA 2013.10.12 update 2015.9.9
  3. # pepomp4ctl ; get video or jpeg
  4. # pepomp4ctl dev image pid
  5. #VIDEOPG=ffmpeg
  6. RASPICAMERA=/dev/vchiq
  7. RASPIVID=raspivid
  8. RASPISTILL=raspistill
  9. VIDEOPG=avconv
  10. if [ $# != 3 ];then
  11. echo "$0 video-dev xxx.jpg|xxx.mp4 ppid"
  12. exit -1
  13. fi
  14. WORKDIR=/www/remote-hand/tmp
  15. PATH=$PATH:/usr/local/bin:/usr/local/sbin
  16. MP4=`echo $2 | awk 'BEGIN{FS="."};{if($2=="mp4"){printf "YES"} else {printf "NO"}}'`
  17. DEV=$1
  18. IMAGE=$2
  19. ppid=$3
  20. if [ $MP4 = "YES" ];then
  21. OPT=" -r 5 -vcodec mpeg4 -t 10"
  22. else
  23. OPT=""
  24. fi
  25. prog=pepomp4ctl
  26. pidfile=/var/run/${prog}.pid
  27. pprog=pepogmail4pic
  28. ppidfile=/var/run/${pprog}.pid
  29. WORKIMAGE=${WORKDIR}/${IMAGE}
  30. error(){
  31. [ -e ${WORKIMAGE} ] && rm -f ${WORKIMAGE}
  32. [ -e ${pidfile} ] && rm -f ${pidfile}
  33. [ -e ${ppidfile} ] && rm -f ${ppidfile}
  34. exit 0
  35. }
  36. trap error SIGINT SIGTERM SIGHUP SIGKILL SIGCHLD
  37. while [ -e ${pidfile} ];do
  38. VIDEOPGTIME=`ps ax | grep -v grep|grep ${VIDEOPG} | awk '{split($4,I,":");printf("%2d",I[2])}'`
  39. if [ "${VIDEOPGTIME}" -gt 10 ];then
  40. pid=`cat ${pidfile}`
  41. kill -HUP ${pid}
  42. pkill -HUP ${VIDEOPG}
  43. msleep 100
  44. rm -f ${pidfile}
  45. rm -f ${ppidfile}
  46. fi
  47. done
  48. while [ ! -z `pgrep ${VIDEOPG}` ];do
  49. TIMEOUT=`ps ax | grep -v grep|grep avconv | awk '{split($4,I,":");if (I[2] > 10){printf $1}}'`
  50. if [ ! -z "${TIMEOUT}" ];then
  51. kill -HUP ${TIMEOUT}
  52. msleep 100
  53. break
  54. fi
  55. done
  56. if [ -e ${DEV} ];then
  57. echo -n $$ >${pidfile}
  58. echo -n ${ppid} >${ppidfile}
  59. if [ $DEV != $RASPICAMERA ];then
  60. if [ ! -z "${OPT}" ];then
  61. ${VIDEOPG} -f video4linux2 -s 320x240 -i ${DEV} ${OPT} -y ${WORKIMAGE} >/dev/null 2>&1
  62. else
  63. ${VIDEOPG} -f video4linux2 -s 320x240 -i ${DEV} -y ${WORKIMAGE} >/dev/null 2>&1
  64. fi
  65. else
  66. if [ $MP4 != "YES" ];then
  67. $RASPISTILL -vf -hf -w 1920 -h 1080 -o ${WORKIMAGE} >/dev/null 2>&1
  68. else
  69. $RASPIVID -vf -hf -o - -w 640 -h 480 -t 10000 -fps 5 |${VIDEOPG} -f h264 -r 5 -i - -vcodec mpeg4 -y ${WORKIMAGE} >/dev/null 2>&1
  70. fi
  71. fi
  72. rm -f $pidfile
  73. rm -f ${ppidfile}
  74. else
  75. echo "Video device is not connected -> $DEV"
  76. fi