You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/iptv

95 lines
2.2 KiB

#!/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"
m3u_url_file="$config_path/m3u_url"
tmp_playlist="/tmp/iptvplaylist"
player_pid_file="/tmp/iptvplayerpid"
mkdir -p "$config_path"
usage() {
cat <<EOF
usage: iptv [M3U_URL]
[M3U_URL] URL to a M3U playlist, required on first run
--help Display help
EOF
}
save_channels() {
m3u_url=$(cat "$m3u_url_file")
printf "\nLoading channels... "
curl -s "$m3u_url" | grep EXTINF: -A 2 > $tmp_playlist
printf "Done!\n"
printf "Parsing channels... "
channels=()
url=""
while IFS= read -r line; do
if [[ "$line" =~ tvg-name=\"([^\"]+)\" || "$line" =~ tvg-id=\"([^\"]+)\" ]]; then
name="${BASH_REMATCH[1]}"
url=""
elif [[ "$line" == http* ]]; then
url="$line"
channels+=("$name [CH:${#channels[@]}] url:$url")
fi
done < "$tmp_playlist"
printf "Done!\n"
printf "%s\n" "${channels[@]}" > $channels_file
}
if [ -z "$m3u" ] && [ ! -s "$channels_file" ]; then
usage
exit 1
fi
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage
exit 0
fi
if [ ! -z "$m3u" ]; then
echo "$m3u" > $m3u_url_file
save_channels
echo "Playlist saved. Now run iptv again without a M3U URL."
exit
fi
if [[ $(find "$channels_file" -mtime +1) ]]; then
printf "Doing a daily update of channels..\n"
save_channels
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 -oE 'url:(.*)' | sed 's/url://' | 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"
if [ -f "$player_pid_file" ]; then
player_pid=$(cat "$player_pid_file")
if kill -0 "$player_pid" >/dev/null 2>&1; then
kill "$player_pid"
fi
fi
mpv "$selected_channel_url" > /dev/null 2>&1 &
echo $! > "$player_pid_file"