Docker 直播源 Web 管理系统
下面是一个基于 Docker 的直播源管理系统的实现方案,包含扫描、测试和分类功能。
系统架构
- Web 界面:用于输入直播源地址
- 后端处理:使用 Python Flask 处理请求
- FFmpeg:用于测试直播源有效性
- Docker 容器:封装整个应用
实现步骤
1. 创建项目结构
code
live-source-manager/
├── app/
│ ├── static/
│ ├── templates/
│ │ └── index.html
│ ├── __init__.py
│ ├── main.py
│ └── utils.py
├── Dockerfile
├── requirements.txt
└── docker-compose.yml
2. 编写 Dockerfile
dockerfile
FROM python:3.9-slim
WORKDIR /app
RUN apt-get update && apt-get install -y \
ffmpeg \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app.main:app"]
3. 编写 docker-compose.yml
yaml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- ./app:/app
restart: unless-stopped
4. 编写 requirements.txt
code
Flask==2.0.1
gunicorn==20.1.0
requests==2.26.0
5. 编写 Web 界面 (templates/index.html)
html
<!DOCTYPE html>
<html>
<head>
<title>直播源管理系统</title>
</head>
<body>
<h1>直播源管理系统</h1>
<form method="POST" action="/process">
<textarea name="sources" rows="20" cols="80" placeholder="请输入直播源地址,每行一个"></textarea>
<br>
<button type="submit">处理直播源</button>
</form>
{% if message %}
<p>{{ message }}</p>
{% endif %}
</body>
</html>
6. 编写主应用 (app/main.py)
python
from flask import Flask, render_template, request, redirect, url_for
import os
from .utils import process_sources
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process():
sources = request.form['sources']
if not sources:
return render_template('index.html', message="请输入直播源地址")
# 保存原始输入
with open('test.txt', 'w', encoding='utf-8') as f:
f.write(sources)
# 处理直播源
valid_count, invalid_count = process_sources('test.txt', 'tx.txt', 'wx.txt')
message = f"处理完成!有效源: {valid_count} 个,无效源: {invalid_count} 个"
return render_template('index.html', message=message)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
7. 编写工具函数 (app/utils.py)
python
import subprocess
import os
import time
def test_stream_with_ffmpeg(url, timeout=5):
"""
使用FFmpeg测试直播源是否有效
"""
try:
command = [
'ffmpeg',
'-i', url,
'-t', str(timeout),
'-f', 'null',
'-',
'-v', 'error'
]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE
)
_, stderr = process.communicate(timeout=timeout+2)
if process.returncode == 0:
return True
return False
except:
return False
def process_sources(input_file, valid_output, invalid_output):
"""
处理直播源文件
"""
valid_count = 0
invalid_count = 0
with open(input_file, 'r', encoding='utf-8') as f:
sources = [line.strip() for line in f if line.strip()]
with open(valid_output, 'w', encoding='utf-8') as valid_f, \
open(invalid_output, 'w', encoding='utf-8') as invalid_f:
for source in sources:
if test_stream_with_ffmpeg(source):
valid_f.write(source + '\n')
valid_count += 1
else:
invalid_f.write(source + '\n')
invalid_count += 1
return valid_count, invalid_count
使用说明
-
构建并启动容器:
bash
docker-compose up --build
-
访问 http://localhost:5000
-
在文本框中输入直播源地址(每行一个),点击"处理直播源"按钮
-
系统会:
- 将原始输入保存为
test.txt
- 使用 FFmpeg 测试每个直播源
- 将有效源写入
tx.txt
- 将无效源写入
wx.txt
注意事项
-
FFmpeg 测试可能需要几秒钟时间,大量直播源会需要较长时间处理
-
可以根据需要调整 test_stream_with_ffmpeg 函数中的超时时间
-
对于生产环境,建议添加以下功能:
- 用户认证
- 任务队列(Celery)处理长时间任务
- 更完善的错误处理
- 日志记录
-
直播源格式可以是 m3u8、rtmp、rtsp 等 FFmpeg 支持的格式