Skip to content

Instantly share code, notes, and snippets.

@John07

John07/HLS_dvr.sh

Last active Jan 21, 2021
Embed
What would you like to do?
A small script to make recording http live streams (HLS, those streams that work on iOS devices) nicer on a Mac. Script records the stream for a defined period of time and sends the user notifications if anything goes wrong and once it's done.
# required: ffmpeg (e.g. from homebrew), terminal-notifier from https://github.com/alloy/terminal-notifier
# you can schedule this with launchd to run e.g. weekly
# Specify in seconds how long the script should record (default here is 1 hour).
seconds=3600
# Date format for the recording file name
DATE=`date "+%d-%m-%y_%H-%M"`
# start ffmpeg recording
ffmpeg -re -i http://website.com/playlist.m3u8 -c copy -bsf:a aac_adtstoasc recording_$DATE.mp4 &
# notification that recording has started
if [ "$(pgrep -P $$ 'ffmpeg')" ]
then
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -title 'ffmpeg' -message "is recording now" -sender 'com.apple.Terminal'
else
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -title 'ffmpeg' -message "is not recording!" -sound Funk -sender 'com.apple.Terminal'
exit 42
fi
# check every 30 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
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -title 'ffmpeg' -message "is no longer running" -sound Funk -sender 'com.apple.Terminal'
fi
sleep 30
done
# notification when time is up
/Applications/terminal-notifier.app/Contents/MacOS/terminal-notifier -title 'ffmpeg' -message "recording finished" -sound default -sender 'com.apple.Terminal'
# stop ffmpeg (using this because stopping ffmpeg via -t for duration turned out to be extremely unreliable)
kill $(pgrep -P $$ 'ffmpeg')
@jinpark

This comment has been minimized.

Copy link

@jinpark jinpark commented Dec 12, 2014

Thanks a lot! Saved me a bunch of time :)

@alexose

This comment has been minimized.

Copy link

@alexose alexose commented Mar 10, 2017

This is great! Thanks.

@moloku

This comment has been minimized.

Copy link

@moloku moloku commented Apr 5, 2019

Thanks for the idea!
Instead of "kill" you could use ffmpeg's -t for time limits.

@varenc

This comment has been minimized.

Copy link

@varenc varenc commented Jun 4, 2020

using ffmpeg -re real time option will eventually results in packet loss here. That option is mainly for outputing live streams not recording them. With that enabled ffmpeg ends up recording just a bit slower than 1x and eventually it think it's farther behind in the stream then the packets the streaming serving still has in its buffer. The docs touch on this

@moloku see the comment on the use of kill :-)

# stop ffmpeg (using this because stopping ffmpeg via -t for duration turned out to be extremely unreliable)

a better option to use is -timelimit limits the amount of time ffmpeg has been running ( not the time in the stream)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment