pepomp4ctl

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

  1. #!/bin/bash
  2. # licence GPLv2 ; this scripts designed by IZAMUKARERA 2013.10.12 update 2020.7.27
  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="-ss 3 -s 320x240 -r 5 -vcodec mpeg4 -t 10"
  22. else
  23. OPT="-ss 3 -s 640x480"
  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. ${VIDEOPG} -f video4linux2 -i ${DEV} ${OPT} -y ${WORKIMAGE} >/dev/null 2>&1
  61. else
  62. if [ $MP4 != "YES" ];then
  63. $RASPISTILL -t 1000 -vf -hf -w 1920 -h 1080 -o ${WORKIMAGE} >/dev/null 2>&1
  64. else
  65. # $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
  66. $RASPIVID -vf -hf -o ${WORKIMAGE}.tmp -w 640 -h 480 -t 10000 -fps 5
  67. cat ${WORKIMAGE}.tmp |${VIDEOPG} -f h264 -r 5 -i - -vcodec mpeg4 -y ${WORKIMAGE} >/dev/null 2>&1
  68. rm ${WORKIMAGE}.tmp
  69. fi
  70. fi
  71. rm -f $pidfile
  72. rm -f ${ppidfile}
  73. else
  74. echo "Video device is not connected -> $DEV"
  75. fi