Files
2026-08-15 20:29:20 +08:00

73 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# FFmpeg 视频截图与缩略图生成
## 功能说明
从视频中每 3 秒截取一张图片,并自动生成 8x8 的缩略图网格(每张缩略图显示 64 帧画面),所有图片存放在以视频文件名命名的子文件夹中。
## 完整命令
```bash
# 替换 "你的视频文件名.mp4" 为实际视频路径
INPUT="你的视频文件名.mp4" && \
BASE="${INPUT%.*}" && \
FRAME_DIR="./frames_$BASE" && \
mkdir -p "$FRAME_DIR" && \
ffmpeg -i "$INPUT" -vf "fps=1/3" "$FRAME_DIR/frame_%04d.jpg" && \
cd "$FRAME_DIR" && \
total=$(ls frame_*.jpg 2>/dev/null | wc -l) && \
batch=64 && \
for ((i=0; i<total; i+=batch)); do \
start=$((i+1)); \
end=$((i+batch)); \
[ $end -gt $total ] && end=$total; \
ls -v frame_*.jpg | sed -n "$start,$end p" | sed 's/^/file /' > filelist.txt; \
ffmpeg -f concat -safe 0 -i filelist.txt \
-vf "scale=200:-1,drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf:text='%{pts\:hms}':x=10:y=10:fontsize=12:fontcolor=white:borderw=1:bordercolor=black,drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf:text='frame_%{n}':x=10:y=28:fontsize=10:fontcolor=white:borderw=1:bordercolor=black,tile=8x8" \
-vsync cfr -frames:v 1 "${BASE}_thumb_$((i/batch+1)).jpg"; \
rm -f filelist.txt; \
done && \
cd ..
```
## 使用步骤
1. **修改视频路径**:将命令开头的 `INPUT="你的视频文件名.mp4"` 改为你的实际视频文件路径
2. **执行命令**:在终端中粘贴并运行
3. **查看结果**
- 所有截图保存在 `./frames_视频名/` 目录下
- 缩略图文件名为 `视频名_thumb_1.jpg``视频名_thumb_2.jpg`...
## 参数说明
| 参数 | 说明 |
|------|------|
| `fps=1/3` | 每 3 秒截取 1 帧 |
| `batch=64` | 每 64 张图片生成一张 8x8 缩略图 |
| `scale=200:-1` | 缩略图中每张图片宽度 200px,高度自适应 |
| `tile=8x8` | 8 行 8 列网格布局 |
| `drawtext` | 在图片上叠加时间戳和帧编号 |
## 注意事项
- 需要提前安装 `ffmpeg``dejavu` 字体(用于显示文字水印)
- 如果系统字体路径不同,请修改 `fontfile=` 参数
- 视频文件路径建议使用绝对路径或相对路径,避免空格和特殊字符
## 常用变体
### 修改截图频率
```bash
# 改为每 5 秒截一张
ffmpeg -i input.mp4 -vf "fps=1/5" frames/frame_%04d.jpg
```
### 修改缩略图网格大小
```bash
# 改为 4x4 网格(每张缩略图显示 16 帧)
# 将 tile=8x8 改为 tile=4x4,同时 batch=16
```
### 不添加文字水印
```bash
# 移除所有 drawtext 相关参数
ffmpeg -f concat -safe 0 -i filelist.txt -vf "scale=200:-1,tile=8x8" -vsync cfr -frames:v 1 thumb.jpg
```