iptv/iptv
2023-03-19 21:30:18 +01:00

75 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
command -v fzf >/dev/null 2>&1 || { echo >&2 "fzf required but not installed"; exit 1; }
command -v mpv >/dev/null 2>&1 || { echo >&2 "mpv required but not installed"; exit 1; }
m3u=$1
config_path="$HOME/.config/iptv/"
channels_file="$config_path/channels"
tmp_playlist="/tmp/iptvplaylist"
player_pid_file="/tmp/iptvplayerpid"
mkdir -p "$config_path"
if [ -z "$m3u" ] && [ ! -s "$channels_file" ]; then
echo "No M3U playlist has been configured. Run with: iptv http://domain/playlist.m3u"
exit 1
fi
if [ ! -z "$m3u" ]; then
printf "\nLoading channels... "
curl -s $m3u | grep EXTINF: -A 2 > $tmp_playlist
printf "Done!\n"
printf "Parsing channels... "
channels=()
readarray -t lines < "$tmp_playlist"
for (( i=0; i<${#lines[@]}; i+=3 )); do
line1="${lines[i]}"
line2="${lines[i+1]}"
line3="${lines[i+2]}"
if [[ "$line2" == http* ]]; then
url="$line2"
elif [[ "$line3" == http* ]]; then
url="$line3"
fi
if [[ "$line1" =~ tvg-name=\"([^\"]+)\" ]]; then
name="${BASH_REMATCH[1]}"
channels+=("$name [CH:$i] url:$url")
elif [[ "$line1" =~ tvg-id=\"([^\"]+)\" ]]; then
name="${BASH_REMATCH[1]}"
channels+=("$name [CH:$i] url:$url")
fi
done
printf "Done!\n"
printf "%s\n" "${channels[@]}" > $channels_file
echo "Playlist saved. Now run iptv again without a M3U URL."
exit
fi
selected=$(cat "$channels_file" | sed 's/ [^ ]*$//' | fzf)
if [ ! -n "$selected" ]; then
exit 1
fi
selected_channel=$(echo "$selected" | sed 's/.*\(\[CH:[0-9]\+\]\).*/\1/')
selected_channel_line=$(cat "$channels_file" | grep -F "$selected_channel")
selected_channel_url=$(echo "$selected_channel_line" | grep -oP 'url:\K.*' | tr -d '\r')
selected_channel_name=$(echo "$selected_channel_line" | sed 's/\(.*\) url:.*/\1/')
printf "Playing %s from %s" "$selected_channel_url" "$selected_channel_name"
player_pid=$(cat "$player_pid_file")
if kill -0 "$player_pid" >/dev/null 2>&1; then
kill "$player_pid"
fi
mpv "$selected_channel_url" > /dev/null 2>&1 &
echo $! > "$player_pid_file"