[FFmpeg-user] Autolaunch FFMPEG when network traffic detected

robertlazarski robertlazarski at gmail.com
Mon Jul 23 19:18:52 EEST 2018


On Mon, Jul 23, 2018 at 9:34 AM, yannickb <ybarbeaux+ffmpeg at gmail.com>
wrote:

> I wish I could monitor each of those ports and launch the corresponding
> command automatically as soon as the incoming stream is detected (and stop
> when the stream ends)
>
>
>
One advantage of using a Linux distro like Debian as you mention, is that
you can run each instance of a program in a separate account i.e. user1,
user2 etc for each instance of ffmpeg in this example.

That way its easier to keep of udp ports open and running processes by
userid / group id. You can also use bash lock files per user to maintain
only one open ffmpeg instance.

UDP has a few quirks in my experience. You can use this command to see the
processes using udp.

lsof -i udp

For keeping track of open ffmpeg processes and UDP ports, here is a quick
few lines of bash code ...

#!/bin/bash

uid=`id -u $shell_user `
gid=`id -g $shell_user `
process_count=`ps -ef --Group $gid --User $uid | grep -v grep | grep ffmpeg
| wc -l `
if [[ $process_count -gt 0 ]]; then
    printf "\n\nffmpeg is in progress\n\n"
else
    printf "\n\nffmpeg is not in progress\n\n"
fi

port=999
serviceIsRunning=false
openPorts=$(/bin/netstat -tulpn | grep -vE '^Active|Proto' | awk '{ print
$4}' | awk -F: '{print $NF}' | sed '/^$/d' | sort -u)
for openPort in $openPorts
do
    if [[ "$port" == "$openPort" ]]; then
        serviceIsRunning=true
        echo "service is running."
        break
    else
        printf "\n\nfound open UDP: $openPort that is not port: $port "
    fi
done
if [ $serviceIsRunning == false ]
then
    echo "service is not running."
fi

Best regards,
Robert


More information about the ffmpeg-user mailing list