How to batch compress videos and images from the command line on a Mac

Short answer

macOS already has two command-line tools for this: avconvert re-encodes videos to a preset such as 1280×720, and sips resizes or recompresses images, and both can be run in a for loop over a folder; FFmpeg, installed with Homebrew, gives finer control. None of them aims at a file size, so for “every file under 20 MB” in one line, install SendSize's sendsize command from Settings → Automation and run sendsize compress on the folder with --preset discord or --max-mb.

Compress without opening the window Watch folders, clipboard, links, command line

Videos with avconvert (built in)

avconvert comes with macOS and uses the Mac's own encoder. avconvert --help lists its presets. This loop makes a 720p copy of every MOV in the current folder:

for f in *.mov; do avconvert --source "$f" --preset Preset1280x720 --output "${f%.*}-720p.mp4"; done

Images with sips (built in)

sips changes the files you give it unless you add --out. Work on a copy of the folder. This makes every JPEG at most 2000 pixels on the long side and saves it at quality 70 into a folder called small:

mkdir -p small; for f in *.jpg; do sips -Z 2000 -s formatOptions 70 "$f" --out small/; done

Videos with FFmpeg

FFmpeg is free and open source; install it with brew install ffmpeg. CRF sets the quality instead of the size: 23 is the default, higher numbers give smaller files. The loop below keeps the resolution and re-encodes to H.264:

for f in *.mov; do ffmpeg -i "$f" -c:v libx264 -crf 26 -preset slow -c:a aac -b:a 128k "${f%.*}-small.mp4"; done

One command with a target size: sendsize

For videos it works out the bitrate from the length and encodes in two passes with x264, then tries a lower resolution if the file is still too big, up to 3 times. sendsize presets lists every use and sendsize with no arguments prints all options; the automation page explains each one.

  1. In SendSize, open Settings → Automation and click Install… under Terminal. It makes /usr/local/bin/sendsize, a link to the tool inside the app.
  2. Open a new Terminal window. sendsize compress ~/Movies/Trip --preset discord compresses every video, photo, GIF and PDF in the folder and its subfolders so each file is under 20 MB.
  3. Use --max-mb 8 for a size of your own, or --preset gmail for an email (under 25 MB). Without a preset it uses Compress: smaller, quality first, no target.
  4. Choose where results go: by default a new folder next to the originals; --suffix -small next to each file; --subfolder Smaller inside each folder; --out ~/Desktop/Send into one folder; --replace in place of the originals, which go to the Trash.

In scripts

  • Exit code 0 means every file worked, 1 that a file failed, 2 a wrong option.
  • Files SendSize already made are skipped, so a script that runs every night does not compress the same files again. Add --force to do them anyway.
  • One line per file is printed with the size before and after, then a summary.

What to keep in mind

  • The sendsize command follows the app's license: the free version compresses 5 files at a time and says so; the $19.99 license removes the limit.
  • avconvert and sips are part of macOS; their presets and options can change with macOS versions. avconvert --help and man sips show what your Mac has.
  • WebM and MKV are not converted in version 1.0.

Questions

How do I compress a video from the Mac command line without installing anything?

Use avconvert, which comes with macOS: avconvert --source in.mov --preset Preset1280x720 --output out.mp4.

How do I batch resize images in Terminal on a Mac?

sips -Z 2000 *.jpg resizes every JPEG in the folder to at most 2000 pixels on the long side. It overwrites the files unless you add --out.

How do I compress every file under a certain size from the command line?

FFmpeg and avconvert do not aim at a size. sendsize compress <folder> --max-mb 20 does, for videos, photos, GIFs and PDFs.

Where does the sendsize command come from?

It ships inside SendSize. Settings → Automation → Install… puts a link to it in /usr/local/bin, and Remove takes it away.

Sources

Do it with SendSize

Compress without opening the window

PresetWatch folders, clipboard, links, command line

  1. Watch folders and the clipboard: turn them on in the app (license).
  2. Links and the sendsize command: Settings → Automation.
  3. The results land where a normal batch would put them.

Free for 5 files at a time. $19.99 once for any number, on 3 Macs.