ระบบ Live Monitor Service (FFmpeg HLS)

คู่มือการติดตั้ง สถาปัตยกรรมระบบ และการควบคุม Service เบื้องหลังอัตโนมัติ

1 เตรียมความพร้อมและสิทธิ์ของโฟลเดอร์

สร้างพื้นที่สำหรับแปลงไฟล์ HLS (.m3u8 และ .ts) พร้อมตั้งค่าสิทธิ์ให้โปรเซสสามารถเขียนไฟล์ได้โดยไม่มีปัญหา Permission Denied

sudo mkdir -p /var/www/html/m3u8
sudo chown -R pptv:www-data /var/www/html/m3u8
sudo chmod -R 775 /var/www/html/m3u8

2 รายการช่องสัญญาณ (streams.txt)

สร้างไฟล์รายการสตรีมต้นทางไว้ที่ /home/pptv/streams.txt รองรับทั้ง RTMP, HTTP, และ UDP Multicast

nano /home/pptv/streams.txt

รูปแบบการกรอก: URL,ชื่อช่อง

rtmp://ip-server-cdn/cdn-edge-hls/_definst_/1fef-33a1-68d5-e065-54c2,mono29
http://172.30.60.21/BHQ/live/31.ts,onehd
udp://@239.1.1.100:50000?pkt_size=1316&fifo_size=5000000,true1
⚠️ คำเตือน: หากก๊อปปี้ไฟล์มาจากระบบ Windows ให้รันคำสั่ง sed -i 's/\r$//' /home/pptv/streams.txt เพื่อล้างอักขระ carriage return ท้ายบรรทัดออก

3 สร้างสคริปต์ควบคุม (live_monitor.sh)

สคริปต์นี้มีฟังก์ชัน Single-Instance Lock เพื่อป้องกันการรันซ้ำซ้อน และใช้โหมด Stream Copy เพื่อประหยัด CPU

cat << 'EOF' > /home/pptv/live_monitor.sh
#!/bin/bash

# ป้องกันการรันซ้ำซ้อนด้วย Lock file
exec 200>/tmp/live_monitor.lock
flock -n 200 || { echo "❌ สคริปต์กำลังทำงานอยู่แล้ว"; exit 1; }

HLS_OUTPUT_DIR="/var/www/html/m3u8"
CONFIG_FILE="/home/pptv/streams.txt"

mkdir -p "$HLS_OUTPUT_DIR"
echo "=== Starting Stream Monitor (Single Instance Mode) ==="

while true; do
    if [ ! -f "$CONFIG_FILE" ]; then
        sleep 10
        continue
    fi

    while IFS=',' read -r raw_url raw_name || [ -n "$raw_url" ]; do
        rtmp_url=$(echo "$raw_url" | tr -d '\r\n ' )
        stream_name=$(echo "$raw_name" | tr -d '\r\n ' )
        [[ -z "$rtmp_url" || "$rtmp_url" =~ ^# ]] && continue

        OUTPUT_PATH="$HLS_OUTPUT_DIR/$stream_name"
        mkdir -p "$OUTPUT_PATH"

        # ตรวจสอบ Process แบบเจาะจง
        if ! pgrep -f "ffmpeg.*$OUTPUT_PATH/index.m3u8" > /dev/null; then
            INPUT_OPTS="-rw_timeout 5000000"
            if [[ "$rtmp_url" =~ ^http:// || "$rtmp_url" =~ ^https:// ]]; then
                INPUT_OPTS="-reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 5 -timeout 5000000"
            fi

            (
                ffmpeg -nostdin $INPUT_OPTS \
                   -fflags +genpts -async 1 \
                   -avoid_negative_ts make_zero \
                   -i "$rtmp_url" \
                   -c:v copy -c:a copy \
                   -f hls \
                   -hls_time 4 \
                   -hls_list_size 5 \
                   -hls_flags delete_segments+append_list \
                   -hls_segment_filename "$OUTPUT_PATH/seq_%d.ts" \
                   "$OUTPUT_PATH/index.m3u8" > "$OUTPUT_PATH/ffmpeg.log" 2>&1
            ) &
            sleep 0.1
        fi
    done < "$CONFIG_FILE"

    sleep 10
done
EOF

chmod +x /home/pptv/live_monitor.sh

4 ติดตั้งเป็น Systemd Service (Auto-start on Boot)

สร้าง Service ควบคุมให้ระบบทำงานอัตโนมัติตอนเปิดเครื่อง และเปิดใหม่ทันทีหากโปรเซสหลักปิดตัวลง

sudo bash -c 'cat << "EOF" > /etc/systemd/system/live-monitor.service
[Unit]
Description=Live Stream FFmpeg Auto-Monitor Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/home/pptv/live_monitor.sh
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF'

sudo systemctl daemon-reload
sudo systemctl enable live-monitor.service
sudo systemctl start live-monitor.service

5 คำสั่งควบคุมและตรวจสอบการทำงาน

ตรวจสอบสถานะ Service:

sudo systemctl status live-monitor.service

ตรวจสอบจำนวนโปรเซส FFmpeg ที่กำลังแปลงสัญญาณ:

pgrep -c ffmpeg

ตรวจสอบ Log ของแต่ละช่อง (ตัวอย่างช่อง onehd):

cat /var/www/html/m3u8/onehd/ffmpeg.log