Луна

Video Converter for macOS in TERMINAL
This script automatically compresses videos in .mp4, .mov, .avi, or .mkv format to produce output files close to 50 MB, regardless of the original size or duration.
Outputs H.265 .mp4 files into a converted_target folder.

It works by calculating the ideal video bitrate based on the video’s duration, reserving a portion for audio, and using the remaining bandwidth for video quality. The output is a visually balanced .mp4 file that’s light enough to store, send, or upload – while still looking good.


Integration

▶️ Step 1 Install in TERMINAL – ffmpeg and bc via Homebrew (once):
To install it, just paste in terminal this code- brew install ffmpeg bc
▶️ Step 2 Create a folder on your Desktop (e.g. converting123) 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: convert_to_50mb.sh
→ Make sure the file is placed inside the converting123 folder on your Desktop.
▶️ Step 4 Make it executable in Terminal, Paste this in terminal:
   chmod +x ~/Desktop/converting123/convert_to_50mb.sh
▶️ Step 5 Run this in terminal:
   cd ~/Desktop/converting123 && chmod +x convert_to_50mb.sh && ./convert_to_50mb.sh

NEED Highest or Lower Quality?
In order to get better, or lower video quality LOOK in the SCRIPT for – (30*8192) and change (30) to a higher or lower (number = quality).

The script will create a folder named converted_target and place compressed .mp4 files inside. It automatically adjusts quality per video length, so even short or long clips come out balanced in size.

Output:
Works even if your input file is much bigger or smaller – the script adjusts the bitrate dynamically
A new folder named converted_target/ will appear in the same location
Inside it, you’ll find .mp4 files ~50MB each


Step 3 Video Compressor Script

Make sure this script is in Text file in your desktop.
→ Open TextEdit
→ Set format to Plain Text
→ Paste the SCRIPT BELOW
→ Save it as: convert_to_50mb.sh
→ Make sure the file is placed inside the converting123 folder on your Desktop.


#!/bin/bash
mkdir -p converted_target

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

  dur=$(ffprobe -v error -select_streams v:0 -show_entries format=duration \
    -of default=noprint_wrappers=1:nokey=1 "$f")

  kbps=$(echo "(30*8192)/$dur - 64" | bc)

  ffmpeg -y -i "$f" \
    -vf "scale=640:-2:force_original_aspect_ratio=decrease,setsar=1:1" \
    -c:v libx265 -b:v "${kbps}k" -preset fast \
    -c:a aac -b:a 64k -ar 44100 -ac 1 \
    converted_target/"${f%.*}_converted.mp4"
done