Skip to content

Instantly share code, notes, and snippets.

@gphg
Last active August 22, 2024 06:03
Show Gist options
  • Save gphg/0b674d0b2e4561778c46b267017dd472 to your computer and use it in GitHub Desktop.
Save gphg/0b674d0b2e4561778c46b267017dd472 to your computer and use it in GitHub Desktop.
Scale down any video into 720p MP4 video. Required ffmpeg.
#!/bin/bash
#
# Small and simple bash script that reencode video into 720p (any orientation).
# Required ffmpeg binary on OS's $PATH (on Windows %PATH%) or current working directory.
#
# You can run this on Windows under a POSIX subsystem (MSYS2, CygWin, Bash from Git for Windows)
#
# Usage:
# 720pify.sh ../video1.mp4 somedir/video2.mp4
#
# Results are saved on cwd (current working directory)
# Set no prefix (empty) "" = will overwrite if source on cwd
FILE_PREFIX="scaled_"
# Set to 0 = loseless, very big. Suggested value is between 18 to 23.
# Set number to low if expecting the video will get reencoded again by some online services
# More information: https://superuser.com/a/1587523
BIT_CONSTANT=18
for v in "$@"
do
# Snips the source, save name as it is + prefix
f="${FILE_PREFIX}$(basename "$v")"
# Get source's width and height
w=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=nw=1:nk=1 "$v")
h=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nw=1:nk=1 "$v")
# Scale based on smaller number.
if [[ $h -gt $w ]]; then
ffmpeg -i "$v" -crf $BIT_CONSTANT -vf "scale=720:-2" "$f"
else
ffmpeg -i "$v" -crf $BIT_CONSTANT -vf "scale=-2:720" "$f"
fi
done
@gphg
Copy link
Author

gphg commented Aug 22, 2024

Use case: Discord video upload. On non-nitro users (free), video is scaled down to 720p (smaller file size). BUT it is still reflected by the source, eg if the source is somewhat bigger than maximum server's file size, then it fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment