Files
iptv-live/check.sh
T
2026-11-28 01:56:25 +08:00

51 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# 探测 m3u 里的 IPv6 直播源能不能真播。用法: ./check.sh <m3u文件或URL>
# 判定: 拿到 #EXTM3U -> 再用 ffprobe 看到视频流才算能播(302 后 403 的算失败)
set -uo pipefail
src="$1"
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
case "$src" in
http*) curl -sSL -m 40 -o "$tmp/in.m3u" "$src" ;;
*) cp "$src" "$tmp/in.m3u" ;;
esac
sed -i 's/\r$//' "$tmp/in.m3u"
# name<TAB>url,只留走 IPv6 的条目:要么是 IPv6 字面量 "://[",要么域名有 AAAA 记录
awk '/^#EXTINF/{n=$0; sub(/.*,/,"",n); next}
/^http/ && !seen[$0]++ {print n"\t"$0}' "$tmp/in.m3u" > "$tmp/all.tsv"
: > "$tmp/pairs.tsv"
declare -A seen=() aaaa=()
while IFS=$'\t' read -r n u; do
if [[ "$u" == *"://["* ]]; then printf '%s\t%s\n' "$n" "$u" >> "$tmp/pairs.tsv"; continue; fi
h=${u#*://}; h=${h%%/*}; h=${h%%:*}
if [ -z "${seen[$h]:-}" ]; then
seen[$h]=1
nslookup -type=AAAA "$h" 223.5.5.5 2>/dev/null | grep -q '^Addresses\?:.*:' && aaaa[$h]=1 || aaaa[$h]=0
fi
[ "${aaaa[$h]}" = 1 ] && printf '%s\t%s\n' "$n" "$u" >> "$tmp/pairs.tsv"
done < "$tmp/all.tsv"
total=$(wc -l < "$tmp/pairs.tsv")
[ "$total" -eq 0 ] && { echo "没有 IPv6 条目:$src"; exit 1; }
probe() {
local n="$1" u="$2" body code
body=$(curl -g -sL -m 10 -A "okhttp/3.12.0" "$u" 2>/dev/null | head -c 400)
if ! grep -q '#EXTM3U' <<<"$body"; then echo -e "FAIL\tcurl\t$n\t$u"; return; fi
code=$(ffprobe -v error -rw_timeout 10000000 -select_streams v:0 \
-show_entries stream=codec_name,width,height -of csv=p=0 "$u" 2>&1 </dev/null | grep -v '^\[' | head -1)
if [[ "$code" =~ ^(h264|hevc|mpeg2video|av1|vp9) ]]; then echo -e "OK\t$code\t$n\t$u"
else echo -e "FAIL\tprobe\t$n\t$u"; fi
}
export -f probe
# 串行 + 间隔: 移动 OTT 服务器并发会限流成 403
( while IFS=$'\t' read -r n u; do printf '%s\t%s\n' "${n:-?}" "$u"; done < "$tmp/pairs.tsv" ) |
while IFS=$'\t' read -r n u; do probe "$n" "$u"; sleep 0.3; done > "$tmp/res.tsv"
echo "IPv6 条目 $total 条"
echo "能播 $(grep -c '^OK' "$tmp/res.tsv") 失败 $(grep -c '^FAIL' "$tmp/res.tsv")"
echo "--- 能播的 ---"; grep '^OK' "$tmp/res.tsv" | cut -f3,4 || echo "(无)"
echo "--- 失败原因 ---"; grep '^FAIL' "$tmp/res.tsv" | cut -f2 | sort | uniq -c