10K+
专为媒体库自动生成美观封面图片的 Docker 服务,支持多种样式,开箱即用。主要服务于 Media Saber,其他工具也可以通过 API 调用使用。
创建 docker-compose.yml:
version: '3.8'
services:
media-cover:
image: xylplm/media-saber-media-cover-generator:latest
ports:
- "9897:9897"
volumes:
- ./config:/app/config
environment:
- TZ=Asia/Shanghai
启动服务:
docker-compose up -d
docker run -d \
-p 9897:9897 \
-v ./config:/app/config \
-e TZ=Asia/Shanghai \
xylplm/media-saber-media-cover-generator:latest
访问 http://localhost:9897/health 检查服务状态。
默认情况下,服务无需任何配置即可启动使用! 所有配置项都有合理的默认值。
配置分为两类,互相独立,各有默认值:
用于控制服务器运行环境:
| 环境变量 | 说明 | 默认值 |
|---|---|---|
PORT | 服务端口 | 9897 |
HOST | 服务主机 | 0.0.0.0 |
READ_TIMEOUT | 读取超时(秒) | 30 |
WRITE_TIMEOUT | 写入超时(秒) | 300 (5分钟) |
IDLE_TIMEOUT | 空闲超时(秒) | 120 (2分钟) |
CONFIG_DIR | 配置目录 | ./config |
CACHE_MAX_SIZE | 最大缓存大小(字节) | 1073741824 (1GB) |
CACHE_TTL | 缓存过期时间(小时) | 24 |
LOG_LEVEL | 日志级别 | info |
LOG_FORMAT | 日志格式 | json |
LOG_OUTPUT | 日志输出 | stdout |
Docker 示例:
docker run -d \
-p 8080:8080 \
-e PORT=8080 \
-e LOG_LEVEL=debug \
-e WRITE_TIMEOUT=600 \
-v ./config:/app/config \
xylplm/media-saber-media-cover-generator:latest
用于自定义业务逻辑(字体、图片、模板),在 config/config.yaml 中配置。
重要:
config.example.yaml 到 config/ 目录作为参考config/config.yaml(可选)创建 config/config.yaml:
template:
output_width: 3840
output_height: 2160
步骤:
./config/fonts/ 目录(宿主机路径)config/config.yaml 配置文件:font:
chinese:
local_path: "/app/config/fonts/my-font.ttf" # 容器内路径
english:
local_path: "/app/config/fonts/my-en-font.ttf"
路径说明:
./config/fonts/my-font.ttf/app/config/fonts/my-font.ttf(配置文件中使用)./config:/app/config验证配置:
服务启动后会在日志中显示字体加载状态:
✅ 成功加载自定义字体自定义字体加载失败,将使用预制字体(并说明原因)查看日志确认字体加载状态:
docker logs <container-name>
注意:如果自定义字体加载失败,服务会自动降级使用预制字体,不会影响正常运行。
template:
library_mappings:
"电影": "single_1"
"电视剧": "multi_1"
"动漫": "multi_2"
image:
max_size: 52428800 # 50MB
quality: 90
download_timeout: 60 # 下载超时60秒
# 图片处理配置(可选,默认值已优化)
image:
max_size: 52428800 # 最大图片大小(字节) (默认: 50MB)
max_width: 4096 # 最大图片宽度 (默认: 4096)
max_height: 4096 # 最大图片高度 (默认: 4096)
quality: 90 # 图片质量 1-100 (默认: 85)
download_timeout: 60 # 下载超时(秒) (默认: 60)
max_retries: 3 # 最大重试次数 (默认: 3)
# 字体配置(可选,默认使用预制字体)
font:
chinese:
name: "自定义中文字体" # 字体名称
local_path: "/app/config/fonts/chinese.ttf" # 容器内路径
size: 1.0 # 字体大小倍数 (默认: 1.0)
english:
name: "自定义英文字体" # 字体名称
local_path: "/app/config/fonts/english.ttf" # 容器内路径
size: 1.0 # 字体大小倍数 (默认: 1.0)
download_timeout: 30 # 字体下载超时(秒) (默认: 30)
max_retries: 3 # 最大重试次数 (默认: 3)
# 模板配置(可选,默认值已优化)
template:
output_width: 3840 # 输出图片宽度 (默认: 1920)
output_height: 2160 # 输出图片高度 (默认: 1080)
default_template: "single_1" # 默认模板 (默认: single_1)
# 自定义样式配置(可选)
styles:
single_1:
name: "单图样式1"
description: "简洁风格的单图封面"
blur_size: 50 # 模糊大小 0-200
color_ratio: 0.8 # 颜色比例 0-1
use_primary: false # 是否使用主色调
single_2:
name: "单图样式2"
blur_size: 30
color_ratio: 0.6
use_primary: true
# 媒体库到模板的映射(可选)
library_mappings:
"电影": "single_1"
"电视剧": "multi_1"
"动漫": "multi_2"
version: '3.8'
services:
media-cover:
image: xylplm/media-saber-media-cover-generator:latest
ports:
- "9897:9897"
volumes:
- ./config:/app/config
无需任何配置,直接启动即可使用。
version: '3.8'
services:
media-cover:
image: xylplm/media-saber-media-cover-generator:latest
ports:
- "8080:8080"
environment:
- PORT=8080
- LOG_LEVEL=debug
volumes:
- ./config:/app/config
version: '3.8'
services:
media-cover:
image: xylplm/media-saber-media-cover-generator:latest
ports:
- "9897:9897"
environment:
- LOG_LEVEL=info
- LOG_FORMAT=json
- WRITE_TIMEOUT=300
- CACHE_MAX_SIZE=2147483648 # 2GB
volumes:
- ./config:/app/config
restart: unless-stopped
将字体文件放到 ./config/fonts/ 目录(宿主机路径)
./config/fonts/my-font.ttf创建 config/config.yaml:
font:
chinese:
# 容器内路径,对应宿主机的 ./config/fonts/my-font.ttf
local_path: "/app/config/fonts/my-font.ttf"
template:
output_width: 3840
output_height: 2160
version: '3.8'
services:
media-cover:
image: xylplm/media-saber-media-cover-generator:latest
ports:
- "9897:9897"
volumes:
- ./config:/app/config
curl http://localhost:9897/health
curl http://localhost:9897/api/templates
返回示例:
{
"templates": [
{
"id": "single_1",
"name": "单图样式 1",
"description": "经典单图布局"
},
{
"id": "single_2",
"name": "单图样式 2",
"description": "现代单图布局"
},
{
"id": "multi_1",
"name": "多图样式 1",
"description": "3图横向排列"
},
{
"id": "multi_2",
"name": "多图样式 2",
"description": "2+1图混合布局"
},
{
"id": "multi_3",
"name": "多图样式 3",
"description": "5图横向布局,中间1张左右各2张阶梯倾斜"
}
]
}
curl -X POST http://localhost:9897/api/generate-cover \
-H "Content-Type: application/json" \
-d '{
"library": {
"name": "电影",
"titleZh": "示例电影",
"titleEn": "Sample Movie"
},
"images": [
{
"url": "https://example.com/poster.jpg",
"type": "poster"
}
]
}' \
--output cover.jpg
curl -X POST http://localhost:9897/api/generate-cover \
-H "Content-Type: application/json" \
-d '{
"library": {
"name": "电视剧",
"titleZh": "示例电视剧",
"titleEn": "Sample TV Series"
},
"images": [
{
"url": "https://example.com/poster1.jpg",
"type": "poster"
},
{
"url": "https://example.com/backdrop1.jpg",
"type": "backdrop"
},
{
"url": "https://example.com/backdrop2.jpg",
"type": "backdrop"
}
]
}' \
--output cover.jpg
curl -X POST http://localhost:9897/api/generate-cover \
-H "Content-Type: application/json" \
-d '{
"library": {
"name": "电视剧",
"titleZh": "示例电视剧",
"titleEn": "Sample TV Series"
},
"images": [
{
"url": "https://example.com/poster.jpg",
"type": "poster"
}
],
"templateId": "multi_1",
"userAgent": "Media Saber v1.0",
"lowPerformanceMode": false
}' \
--output cover.jpg
library 对象(必填):
name (string) - 媒体库名称,必填。如:电影、电视剧、动漫等titleZh (string) - 中文标题,必填。如:示例电影titleEn (string) - 英文标题,必填。如:Sample Movieimages 数组(必填):
url (string) - 图片网络地址,必填type (string) - 图片类型,可选。支持:
poster - 海报(默认值)backdrop - 背景图logo - 标志其他参数(可选):
templateId (string) - 指定使用的模板ID。可选值:single_1、single_2、multi_1、multi_2、multi_3。不指定时使用默认模板userAgent (string) - 自定义 User-Agent 字符串lowPerformanceMode (boolean) - 低性能模式开关(默认 false),启用时会降低并发和资源占用{
"success": true,
"coverImage": "base64编码的图片数据...",
"format": "jpeg",
"size": {
"width": 1920,
"height": 1080
}
}
保存返回的图片:
# 方法1:curl 自动保存
curl -X POST http://localhost:9897/api/generate-cover ... --output cover.jpg
# 方法2:使用 jq 提取 base64 并保存
curl -s -X POST http://localhost:9897/api/generate-cover ... | \
jq -r '.coverImage' | base64 -d > cover.jpg
config/ # Docker 挂载点
├── config.yaml # 用户配置文件(可选)
├── config.example.yaml # 配置示例(自动生成)
├── cache/ # 缓存目录
│ ├── images/ # 图片缓存
│ └── fonts/ # 字体缓存
├── fonts/ # 自定义字体(可选)
└── logs/ # 日志文件(可选)
A: 通过环境变量 PORT:
docker run -e PORT=8080 -p 8080:8080 ...
A:
./config/fonts/ 目录(宿主机路径)config/config.yaml:font:
chinese:
# 注意:这是容器内路径,Docker会自动映射 ./config -> /app/config
local_path: "/app/config/fonts/my-font.ttf"
路径映射关系:
./config/fonts/my-font.ttf/app/config/fonts/my-font.ttf(配置中使用)A: 在 config/config.yaml 中配置:
template:
output_width: 3840
output_height: 2160
A: 通过 Docker 命令:
docker logs media-cover-generator
或配置日志输出到文件:
docker run -e LOG_OUTPUT=file ...
日志文件位于 config/logs/app.log。
A:
CACHE_MAX_SIZE 调整)A: 删除缓存目录:
rm -rf ./config/cache/*
或设置缓存过期时间(默认 24 小时):
docker run -e CACHE_TTL=12 ... # 12小时过期
A: 不需要!所有配置都有默认值,可以零配置启动。只有需要自定义时才创建 config/config.yaml。
A:
| 配置项 | 默认值 |
|---|---|
| 服务端口 | 9897 |
| 服务主机 | 0.0.0.0 |
| 读取超时 | 30秒 |
| 写入超时 | 300秒(5分钟) |
| 空闲超时 | 120秒(2分钟) |
| 缓存大小 | 1GB |
| 缓存过期 | 24小时 |
| 日志级别 | info |
| 配置项 | 默认值 |
|---|---|
| 输出宽度 | 1920 |
| 输出高度 | 1080 |
| 图片质量 | 85 |
| 图片最大大小 | 50MB |
| 下载超时 | 60秒 |
| 默认模板 | single_1 |
| 可用样式 | 5种(single_1, single_2, multi_1, multi_2, multi_3) |
docker-compose pull
docker-compose up -d
或:
docker pull xylplm/media-saber-media-cover-generator:latest
docker-compose up -d
升级不影响现有数据:
根据使用频率调整缓存:
# 高频使用:增大缓存
docker run -e CACHE_MAX_SIZE=5368709120 ... # 5GB
# 低频使用:减小缓存
docker run -e CACHE_MAX_SIZE=536870912 ... # 512MB
根据网络环境调整:
# 网络较慢:增加超时
docker run -e WRITE_TIMEOUT=600 ... # 10分钟
在 config/config.yaml 中:
image:
download_timeout: 60 # 下载超时60秒
生产环境建议使用 info 或 warn:
docker run -e LOG_LEVEL=warn ...
MIT License
享受自动化封面生成带来的便利! 🎉
Content type
Image
Digest
sha256:1d64ce846…
Size
29.6 MB
Last updated
8 months ago
docker pull xylplm/media-saber-media-cover-generator:DEV_202512021056