如何用 ffmpeg 和 cmdbook 壓縮螢幕錄影
Shrink a screen recording with ffmpeg from the command line
作者示範如何用 cmdbook 將 ffmpeg 螢幕錄影壓縮指令設成可重用表單,讓輸入檔和輸出寬度可透過介面選擇。寬度可選 1280、960 或 1920,並以 -2 計算偶數高度,避免編碼器因高度不是偶數而報錯。
You recorded your screen for a bug report or a demo. One minute, 285 MB. Too big to attach, too big to send.
How do you do it?
You ask an AI, and get ffmpeg -i in.mov out.mp4. It works: 46 MB. Better, still big.
You ask for smaller and get a scale=960:-1. On a window recording that's 1280×1001, it fails:
[libx264] height not divisible by 2 (960x751)
The video encoder needs even sizes, and -1 gave it 751. You ask again: it should be -2. Then you add -crf 28 for a smaller file, -pix_fmt yuv420p so it plays everywhere, and -movflags +faststart so it starts playing before it's fully downloaded.
You save it somewhere: a Markdown note, or a shrink.sh file. Next time it's another file, or you want it 1920 wide, and you have to find the script and edit the middle of the ffmpeg line.
With cmdbook, the script has one place to live, and it gets a GUI for free.
Here's how
1. Save the script once. Create a command in cmdbook and paste the script (it's at the end).
2. Mark what changes. The file and the width become placeholders:
IN="{{prompt:file}}"
...
-vf "scale={{prompt:width?1280|960|1920}}:-2"
1280|960|1920 becomes a dropdown. The first one is the default.
3. Click Run. cmdbook shows a form. Drag the recording onto it, pick a width, press Enter.
4. Get the small file. It's saved next to the original, with -small in the name:
285M Screen Recording.mov
6.0M Screen Recording-small.mp4
One minute of 1080p, from 285 MB to 6 MB, in 4 seconds on my Mac. The script drops the audio (-an), since screen recordings usually have none. If yours has sound, delete -an.
Next time, your last width is already selected.
That's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.
Try cmdbook here: printf.app/cmdbook
The full script (needs ffmpeg)
IN="{{prompt:file}}"
OUT="${IN%.*}-small.mp4"
ffmpeg -hide_banner -v error -stats -y -i "$IN" \
-vf "scale={{prompt:width?1280|960|1920}}:-2" \
-c:v libx264 -crf 28 -preset fast -pix_fmt yuv420p -movflags +faststart -an "$OUT"
du -h "$IN" "$OUT"
來源:DEV Community · dev.to
