Луна

Video Converter for macOS in TERMINAL
This script automatically compresses videos in .mp4, .mov, .avi, or .mkv format to reduce file size while preserving good visual quality.
Outputs H.265 .mp4 files into a converted_target folder.

It works by using CRF-based compression (constant quality), instead of targeting a fixed file size. This means the output size depends on the video content and complexity. The result is a visually balanced .mp4 file that’s smaller and efficient, while still looking good.


Integration

▶️ Step 1 Install in TERMINAL – ffmpeg via Homebrew (once):
To install it, just paste in terminal this code- brew install ffmpeg
▶️ Step 2 Create a folder on your Desktop (e.g. convert-videos) and drop your videos inside.
▶️ Step 3 Open TextEdit → Set format to Plain Text
→ Copy the SCRIPT BELOW and Paste it in the TextEdit
→ Save it with name: encode_x265.sh
→ Make sure the file is placed inside the convert-videos folder on your Desktop.
▶️ Step 4 Make it executable in Terminal, Paste this in terminal:
chmod +x ~/Desktop/convert-videos/encode_x265.sh
▶️ Step 5 Run this in terminal:
cd ~/Desktop/convert-videos && ./encode_x265.sh

NEED Faster compression or Better Quality?
In order to change speed or compression, look in the SCRIPT for -preset and change it:
-preset slow = best compression (smallest file), but very slow
-preset medium = best balance
-preset fast = faster, but slightly bigger file

The script will create a folder named converted_target and place compressed .mp4 files inside. It automatically processes all videos in the folder.

Output:
Works with different video sizes
A new folder named converted_target/ will appear in the same location
Inside it, you’ll find compressed .mp4 files

NOTE:
This script is focused on good quality and smaller file size, not fixed size.
Output size depends on the video content and CRF setting.
Slower presets give smaller files and better compression.
If a video is already compressed, it may not get smaller and quality can be reduced.


Step 3 Video Compressor Script

Make sure this script is saved as a Text file on your Desktop.
→ Open TextEdit
→ Set format to Plain Text
→ Paste the SCRIPT BELOW
→ Save it as: encode_x265.sh
→ Make sure the file is placed inside the convert-videos folder on your Desktop.


#!/bin/bash
set -euo pipefail
shopt -s nullglob
mkdir -p converted_target

# Best balance between quality/size:
# 22 = better quality, larger file
# 24 = very good balance
# 26 = smaller file, but more loss
CRF=24

# Audio
AUDIO_KBPS=128

# Select the best available encoder
if ffmpeg -hide_banner -encoders 2>/dev/null | grep -q 'libx265'; then
  VIDEO_MODE="libx265"
elif ffmpeg -hide_banner -encoders 2>/dev/null | grep -q 'hevc_videotoolbox'; then
  VIDEO_MODE="hevc_videotoolbox"
else
  VIDEO_MODE="h264_videotoolbox"
fi

echo "Using video encoder: $VIDEO_MODE"

for f in *.{mp4,mov,avi,mkv,MP4,MOV,AVI,MKV}; do
  [ -f "$f" ] || continue

  echo "--------------------------------------------------"
  echo "Processing: $f"

  out="converted_target/${f%.*}_converted.mp4"

  case "$VIDEO_MODE" in
    libx265)
      ffmpeg -y -hide_banner -loglevel error -stats \
        -i "$f" \
        -c:v libx265 \
        -crf "$CRF" \
        -preset fast \
        -pix_fmt yuv420p \
        -tag:v hvc1 \
        -c:a aac \
        -b:a "${AUDIO_KBPS}k" \
        -ar 48000 \
        -ac 2 \
        -movflags +faststart \
        "$out"
      ;;
    hevc_videotoolbox)
      ffmpeg -y -hide_banner -loglevel error -stats \
        -i "$f" \
        -c:v hevc_videotoolbox \
        -q:v 65 \
        -pix_fmt yuv420p \
        -tag:v hvc1 \
        -c:a aac \
        -b:a "${AUDIO_KBPS}k" \
        -ar 48000 \
        -ac 2 \
        -movflags +faststart \
        "$out"
      ;;
    h264_videotoolbox)
      ffmpeg -y -hide_banner -loglevel error -stats \
        -i "$f" \
        -c:v h264_videotoolbox \
        -b:v 2500k \
        -maxrate 3500k \
        -bufsize 7000k \
        -pix_fmt yuv420p \
        -c:a aac \
        -b:a "${AUDIO_KBPS}k" \
        -ar 48000 \
        -ac 2 \
        -movflags +faststart \
        "$out"
      ;;
  esac

  echo "Saved: $out"
done

echo "Done."