diff --git a/.build/prod/compose.yml b/.build/prod/compose.yml index dc2af36..c60d056 100644 --- a/.build/prod/compose.yml +++ b/.build/prod/compose.yml @@ -5,3 +5,9 @@ services: dockerfile: .build/prod/yt-restream.Dockerfile hostname: yt-restream image: git.shihaam.dev/sarlink/tv/yt-restream + m3u8-restream: + build: + context: ../../ + dockerfile: .build/prod/m3u8-restream.Dockerfile + hostname: m3u8-restream + image: git.shihaam.dev/sarlink/tv/m3u8-restream diff --git a/.build/prod/m3u8-restream.Dockerfile b/.build/prod/m3u8-restream.Dockerfile new file mode 100644 index 0000000..5c6860f --- /dev/null +++ b/.build/prod/m3u8-restream.Dockerfile @@ -0,0 +1,11 @@ +FROM debian:latest + +WORKDIR /root/ + +RUN apt update \ + && apt install -y python3 curl ffmpeg + +COPY m3u8-restream.sh . +WORKDIR /var/www/html + +CMD python3 -m http.server -d live/ & /root/m3u8-restream.sh diff --git a/m3u8-restream.sh b/m3u8-restream.sh new file mode 100755 index 0000000..f50a415 --- /dev/null +++ b/m3u8-restream.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +M3U8_STREAM_URL="${1:-$M3U8_STREAM_URL}" +SLUG="${2:-$SLUG}" +USER_AGENT="${3:-$USER_AGENT}" + +if [ -n "$M3U8_STREAM_URL" ] && [ -n "$SLUG" ]; then + : +else + echo "Error: M3U8 URL and SLUG required. Provide both as arguments or set M3U8_STREAM_URL and SLUG environment variables." + exit 1 +fi + +mkdir -p live + +if [ -n "$USER_AGENT" ]; then + ffmpeg -user_agent "$USER_AGENT" -i "$M3U8_STREAM_URL" -c copy -f hls -hls_time 6 -hls_list_size 10 -hls_flags delete_segments "live/$SLUG.m3u8" +else + ffmpeg -i "$M3U8_STREAM_URL" -c copy -f hls -hls_time 6 -hls_list_size 10 -hls_flags delete_segments "live/$SLUG.m3u8" +fi diff --git a/yt-restream.sh b/yt-restream.sh index 82545f6..fd08f8c 100755 --- a/yt-restream.sh +++ b/yt-restream.sh @@ -1,20 +1,21 @@ #!/bin/bash - -if [ -n "$1" ]; then - CHANNEL_ID="$1" -elif [ -n "$CHANNEL_ID" ]; then - CHANNEL_ID="$CHANNEL_ID" +CHANNEL_ID="${1:-$CHANNEL_ID}" +SLUG="${2:-$SLUG}" +if [ -n "$CHANNEL_ID" ]; then + : else echo "Error: No channel ID provided. Use as argument or set CHANNEL_ID environment variable." exit 1 fi -while true; do - VIDEO_ID=$(curl -sL https://www.youtube.com/@$CHANNEL_ID/streams | grep "Tap to watch live" | sed 's/.*\/watch?v=\([^\\]*\).*/\1/') - M3U8_STREAM_URL=$(yt-dlp -g -f b $(echo https://www.youtube.com/watch?v=$VIDEO_ID)) +# Use SLUG if provided, otherwise use CHANNEL_ID as slug +SLUG="${SLUG:-$CHANNEL_ID}" - mkdir -p live - timeout 21555 ffmpeg -i $M3U8_STREAM_URL -c copy -f hls -hls_time 6 -hls_list_size 10 -hls_flags delete_segments live/$CHANNEL_ID.m3u - rm -rf live/* +while true; do + VIDEO_ID=$(curl -sL "https://www.youtube.com/@$CHANNEL_ID/streams" | grep "Tap to watch live" | sed 's/.*\/watch?v=\([^\\]*\).*/\1/') + M3U8_STREAM_URL=$(yt-dlp -g -f b "https://www.youtube.com/watch?v=$VIDEO_ID") + mkdir -p "live/$SLUG" + timeout 21555 ffmpeg -i "$M3U8_STREAM_URL" -c copy -f hls -hls_time 6 -hls_list_size 10 -hls_flags delete_segments "live/$SLUG/live.m3u8" + rm -rf "live/$SLUG"/* done