dtype.org
Log in

Pi1

From dtype.org

systemd services

autossh.service

[Unit]
Description=AutoSSH tunnel service
After=network.target

[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 110" -o "ServerAliveCountMax 3" -o ExitOnForwardFailure=yes -N -i /root/.ssh/id_rsa -R "[::]:62222:localhost:22" r@52.45.246.197
Restart=always

[Install]
WantedBy=multi-user.target

ffmpeg-driveway.service

[Unit]
Description=ffmpeg driveway feed
After=network.target

[Service]
User=drew
Group=drew
ExecStart=/var/www/html/driveway/ffmpeg.sh
Restart=always

[Install]
WantedBy=multi-user.target

ffmpeg-live.service

[Unit]
Description=ffmpeg driveway feed live
After=network.target

[Service]
User=drew
Group=drew
ExecStart=/home/drew/live_stream.sh

[Install]
WantedBy=multi-user.target

service supporting scripts

/var/www/html/driveway/ffmpeg.sh

#!/bin/bash
cd /var/www/html/driveway/
/usr/bin/ffmpeg -loglevel quiet -i rtsp://192.168.42.109:554/CH001.sdp?tcp -timelimit 86400 -c copy -f segment -segment_time 600 -segment_atclocktime 1 -reset_timestamps 1 -strftime 1 "driveway-%Y%m%d-%H%M%S.mp4"

/home/drew/live_stream.sh

#!/bin/bash
ffmpeg -i rtsp://192.168.42.109:554/CH001.sdp?tcp -f lavfi -i anullsrc -c:v copy -c:a aac -t 5:00 -f flv rtmp://a.rtmp.youtube.com/live2/YOUTUBEKEY

sqs-consumer.sh

#!/usr/bin/python3 -u
import boto3
import os
import time

POLLSLEEP = 1
POLLTIME = 20

sqs = boto3.resource('sqs')
snsclient = boto3.client('sns')

queue = sqs.get_queue_by_name(QueueName='host-pi1')

while True:
    for message in queue.receive_messages(
        MessageAttributeNames=['command'],
        MaxNumberOfMessages=1,
        WaitTimeSeconds=POLLTIME
    ):
        command = ''
        print("Received message")
        print(message.message_attributes)
        if message.message_attributes is not None:
            command = message.message_attributes.get('command').get('StringValue')
        if command == 'ffmpeg-live':
            print("Executing service ffmpeg-live start")
            os.system("service ffmpeg-live start")
        message.delete()
        response = snsclient.publish(
            TargetArn="arn:aws:sns:us-east-1:826233342239:emaildrew",
            Message="SQS message received by pi1!"
        )
    time.sleep(POLLSLEEP)