Files
2026-04-13 21:05:23 +08:00

47 lines
1.9 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.
# python-ffmpeg-hevc-crf18
正如其名按照固定配置HEVC CRF 18压制视频“以尽可能不影响画面的情况下压缩体积”。
## 依赖
唯一不可或缺的只有`ffmpeg`。无论 Windows 还是 Linux获取这个软件包并不难。
可选第三方 Python 库:`tqdm``colorlog`。没有也没关系,接口是兼容的。
## 应用
有两种命令行调用。当模块导入挑着用也没问题,只是没必要。
- `cmps_hevc_crf18 [-d SRCDIR] [-o OUTDIR] [-f EXT1] [-f EXT2] [-r] [-s]`
- `cmps_hevc_crf18 [-r] [-s] [-o OUTDIR] files`
剩下的直接翻译 argparse 帮助文本得了:
```
编码视频文件为 HEVC CRF 18MP4格式。
位置参数:
files 单独的视频文件(与 -d 互斥,二选一)
选项:
-h, --help 鹰文帮助
-d, --dir SRCDIR 源文件夹(会在里面搜索指定后缀的文件,与 files 互斥)
-o, --outdir OUTDIR 输出文件夹(默认原地,跟 -d 合用会保留相对路径)
-f, --ext EXTENSIONS 指定的后缀名(.mp4 和 mp4 均可,可重复指定多个后缀,不区分大小写)
-r, --rm-original 跑完是否把原文件删了
-s, --silent 静默(不显示进度条)
```
## 背景
实际就是命令行:
```sh
ffmpeg -i input.mp4 -c:v libx265 -crf 18 -preset medium -c:a copy -tag:v hvc1 output.mp4
```
写那么多 Python 封装只是为了路径处理和好看一点的终端输出。
而在 Windows 里文件所有权并不复杂(当前用户通常都可以读写),我更倾向于用 PowerShell
```powershell
get-childitem path -File -Recurse -Filter *.mp4 | foreach {
cmps_hevc $_.FullName # 依旧 ffmpeg也就变换一下 input.mp4 output.mp4
rename-item $_.FullName "$($_.Name).bak"
}
```
就算后续还要筛选(好比有的视频压制完比原文件还大),也比 Python 方便得多。