sneaky4oe

Orange Pi Zero 2W ffmpeg streaming with TPlink Tapo C110

Sep 29th, 2025 (edited)
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.80 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. set -u
  4.  
  5. RTSP_URL="rtsp://*****:******@*******:554/stream1"
  6. RTMP_URL="rtmp://live.restream.io/live/re_**********"
  7. MUBERT_URL="https://stream.mubert.com/b2b/v2?playlist=6.4.3&****************"
  8.  
  9.  
  10. # Defaults for prompts (can override via env)
  11. STREAM_DAYS="${STREAM_DAYS:-0}"
  12. STREAM_HOURS="${STREAM_HOURS:-0}"
  13. STREAM_MINS="${STREAM_MINS:-1}"
  14. # ========================================================
  15.  
  16. SCRIPT_PATH="$0"
  17. SCRIPT_DIR="$(cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd)"
  18.  
  19. LOG_FILE="$HOME/stream.log"   # live == persistent (same file, like your original)
  20. CUR_LOG="$LOG_FILE"
  21.  
  22. # Colors (green = view logs, red = kill)
  23. if command -v tput >/dev/null 2>&1 && [ -t 1 ]; then
  24.  C_RED="$(tput setaf 1)"; C_GREEN="$(tput setaf 2)"; T_BOLD="$(tput bold)"; T_RESET="$(tput sgr0)"
  25. else
  26.  C_RED=""; C_GREEN=""; T_BOLD=""; T_RESET=""
  27. fi
  28.  
  29. # Pick a writable place for helper
  30. if [ -w "$SCRIPT_DIR" ]; then
  31.  LOG_HELPER="$SCRIPT_DIR/stream-log"
  32. else
  33.  LOG_HELPER="$HOME/stream-log"
  34. fi
  35.  
  36. # ---------- Preflight: only kill prior ffmpeg we named ----------
  37. pkill -f 'ffmpeg stream' 2>/dev/null || true
  38. sleep 0.2
  39. pkill -9 -f 'ffmpeg stream' 2>/dev/null || true
  40.  
  41. # ---------- Prompts ----------
  42. echo "--- Stream Duration ---"
  43. read -p "Enter duration in DAYS (default: ${STREAM_DAYS}): " DAYS_INPUT || true
  44. read -p "Enter duration in HOURS (default: ${STREAM_HOURS}): " HOURS_INPUT || true
  45. read -p "Enter duration in MINUTES (default: ${STREAM_MINS}): " MINS_INPUT || true
  46.  
  47. DAYS=${DAYS_INPUT:-$STREAM_DAYS}
  48. HOURS=${HOURS_INPUT:-$STREAM_HOURS}
  49. MINS=${MINS_INPUT:-$STREAM_MINS}
  50.  
  51. for vname in DAYS HOURS MINS; do
  52.  vval="${!vname}"
  53.  [[ "$vval" =~ ^[0-9]+$ ]] || { echo "Invalid ${vname}: ${vval}"; exit 1; }
  54. done
  55.  
  56. TOTAL_SECONDS=$((DAYS*86400 + HOURS*3600 + MINS*60))
  57. [ "$TOTAL_SECONDS" -le 0 ] && { echo "Error: Total duration must be greater than zero."; exit 1; }
  58.  
  59. # ---------- Prepare log ----------
  60. : > "$LOG_FILE"
  61.  
  62. # ---------- Helper to tail logs (viewer only) ----------
  63. cat > "$LOG_HELPER" <<'EOF'
  64. #!/bin/bash
  65. CUR_LOG="$HOME/stream.log"
  66. if command -v tput >/dev/null 2>&1 && [ -t 1 ]; then
  67.  C_GREEN="$(tput setaf 2)"; T_BOLD="$(tput bold)"; T_RESET="$(tput sgr0)"
  68. else
  69.  C_GREEN=""; T_BOLD=""; T_RESET=""
  70. fi
  71. echo "${C_GREEN}${T_BOLD}Live log:${T_RESET}   $CUR_LOG"
  72. echo
  73. echo "Press Ctrl+C to stop VIEWING the logs (the stream continues)."
  74. exec tail -F "$CUR_LOG"
  75. EOF
  76. chmod +x "$LOG_HELPER" 2>/dev/null || true
  77.  
  78. # ---------- Info ----------
  79. echo
  80. echo "IMPORTANT: This stream ignores hotkeys (Ctrl+C / Ctrl+Z) and will detach now."
  81. echo "Stop immediately:        ${C_RED}${T_BOLD}pkill -f 'ffmpeg stream'${T_RESET}"
  82. echo
  83. echo "How to monitor and control this stream:"
  84. echo "    ${C_GREEN}${T_BOLD}    • View logs now:${T_RESET}           $LOG_HELPER    (recommended; opens tail immediately)"
  85. echo "    ${C_GREEN}${T_BOLD}    • View live log only:${T_RESET}      tail -F \"$CUR_LOG\""
  86. echo "        • Check active process:    ps ax | grep 'ffmpeg stream'"
  87. echo
  88. echo "    ${C_RED}${T_BOLD}TO STOP IMMEDIATELY:${T_RESET}  pkill -f 'ffmpeg stream'"
  89. echo
  90. echo "Stream will run for ${DAYS} day(s), ${HOURS} hour(s), and ${MINS} minute(s)."
  91. echo
  92. echo "Detaching now. All further output goes to $LOG_FILE."
  93.  
  94. # ---------- Launch stream (double-fork + nohup) ----------
  95. # - Keeps your original ffmpeg line, adding only safe thread queues and HTTP reconnects.
  96. # - Removed unsupported -rw_timeout (that caused your error).
  97. (
  98.  exec </dev/null >/dev/null 2>&1
  99.  nohup bash -c "exec -a 'ffmpeg stream' timeout ${TOTAL_SECONDS}s ffmpeg \
  100.     -rtsp_transport tcp -fflags nobuffer \
  101.     -use_wallclock_as_timestamps 1 -fflags +genpts \
  102.     -thread_queue_size 512 -i \"$RTSP_URL\" \
  103.     -reconnect 1 -reconnect_streamed 1 -reconnect_at_eof 1 \
  104.     -reconnect_on_network_error 1 -reconnect_on_http_error 4xx,5xx -reconnect_delay_max 2 \
  105.     -thread_queue_size 512 -i \"$MUBERT_URL\" \
  106.     -map 0:v:0 -map 1:a:0 \
  107.     -fps_mode passthrough -copytb 1 \
  108.     -c:v copy \
  109.     -c:a aac -b:a 128k -ar 44100 -ac 2 \
  110.     -muxpreload 0 -muxdelay 0 \
  111.     -f flv \"$RTMP_URL\"" >> "$LOG_FILE" 2>&1 &
  112.  disown
  113. ) &
  114.  
  115. # ---------- Tail viewer (Ctrl+C stops viewing only) ----------
  116. exec "$LOG_HELPER"
  117.  
  118.  
  119.  
  120. ## #!/bin/bash
  121. ## # Companion log viewer for stream-x1c. Tails both live and error logs.
  122. ##
  123. ## CUR_LOG="$HOME/stream.current.log"
  124. ## LOG_FILE="$HOME/stream.log"
  125. ##
  126. ## # Optional green headings if interactive
  127. ## if command -v tput >/dev/null 2>&1 && [ -t 1 ]; then
  128. ##   C_GREEN="$(tput setaf 2)"; T_BOLD="$(tput bold)"; T_RESET="$(tput sgr0)"
  129. ## else
  130. ##   C_GREEN=""; T_BOLD=""; T_RESET=""
  131. ## fi
  132. ##
  133. ## # Follow both files, even if rotated/recreated
  134. ## exec tail -F "$CUR_LOG" "$LOG_FILE"
  135. ########### make stream-log file with this content without ## comments for block above for this to work in /usr/local/bin
Advertisement