Last active
May 21, 2024 00:52
-
-
Save NB-XX/93b99375644934b596e04e72e29578a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # 推流入口 | |
| start_push() { | |
| local url="$1" | |
| local output | |
| ffmpeg -i "$url" -c:v copy -c:a aac -b:a 320k -ar 44100 -strict -2 -f flv "<推流地址>" 2>&1 | |
| } | |
| # 监测推流质量 | |
| monitor_stream() { | |
| local output="$1" | |
| local count=0 | |
| local restart_count=0 | |
| # 监测推流质量 | |
| while read -r line; do | |
| echo "$line" | |
| if [[ $line =~ speed=([0-9.]+) ]]; then | |
| local speed=${BASH_REMATCH[1]} | |
| echo "speed=$speed" | |
| if (( $(bc <<< "$speed < 0.9") )); then | |
| ((restart_count++)) | |
| echo "检测到推流质量不佳,已重启推流 ($restart_count)" | |
| if ((restart_count >= 10)); then | |
| echo "连续重启推流次数超过阈值,结束推流" | |
| break | |
| else | |
| echo "重新开始推流..." | |
| pkill -x ffmpeg | |
| sleep 2 | |
| start_push_and_monitor "$url" | |
| return | |
| fi | |
| fi | |
| fi | |
| if [[ $line =~ skipping\ [0-9]+\ segments\ ahead,\ expired\ from\ playlists ]]; then | |
| ((count++)) | |
| if ((count >= 3)); then | |
| echo "连续出现警告次数超过阈值,结束推流" | |
| break | |
| fi | |
| fi | |
| done <<< "$output" | |
| } | |
| # 开始推流和监测推流质量 | |
| start_push_and_monitor() { | |
| local url="$1" | |
| # 开始推流 | |
| start_push "$url" | monitor_stream | |
| } | |
| # 获取正在直播的信息(url的参数可以参考文档,这里默认只获取hololive的直播) | |
| curl --request GET \ | |
| --url 'https://holodex.net/api/v2/live?status=live&type=stream&org=Hololive' \ | |
| --header 'Accept: application/json' \ | |
| --header 'X-APIKEY: <holodex的apikey>' > temp.json | |
| # 输出待选直播 | |
| titles=$(jq -r '.[].title' temp.json) | |
| count=1 | |
| echo "正在直播:" | |
| while IFS= read -r title; do | |
| start_actual=$(jq -r --argjson count "$count" '.[$count - 1] | .start_actual' temp.json) | |
| start_timestamp=$(date -d "$start_actual" +%s) | |
| current_timestamp=$(date +%s) | |
| time_diff=$((current_timestamp - start_timestamp)) | |
| if (( time_diff < 60 )); then | |
| echo "$count. ${title} 直播已开始 $time_diff秒" | |
| elif (( time_diff < 3600 )); then | |
| minutes=$(( time_diff / 60 )) | |
| echo "$count. ${title} 直播已开始 $minutes分钟" | |
| else | |
| hours=$(( time_diff / 3600 )) | |
| minutes=$(( (time_diff % 3600) / 60 )) | |
| echo "$count. ${title} 直播已开始 ${hours}小时${minutes}分钟" | |
| fi | |
| ((count++)) | |
| done <<< "$titles" | |
| # 等待用户输入序号 | |
| read -p "请输入序号: " input | |
| selected_id=$(jq -r --argjson input "$input" '.[$input - 1] | .id' temp.json) | |
| y_url="https://www.youtube.com/watch?v=$selected_id" | |
| # 清理临时文件 | |
| rm temp.json | |
| # 使用yt-dlp获取m3u8并调用推流 | |
| m3u8=$(yt-dlp -g "$y_url") | |
| start_push_and_monitor "$m3u8" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment