Live youtube video from your CCTV (DVR) by means of FFMPEG (Centos 7)

Discussion in 'Programming/Scripts' started by Alex Mamatuik, Dec 9, 2022.

  1. Alex Mamatuik

    Alex Mamatuik Member

    1. cd /
    2. mkdir REPOSITORY && cd REPOSITORY
    3. mkdir DVR && cd DVR
    4. (bash script: dvr_to_youtube.sh) nano dvr_to_youtube.sh
      #!/bin/bash
      ######### Lock checker ###########

      myName="`echo $0 | awk '{print $NF}' FS='/'`"

      lockDir="/var/lock/"

      lockFile=$lockDir$myName.pid

      currentPID=$$

      oldPID="`cat $lockFile`"

      oldderExist=` kill -0 $oldPID 2>/dev/null ; echo $? `

      if [ "$oldderExist" == "0" ]; then echo "An Other Instance Is Running...! PID:$oldPID" ; exit ; else echo $currentPID > $lockFile ; fi

      ######### Lock checker ###########

      while true
      do
      if [ -z "$1" ]; then
      RTSPIP=<my_opened_ip_to_connect_with_camera>
      USER=<name>;
      PASS=<password>
      RTSPPORT=<enabled_port>
      else
      IP_ADDRESS=$1
      fi

      RTSPNAME=camera2
      DIR=/REPOSITORY/DVR
      URL=https://a.upload.youtube.com/http_upload_hls
      STREAM=<your_key_from_youtu.be>

      # FFMPEG command for creating segments from RTSP stream
      ffmpeg -rtsp_transport tcp -i rtsp://$USER:$PASS@$RTSPIP:$RTSPPORT/trackID=2 -f lavfi -i anullsrc -c:v copy -c:a aac -map 0:v -map 1 -f hls -hls_time 4 -hls_playlist_type event -method PUT 'https://a.upload.youtube.com/http_upload_hls?cid=$STREAM&copy=0&file=stream.m3u8'
      sleep 5
      done


    5. (making service) nano dvr_youtube.service
      [Unit]
      Description=DVR to YOUTUBE

      [Service]
      ExecStart=/REPOSITORY/DVR/dvr_to_youtube.sh
      Restart=on-failure

      [Install]
      WantedBy=multi-user.target
    6. chmod +x /REPOSITORY/DVR/dvr_to_youtube.sh
    7. ln -s /REPOSITORY/DVR/dvr_youtube.service /lib/systemd/system/dvr_youtube.service
    8. systemctl enable dvr_youtube.service
    9. systemctl start dvr_youtube.service
    10. systemctl status dvr_youtube.service
     

Share This Page