Last active
December 1, 2024 10:48
-
-
Save junkdog/07af7286a85f3a8da9bff73875364b8d to your computer and use it in GitHub Desktop.
ffmpeg qol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# | |
# Screen Recording Script | |
# ---------------------- | |
# | |
# Records a selected window on your X11 display using FFmpeg. The script prompts | |
# you to click on a window, then automatically starts recording it to ${PWD}/output.mp4. | |
# | |
# Prerequisites: | |
# - ZSH shell | |
# - FFmpeg | |
# - X11 environment | |
# - xwininfo utility | |
# | |
# Usage: | |
# 1. Run the script | |
# 2. Click on the window you want to record | |
# 3. Recording starts automatically | |
# 4. Press Ctrl+C to stop recording | |
# | |
# Output: | |
# Creates 'output.mp4' in the current directory. If the file exists, | |
# you'll be prompted before overwriting. | |
# | |
# Technical details: | |
# - Format: MP4 (H.264) | |
# - Framerate: 30 FPS | |
# - Quality: CRF 23 (balanced) | |
# - Pixel format: YUV420P | |
# - Resolution: Matches window (adjusted to even dimensions) | |
# | |
# The script includes checks for: | |
# - Valid window geometry | |
# - Even dimensions (required for YUV420P) | |
# - Existing file conflicts | |
function record() { | |
if [[ $geometry =~ ([0-9]+)x([0-9]+)[+]([0-9]+)[+]([0-9]+) ]]; then | |
local w=$match[1] | |
local h=$match[2] | |
local x=$match[3] | |
local y=$match[4] | |
fi | |
if [[ -z $w || -z $h || -z $x || -z $y ]]; then | |
echo "failed to parse geometry" | |
return 1 | |
fi | |
# ensure width and height are divisible by 2 | |
if (( w % 2 != 0 || h % 2 != 0 )); then | |
echo "Width ($w) and height ($h) must be divisible by 2 for yuv420p encoding" | |
return 1 | |
fi | |
# ensure output file doesn't exist | |
local output="output.mp4" | |
if [[ -f $output ]]; then | |
echo "Warning: $output already exists" | |
read -q "REPLY?Overwrite? (y/n) " | |
echo | |
[[ $REPLY =~ ^[Yy]$ ]] && rm $output || return 1 | |
fi | |
ffmpeg -f x11grab \ | |
-video_size ${w}x${h} \ | |
-framerate 30 \ | |
-i :0.0+${x},${y} \ | |
-c:v libx264 \ | |
-preset medium \ | |
-crf 23 \ | |
-pix_fmt yuv420p \ | |
output.mp4 | |
} | |
echo "select window to record" | |
geometry=$(xwininfo \ | |
| grep -e '-geometry' \ | |
| sed --regexp-extended 's/.*geometry[[:space:]]+(.+)/\1/' | |
) | |
record |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment