Skip to content

Instantly share code, notes, and snippets.

@yalov
Forked from John07/HLS_dvr.sh
Last active January 25, 2022 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save yalov/a8818cbbedadc5b5db03 to your computer and use it in GitHub Desktop.
Save yalov/a8818cbbedadc5b5db03 to your computer and use it in GitHub Desktop.
A small bash script to make ffmpeg recording HLS streams (m3u8).
#!/bin/bash
for i in {1..168} # week
do
echo "ITERATION $i"
seconds=3600 #hour
# Date format for the recording file name
DATE=`date "+%y%m%d_%H%M%S"`
# start ffmpeg recording
ffmpeg -loglevel warning -hide_banner -re -i http://1.2.3.4/mystream.m3u8 -c copy -bsf:a aac_adtstoasc $DATE.mp4 &
# notification that recording has started
if [ "$(pgrep -P $$ 'ffmpeg')" ]
then echo -e "is recording now"
else echo -e "is not recording!" exit 42
fi
# check every seconds for $seconds to make sure ffmpeg is still running
START=`date +%s`
while [ $(( $(date +%s) - $seconds )) -lt $START ]; do
if [ -z "$(pgrep -P $$ 'ffmpeg')" ]
then
echo -e "is no longer running\n"
break
else
echo -e "steel running"
fi
sleep 60
done
echo -e "recording finished"
# stop ffmpeg (using this because stopping ffmpeg via -t for duration turned out to be extremely unreliable)
kill $(pgrep -P $$ 'ffmpeg')
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment