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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
import subprocess
|
| 7 |
+
from flask import Flask, render_template_string, request, redirect, url_for, jsonify, send_from_directory
|
| 8 |
+
from werkzeug.utils import secure_filename
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
app.config['UPLOAD_FOLDER'] = '/app/uploads'
|
| 12 |
+
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB max file size
|
| 13 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 14 |
+
os.makedirs('/app/hls', exist_ok=True)
|
| 15 |
+
|
| 16 |
+
CONFIG_FILE = '/app/stream_config.json'
|
| 17 |
+
|
| 18 |
+
DEFAULT_CONFIG = {
|
| 19 |
+
"input_stream": "http://10k.lucastv.pro/2c99755ae255/gJnMAT2/465825",
|
| 20 |
+
"channel_name": "Antv Live Stream",
|
| 21 |
+
"ad_enabled": False,
|
| 22 |
+
"ad_file": "",
|
| 23 |
+
"ad_position": "top_right", # top_left, top_right, bottom_left, bottom_right, center
|
| 24 |
+
"ad_width": 250,
|
| 25 |
+
"mode": "periodic", # always, periodic, schedule
|
| 26 |
+
"period_interval": 300, # every 300 seconds (5 mins)
|
| 27 |
+
"period_duration": 30, # show for 30 seconds
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def load_config():
|
| 31 |
+
if os.path.exists(CONFIG_FILE):
|
| 32 |
+
try:
|
| 33 |
+
with open(CONFIG_FILE, 'r') as f:
|
| 34 |
+
return {**DEFAULT_CONFIG, **json.load(f)}
|
| 35 |
+
except:
|
| 36 |
+
pass
|
| 37 |
+
return DEFAULT_CONFIG
|
| 38 |
+
|
| 39 |
+
def save_config(config):
|
| 40 |
+
with open(CONFIG_FILE, 'w') as f:
|
| 41 |
+
json.dump(config, f, indent=4)
|
| 42 |
+
|
| 43 |
+
ffmpeg_process = None
|
| 44 |
+
process_lock = threading.Lock()
|
| 45 |
+
|
| 46 |
+
def build_ffmpeg_cmd(config):
|
| 47 |
+
input_stream = config.get("input_stream", DEFAULT_CONFIG["input_stream"])
|
| 48 |
+
output_dir = "/app/hls"
|
| 49 |
+
|
| 50 |
+
cmd = [
|
| 51 |
+
"ffmpeg", "-y", "-re",
|
| 52 |
+
"-fflags", "+genpts+igndts+discardcorrupt",
|
| 53 |
+
"-probesize", "4000000",
|
| 54 |
+
"-analyzeduration", "4000000",
|
| 55 |
+
"-reconnect", "1", "-reconnect_at_eof", "1", "-reconnect_streamed", "1", "-reconnect_delay_max", "5",
|
| 56 |
+
"-i", input_stream
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
filter_complex_parts = []
|
| 60 |
+
|
| 61 |
+
ad_file = config.get("ad_file", "")
|
| 62 |
+
ad_path = os.path.join(app.config['UPLOAD_FOLDER'], ad_file) if ad_file else ""
|
| 63 |
+
has_ad = config.get("ad_enabled", False) and ad_file and os.path.exists(ad_path)
|
| 64 |
+
|
| 65 |
+
if has_ad:
|
| 66 |
+
# Check if file is gif or static image (png/jpg)
|
| 67 |
+
is_gif = ad_file.lower().endswith('.gif')
|
| 68 |
+
if is_gif:
|
| 69 |
+
cmd.extend(["-stream_loop", "-1", "-i", ad_path])
|
| 70 |
+
else:
|
| 71 |
+
cmd.extend(["-i", ad_path])
|
| 72 |
+
|
| 73 |
+
width = config.get("ad_width", 250)
|
| 74 |
+
# format=rgba preserves transparent background for PNGs
|
| 75 |
+
filter_complex_parts.append(f"[1:v]format=rgba,scale={width}:-1:flags=fast_bilinear[ad_scaled];")
|
| 76 |
+
|
| 77 |
+
pos = config.get("ad_position", "top_right")
|
| 78 |
+
# Coordinate mapping for overlay
|
| 79 |
+
if pos == "top_left":
|
| 80 |
+
x, y = "30", "30"
|
| 81 |
+
elif pos == "top_right":
|
| 82 |
+
x, y = "main_w-overlay_w-30", "30"
|
| 83 |
+
elif pos == "bottom_left":
|
| 84 |
+
x, y = "30", "main_h-overlay_h-30"
|
| 85 |
+
elif pos == "bottom_right":
|
| 86 |
+
x, y = "main_w-overlay_w-30", "main_h-overlay_h-30"
|
| 87 |
+
elif pos == "center":
|
| 88 |
+
x, y = "(main_w-overlay_w)/2", "(main_h-overlay_h)/2"
|
| 89 |
+
else:
|
| 90 |
+
x, y = "main_w-overlay_w-30", "30"
|
| 91 |
+
|
| 92 |
+
mode = config.get("mode", "periodic")
|
| 93 |
+
if mode == "always":
|
| 94 |
+
enable_expr = "1"
|
| 95 |
+
elif mode == "periodic":
|
| 96 |
+
interval = config.get("period_interval", 300)
|
| 97 |
+
duration = config.get("period_duration", 30)
|
| 98 |
+
enable_expr = f"lt(mod(t\\,{interval}),{duration})"
|
| 99 |
+
else:
|
| 100 |
+
enable_expr = "1"
|
| 101 |
+
|
| 102 |
+
filter_complex_parts.append(
|
| 103 |
+
f"[0:v][ad_scaled]overlay={x}:{y}:enable='{enable_expr}',format=yuv420p,split=4[v1][v2][v3][v4];"
|
| 104 |
+
)
|
| 105 |
+
else:
|
| 106 |
+
filter_complex_parts.append(
|
| 107 |
+
"[0:v]format=yuv420p,split=4[v1][v2][v3][v4];"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
filter_complex_parts.extend([
|
| 111 |
+
"[v1]scale=1280:720,fps=30[v720p30];",
|
| 112 |
+
"[v2]scale=1280:720,fps=50[v720p50];",
|
| 113 |
+
"[v3]scale=1920:1080,fps=30[v1080p30];",
|
| 114 |
+
"[v4]scale=1920:1080,fps=50[v1080p50];",
|
| 115 |
+
"[0:a]aresample=44100:async=1,pan=stereo|"
|
| 116 |
+
"FL=0.5*FL+0.707*FC+0.5*BL+0.5*LFE|"
|
| 117 |
+
"FR=0.5*FR+0.707*FC+0.5*BR+0.5*LFE,asplit=4[a1][a2][a3][a4]"
|
| 118 |
+
])
|
| 119 |
+
|
| 120 |
+
cmd.extend([
|
| 121 |
+
"-filter_complex", "".join(filter_complex_parts),
|
| 122 |
+
"-map", "[v720p30]", "-map", "[a1]", "-c:v:0", "libx264", "-preset", "ultrafast", "-b:v:0", "1200k", "-maxrate:v:0", "1500k", "-bufsize:v:0", "3000k", "-g:v:0", "60", "-keyint_min:v:0", "60", "-sc_threshold:v:0", "0", "-c:a:0", "aac", "-b:a:0", "128k",
|
| 123 |
+
"-map", "[v720p50]", "-map", "[a2]", "-c:v:1", "libx264", "-preset", "ultrafast", "-b:v:1", "1800k", "-maxrate:v:1", "2200k", "-bufsize:v:1", "4500k", "-g:v:1", "100", "-keyint_min:v:1", "100", "-sc_threshold:v:1", "0", "-c:a:1", "aac", "-b:a:1", "128k",
|
| 124 |
+
"-map", "[v1080p30]", "-map", "[a3]", "-c:v:2", "libx264", "-preset", "ultrafast", "-b:v:2", "2500k", "-maxrate:v:2", "3000k", "-bufsize:v:2", "6000k", "-g:v:2", "60", "-keyint_min:v:2", "60", "-sc_threshold:v:2", "0", "-c:a:2", "aac", "-b:a:2", "192k",
|
| 125 |
+
"-map", "[v1080p50]", "-map", "[a4]", "-c:v:3", "libx264", "-preset", "ultrafast", "-b:v:3", "3500k", "-maxrate:v:3", "4000k", "-bufsize:v:3", "8000k", "-g:v:3", "100", "-keyint_min:v:3", "100", "-sc_threshold:v:3", "0", "-c:a:3", "aac", "-b:a:3", "192k",
|
| 126 |
+
"-f", "hls",
|
| 127 |
+
"-hls_time", "4",
|
| 128 |
+
"-start_number", "0",
|
| 129 |
+
"-hls_list_size", "6",
|
| 130 |
+
"-hls_flags", "delete_segments+independent_segments",
|
| 131 |
+
"-master_pl_name", "master.m3u8",
|
| 132 |
+
"-var_stream_map", "v:0,a:0,name:720p30 v:1,a:1,name:720p50 v:2,a:2,name:1080p30 v:3,a:3,name:1080p50",
|
| 133 |
+
f"{output_dir}/%v/segment_%05d.ts",
|
| 134 |
+
f"{output_dir}/%v/playlist.m3u8"
|
| 135 |
+
])
|
| 136 |
+
|
| 137 |
+
for sub in ["720p30", "720p50", "1080p30", "1080p50"]:
|
| 138 |
+
os.makedirs(os.path.join(output_dir, sub), exist_ok=True)
|
| 139 |
+
|
| 140 |
+
return cmd
|
| 141 |
+
|
| 142 |
+
def run_ffmpeg_worker():
|
| 143 |
+
global ffmpeg_process
|
| 144 |
+
while True:
|
| 145 |
+
config = load_config()
|
| 146 |
+
cmd = build_ffmpeg_cmd(config)
|
| 147 |
+
print("[FFmpeg Manager] Starting FFmpeg process...")
|
| 148 |
+
|
| 149 |
+
with process_lock:
|
| 150 |
+
ffmpeg_process = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
|
| 151 |
+
|
| 152 |
+
ffmpeg_process.wait()
|
| 153 |
+
print("[FFmpeg Manager] FFmpeg exited. Restarting in 3 seconds...")
|
| 154 |
+
time.sleep(3)
|
| 155 |
+
|
| 156 |
+
threading.Thread(target=run_ffmpeg_worker, daemon=True).start()
|
| 157 |
+
|
| 158 |
+
def restart_ffmpeg():
|
| 159 |
+
global ffmpeg_process
|
| 160 |
+
with process_lock:
|
| 161 |
+
if ffmpeg_process and ffmpeg_process.poll() is None:
|
| 162 |
+
print("[FFmpeg Manager] Terminating current FFmpeg for config update...")
|
| 163 |
+
ffmpeg_process.terminate()
|
| 164 |
+
try:
|
| 165 |
+
ffmpeg_process.wait(timeout=5)
|
| 166 |
+
except:
|
| 167 |
+
ffmpeg_process.kill()
|
| 168 |
+
|
| 169 |
+
HTML_TEMPLATE = """
|
| 170 |
+
<!DOCTYPE html>
|
| 171 |
+
<html lang="vi">
|
| 172 |
+
<head>
|
| 173 |
+
<meta charset="UTF-8">
|
| 174 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 175 |
+
<title>Antv Live - IPTV Restream & Visual Ad Manager</title>
|
| 176 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 177 |
+
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
|
| 178 |
+
<style>
|
| 179 |
+
.pos-btn { transition: all 0.2s; }
|
| 180 |
+
.pos-btn.active { background-color: #06b6d4; color: white; border-color: #22d3ee; box-shadow: 0 0 15px rgba(6,182,212,0.5); }
|
| 181 |
+
</style>
|
| 182 |
+
</head>
|
| 183 |
+
<body class="bg-slate-900 text-slate-100 min-h-screen">
|
| 184 |
+
<div class="container mx-auto px-4 py-8 max-w-6xl">
|
| 185 |
+
<header class="flex justify-between items-center mb-8 border-b border-slate-700 pb-4">
|
| 186 |
+
<div>
|
| 187 |
+
<h1 class="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-pink-500">
|
| 188 |
+
ANTV Live Restream & Visual Ad Manager
|
| 189 |
+
</h1>
|
| 190 |
+
<p class="text-slate-400 text-sm mt-1">Hệ thống chèn logo/PNG trong suốt & Quảng cáo động trực quan</p>
|
| 191 |
+
</div>
|
| 192 |
+
<div class="flex items-center gap-2">
|
| 193 |
+
<span class="inline-block w-3 h-3 bg-emerald-500 rounded-full animate-pulse"></span>
|
| 194 |
+
<span class="text-emerald-400 font-medium text-sm">System Online</span>
|
| 195 |
+
</div>
|
| 196 |
+
</header>
|
| 197 |
+
|
| 198 |
+
{% if message %}
|
| 199 |
+
<div class="bg-emerald-900/50 border border-emerald-500 text-emerald-200 px-4 py-3 rounded-lg mb-6 flex justify-between items-center">
|
| 200 |
+
<span>{{ message }}</span>
|
| 201 |
+
<button onclick="this.parentElement.remove()" class="text-emerald-400 hover:text-white font-bold">×</button>
|
| 202 |
+
</div>
|
| 203 |
+
{% endif %}
|
| 204 |
+
|
| 205 |
+
<div class="grid grid-cols-1 lg:grid-cols-12 gap-8">
|
| 206 |
+
<!-- Left Panel: Form Settings -->
|
| 207 |
+
<div class="lg:col-span-7 bg-slate-800/80 border border-slate-700 rounded-2xl p-6 shadow-xl space-y-6">
|
| 208 |
+
<h2 class="text-xl font-semibold flex items-center gap-2 text-cyan-400">
|
| 209 |
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
|
| 210 |
+
Cấu hình Luồng & Tùy chọn Chèn
|
| 211 |
+
</h2>
|
| 212 |
+
|
| 213 |
+
<form method="POST" enctype="multipart/form-data" class="space-y-5">
|
| 214 |
+
<div>
|
| 215 |
+
<label class="block text-sm font-medium text-slate-300 mb-1">Link IPTV Nguồn (Input Stream URL)</label>
|
| 216 |
+
<input type="text" name="input_stream" value="{{ config.input_stream }}" required
|
| 217 |
+
class="w-full bg-slate-900 border border-slate-700 rounded-lg px-4 py-2 text-slate-200 focus:outline-none focus:border-cyan-500">
|
| 218 |
+
</div>
|
| 219 |
+
|
| 220 |
+
<div class="border-t border-slate-700 pt-4">
|
| 221 |
+
<div class="flex items-center justify-between mb-4">
|
| 222 |
+
<label class="font-medium text-slate-200 flex items-center gap-2 cursor-pointer">
|
| 223 |
+
<input type="checkbox" name="ad_enabled" {% if config.ad_enabled %}checked{% endif %} class="w-4 h-4 accent-cyan-500 rounded">
|
| 224 |
+
Bật Chèn File (PNG trong suốt / GIF động / MP4)
|
| 225 |
+
</label>
|
| 226 |
+
</div>
|
| 227 |
+
|
| 228 |
+
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-4">
|
| 229 |
+
<div>
|
| 230 |
+
<label class="block text-sm font-medium text-slate-300 mb-1">Tải lên hình ảnh / GIF / MP4</label>
|
| 231 |
+
<input type="file" name="ad_file_upload" accept=".png,.gif,.mp4,.jpg"
|
| 232 |
+
class="w-full text-sm text-slate-400 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-cyan-600 file:text-white hover:file:bg-cyan-500">
|
| 233 |
+
{% if config.ad_file %}
|
| 234 |
+
<p class="text-xs text-slate-400 mt-1">Đang dùng file: <span class="text-cyan-400">{{ config.ad_file }}</span></p>
|
| 235 |
+
{% endif %}
|
| 236 |
+
</div>
|
| 237 |
+
<div>
|
| 238 |
+
<label class="block text-sm font-medium text-slate-300 mb-1">Chiều rộng hiển thị (px)</label>
|
| 239 |
+
<input type="number" name="ad_width" value="{{ config.ad_width }}" class="w-full bg-slate-900 border border-slate-700 rounded-lg px-4 py-2 text-slate-200 focus:outline-none focus:border-cyan-500">
|
| 240 |
+
</div>
|
| 241 |
+
</div>
|
| 242 |
+
|
| 243 |
+
<!-- VISUAL POSITION SELECTOR -->
|
| 244 |
+
<div class="mb-4">
|
| 245 |
+
<label class="block text-sm font-medium text-slate-300 mb-2">Chọn vị trí hiển thị trực quan trên màn hình</label>
|
| 246 |
+
<input type="hidden" name="ad_position" id="ad_position_input" value="{{ config.ad_position }}">
|
| 247 |
+
|
| 248 |
+
<div class="relative w-full aspect-video bg-slate-950 border-2 border-slate-700 rounded-xl overflow-hidden p-3 flex flex-col justify-between shadow-inner">
|
| 249 |
+
<!-- Top Row -->
|
| 250 |
+
<div class="flex justify-between">
|
| 251 |
+
<button type="button" data-pos="top_left" class="pos-btn px-4 py-2 bg-slate-900 border border-slate-700 rounded-lg text-xs font-semibold text-slate-300 hover:border-cyan-400">↖ Góc Trên Trái</button>
|
| 252 |
+
<button type="button" data-pos="top_right" class="pos-btn px-4 py-2 bg-slate-900 border border-slate-700 rounded-lg text-xs font-semibold text-slate-300 hover:border-cyan-400">↗ Góc Trên Phải</button>
|
| 253 |
+
</div>
|
| 254 |
+
<!-- Center Row -->
|
| 255 |
+
<div class="flex justify-center">
|
| 256 |
+
<button type="button" data-pos="center" class="pos-btn px-4 py-2 bg-slate-900 border border-slate-700 rounded-lg text-xs font-semibold text-slate-300 hover:border-cyan-400">⊙ Chính Giữa</button>
|
| 257 |
+
</div>
|
| 258 |
+
<!-- Bottom Row -->
|
| 259 |
+
<div class="flex justify-between">
|
| 260 |
+
<button type="button" data-pos="bottom_left" class="pos-btn px-4 py-2 bg-slate-900 border border-slate-700 rounded-lg text-xs font-semibold text-slate-300 hover:border-cyan-400">↙ Góc Dưới Trái</button>
|
| 261 |
+
<button type="button" data-pos="bottom_right" class="pos-btn px-4 py-2 bg-slate-900 border border-slate-700 rounded-lg text-xs font-semibold text-slate-300 hover:border-cyan-400">↘ Góc Dưới Phải</button>
|
| 262 |
+
</div>
|
| 263 |
+
</div>
|
| 264 |
+
</div>
|
| 265 |
+
|
| 266 |
+
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
| 267 |
+
<div>
|
| 268 |
+
<label class="block text-sm font-medium text-slate-300 mb-1">Chế độ hiển thị</label>
|
| 269 |
+
<select name="mode" id="mode_select" onchange="toggleModeFields()" class="w-full bg-slate-900 border border-slate-700 rounded-lg px-3 py-2 text-slate-200 focus:outline-none focus:border-cyan-500">
|
| 270 |
+
<option value="always" {% if config.mode == 'always' %}selected{% endif %}>Luôn luôn hiển thị</option>
|
| 271 |
+
<option value="periodic" {% if config.mode == 'periodic' %}selected{% endif %}>Định kỳ (Theo chu kỳ giây)</option>
|
| 272 |
+
</select>
|
| 273 |
+
</div>
|
| 274 |
+
</div>
|
| 275 |
+
|
| 276 |
+
<div id="periodic_fields" class="grid grid-cols-2 gap-4 mt-4 bg-slate-900/50 p-4 rounded-xl border border-slate-700/50">
|
| 277 |
+
<div>
|
| 278 |
+
<label class="block text-xs font-medium text-slate-300 mb-1">Chu kỳ lặp lại (giây)</label>
|
| 279 |
+
<input type="number" name="period_interval" value="{{ config.period_interval }}" class="w-full bg-slate-900 border border-slate-700 rounded-lg px-3 py-1.5 text-slate-200 text-sm">
|
| 280 |
+
</div>
|
| 281 |
+
<div>
|
| 282 |
+
<label class="block text-xs font-medium text-slate-300 mb-1">Thời gian hiển thị (giây)</label>
|
| 283 |
+
<input type="number" name="period_duration" value="{{ config.period_duration }}" class="w-full bg-slate-900 border border-slate-700 rounded-lg px-3 py-1.5 text-slate-200 text-sm">
|
| 284 |
+
</div>
|
| 285 |
+
</div>
|
| 286 |
+
</div>
|
| 287 |
+
|
| 288 |
+
<div class="pt-4 flex justify-end">
|
| 289 |
+
<button type="submit" class="bg-gradient-to-r from-cyan-500 to-pink-500 text-white font-semibold px-6 py-2.5 rounded-xl shadow-lg hover:opacity-90 transition transform active:scale-95">
|
| 290 |
+
Lưu Cấu Hình & Áp Dụng Ngay
|
| 291 |
+
</button>
|
| 292 |
+
</div>
|
| 293 |
+
</form>
|
| 294 |
+
</div>
|
| 295 |
+
|
| 296 |
+
<!-- Right Panel: Live Player Preview -->
|
| 297 |
+
<div class="lg:col-span-5 space-y-6">
|
| 298 |
+
<div class="bg-slate-800/80 border border-slate-700 rounded-2xl p-6 shadow-xl">
|
| 299 |
+
<h2 class="text-xl font-semibold mb-4 flex items-center gap-2 text-pink-400">
|
| 300 |
+
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"/></svg>
|
| 301 |
+
Xem Trước Trực Tiếp (Live Stream)
|
| 302 |
+
</h2>
|
| 303 |
+
<div class="aspect-video bg-black rounded-xl overflow-hidden relative shadow-inner">
|
| 304 |
+
<video id="video" controls autoplay muted class="w-full h-full object-contain"></video>
|
| 305 |
+
</div>
|
| 306 |
+
<div class="mt-4 text-xs text-slate-400 space-y-1">
|
| 307 |
+
<p><strong>Master Playlist M3U8:</strong></p>
|
| 308 |
+
<code class="block bg-slate-900 p-2 rounded text-cyan-300 break-all select-all">/hls/master.m3u8</code>
|
| 309 |
+
</div>
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
</div>
|
| 313 |
+
</div>
|
| 314 |
+
|
| 315 |
+
<script>
|
| 316 |
+
// Visual Position Selector Logic
|
| 317 |
+
const currentPos = "{{ config.ad_position }}";
|
| 318 |
+
const posButtons = document.querySelectorAll('.pos-btn');
|
| 319 |
+
const posInput = document.getElementById('ad_position_input');
|
| 320 |
+
|
| 321 |
+
function updateActiveButton(pos) {
|
| 322 |
+
posButtons.forEach(btn => {
|
| 323 |
+
if (btn.getAttribute('data-pos') === pos) {
|
| 324 |
+
btn.classList.add('active');
|
| 325 |
+
} else {
|
| 326 |
+
btn.classList.remove('active');
|
| 327 |
+
}
|
| 328 |
+
});
|
| 329 |
+
posInput.value = pos;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
posButtons.forEach(btn => {
|
| 333 |
+
btn.addEventListener('click', () => {
|
| 334 |
+
const pos = btn.getAttribute('data-pos');
|
| 335 |
+
updateActiveButton(pos);
|
| 336 |
+
});
|
| 337 |
+
});
|
| 338 |
+
|
| 339 |
+
updateActiveButton(currentPos);
|
| 340 |
+
|
| 341 |
+
function toggleModeFields() {
|
| 342 |
+
const mode = document.getElementById('mode_select').value;
|
| 343 |
+
const periodicFields = document.getElementById('periodic_fields');
|
| 344 |
+
if (mode === 'periodic') {
|
| 345 |
+
periodicFields.style.display = 'grid';
|
| 346 |
+
} else {
|
| 347 |
+
periodicFields.style.display = 'none';
|
| 348 |
+
}
|
| 349 |
+
}
|
| 350 |
+
toggleModeFields();
|
| 351 |
+
|
| 352 |
+
// HLS Player init
|
| 353 |
+
var video = document.getElementById('video');
|
| 354 |
+
var videoSrc = '/hls/master.m3u8';
|
| 355 |
+
if (Hls.isSupported()) {
|
| 356 |
+
var hls = new Hls();
|
| 357 |
+
hls.loadSource(videoSrc);
|
| 358 |
+
hls.attachMedia(video);
|
| 359 |
+
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
| 360 |
+
video.src = videoSrc;
|
| 361 |
+
}
|
| 362 |
+
</script>
|
| 363 |
+
</body>
|
| 364 |
+
</html>
|
| 365 |
+
"""
|
| 366 |
+
|
| 367 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 368 |
+
def index():
|
| 369 |
+
config = load_config()
|
| 370 |
+
message = None
|
| 371 |
+
|
| 372 |
+
if request.method == 'POST':
|
| 373 |
+
config['input_stream'] = request.form.get('input_stream', config['input_stream'])
|
| 374 |
+
config['ad_enabled'] = True if request.form.get('ad_enabled') else False
|
| 375 |
+
config['ad_position'] = request.form.get('ad_position', 'top_right')
|
| 376 |
+
try:
|
| 377 |
+
config['ad_width'] = int(request.form.get('ad_width', 250))
|
| 378 |
+
except:
|
| 379 |
+
pass
|
| 380 |
+
config['mode'] = request.form.get('mode', 'periodic')
|
| 381 |
+
try:
|
| 382 |
+
config['period_interval'] = int(request.form.get('period_interval', 300))
|
| 383 |
+
config['period_duration'] = int(request.form.get('period_duration', 30))
|
| 384 |
+
except:
|
| 385 |
+
pass
|
| 386 |
+
|
| 387 |
+
file = request.files.get('ad_file_upload')
|
| 388 |
+
if file and file.filename != '':
|
| 389 |
+
filename = secure_filename(file.filename)
|
| 390 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 391 |
+
file.save(filepath)
|
| 392 |
+
config['ad_file'] = filename
|
| 393 |
+
|
| 394 |
+
save_config(config)
|
| 395 |
+
restart_ffmpeg()
|
| 396 |
+
message = "Đã lưu cấu hình và áp dụng thay đổi thành công!"
|
| 397 |
+
config = load_config()
|
| 398 |
+
|
| 399 |
+
return render_template_string(HTML_TEMPLATE, config=config, message=message)
|
| 400 |
+
|
| 401 |
+
@app.route('/hls/<path:filename>')
|
| 402 |
+
def serve_hls(filename):
|
| 403 |
+
return send_from_directory('/app/hls', filename)
|
| 404 |
+
|
| 405 |
+
if __name__ == '__main__':
|
| 406 |
+
app.run(host='0.0.0.0', port=7860)
|