[FFmpeg-user] HLS issue
Jamey Kirby
jkirby at whupfm.org
Fri Nov 29 18:29:30 EET 2024
I am a developer of a PWA streaming app for our radio station. I am using
FFmpeg to create the live HLS stream. It works great except when the source
goes down. FFmpeg never updates the .m3u8 or .ts files when the source
returns online. It just keeps streaming the same old files over and over
again.
If I switch to using an ICY server, everything works as expected.
We want to use HLS. Here is how I am using FFmpeg in my script:
#!/bin/bash
# Configuration
# STREAM_URI="http://audio-ice2.ibiblio.org:8000/whup-128k"
STREAM_URI="tcp://0.0.0.0:54321?listen"
HLS_DIRECTORY="/var/www/whupfm.media/html/hls"
HLS_SEGMENT_TIME=2
HLS_LIST_SIZE=3
HLS_DELETE_THRESHOLD=4
RETRY_INTERVAL=2
# Metadata
METADATA_HIGH="WHUP 104.7 FM Live Stream (192k)"
METADATA_MID="WHUP 104.7 FM Live Stream (128k)"
METADATA_LOW="WHUP 104.7 FM Live Stream (64k)"
# The cleanup function is to be called on script exit.
cleanup() {
echo "$(date) - Cleaning up HLS files..."
find "${HLS_DIRECTORY}" -type f \( -name '*.ts' -o -name '*.m3u8' \)
-delete
}
cleanup_on_exit() {
echo "$(date) - Exiting script..."
cleanup
}
# Clear the HLS working directory. The folder may have contents
# if the system loses power or there is a kernel exception
# resulting in the trap not being called.
cleanup
# Trap signals to run the cleanup function on script exit.
trap cleanup_on_exit SIGINT SIGTERM EXIT
while true
do
# Run FFmpeg.
echo "$(date) - Starting FFmpeg..."
ffmpeg -v quiet -loglevel error -f mp3 -i "${STREAM_URI}" -ar 44100 -ab
192k \
-reconnect 1 -reconnect_streamed 1 -reconnect_at_eof 1
-reconnect_delay_max 2 \
-rw_timeout 4000000 \
-map 0 -c:a copy -metadata title="${METADATA_HIGH}"
-strftime 1 -f hls -hls_allow_cache 0 -hls_start_number_source datetime
-hls_flags delete_segments+append_list -hls_time "${HLS_SEGMENT_TIME}"
-hls_list_size "${HLS_LIST_SIZE}" -hls_delete_threshold
${HLS_DELETE_THRESHOLD} -hls_segment_filename
"${HLS_DIRECTORY}/high_%Y%m%d%H%M%S.ts" "${HLS_DIRECTORY}/high.m3u8" \
-map 0 -c:a aac -b:a "128k" -metadata title="${METADATA_MID}"
-strftime 1 -f hls -hls_allow_cache 0 -hls_start_number_source datetime
-hls_flags delete_segments+append_list -hls_time "${HLS_SEGMENT_TIME}"
-hls_list_size "${HLS_LIST_SIZE}" -hls_delete_threshold
${HLS_DELETE_THRESHOLD} -hls_segment_filename
"${HLS_DIRECTORY}/mid_%Y%m%d%H%M%S.ts" "${HLS_DIRECTORY}/mid.m3u8" \
-map 0 -c:a aac -b:a "64k" -metadata title="${METADATA_LOW}"
-strftime 1 -f hls -hls_allow_cache 0 -hls_start_number_source datetime
-hls_flags delete_segments+append_list -hls_time "${HLS_SEGMENT_TIME}"
-hls_list_size "${HLS_LIST_SIZE}" -hls_delete_threshold
${HLS_DELETE_THRESHOLD} -hls_segment_filename
"${HLS_DIRECTORY}/low_%Y%m%d%H%M%S.ts" "${HLS_DIRECTORY}/low.m3u8"
echo "$(date) - FFmpeg stopped."
cleanup
sleep 2
done
# There's no need for an explicit cleanup call because of a trap.
-- Jamey
More information about the ffmpeg-user
mailing list