#!/bin/bash
# 推流到 Cloudflare Stream
RTSP_SOURCE="rtsp://localhost:8554/camera1"
VIDEO_SOURCE="$(dirname "$0")/videos/3.mp4"
CLOUDFLARE_URL="rtmps://live.cloudflare.com:443/live/0a37e82197ac851a92dc5451dfe78485kb51e49994b6fd9e56b6f1fdfcd339fe6"

echo "开始推流到 Cloudflare Stream..."
echo "源: $VIDEO_SOURCE"
echo "按 Ctrl+C 停止推流"
echo ""

ffmpeg \
  -rtsp_transport tcp \
  -i "rtsp://admin:Wxc767718929@192.168.0.64:554/Streaming/Channels/101" \
  -map 0:v:0 -map 0:a? \
  -c:v libx264 \
  -preset veryfast \
  -tune zerolatency \
  -profile:v baseline \
  -level 3.1 \
  -pix_fmt yuv420p \
  -g 30 -keyint_min 30 \
  -b:v 2500k -maxrate 2500k -bufsize 5000k \
  -c:a opus -strict -2 -ar 48000 -b:a 128k \
  -f whip "https://customer-pj89kn2ke2tcuh19.cloudflarestream.com/8c108b4025d3278b188b443e8a6c5503kb51e49994b6fd9e56b6f1fdfcd339fe6/webRTC/publish" \
  -headers "Authorization: Bearer ZrpMoQ15dCCe6rX0pKINzcb0eNdakSih-TmQrbq-"

# 使用本地视频文件推流（循环播放）
# ffmpeg -re -stream_loop -1 \
#   -i "$VIDEO_SOURCE" \
#   -c:v libx264 \
#   -preset veryfast \
#   -tune zerolatency \
#   -profile:v baseline \
#   -level 3.1 \
#   -g 60 -keyint_min 60 \
#   -pix_fmt yuv420p \
#   -c:a aac -ar 48000 -b:a 128k \
#   -f flv \
#   "$CLOUDFLARE_URL"

# 方案2: 使用 WHIP (需要 FFmpeg 6.1+ 且编译了 WHIP 支持)
# ffmpeg -rtsp_transport tcp \
#   -i "$RTSP_SOURCE" \
#   -c:v libx264 \
#   -preset veryfast \
#   -tune zerolatency \
#   -profile:v baseline \
#   -level 3.1 \
#   -g 30 -keyint_min 30 \
#   -pix_fmt yuv420p \
#   -c:a aac -ar 48000 -b:a 96k \
#   -f whip "https://customer-pj89kn2ke2tcuh19.cloudflarestream.com/8c108b4025d3278b188b443e8a6c5503kb51e49994b6fd9e56b6f1fdfcd339fe6/webRTC/publish" \
#   -headers "Authorization: Bearer ZrpMoQ15dCCe6rX0pKINzcb0eNdakSih-TmQrbq-"
