Skip to content

Instantly share code, notes, and snippets.

@velrino
Created November 24, 2025 19:50
Show Gist options
  • Select an option

  • Save velrino/727891cfb3c0c06d9e69bbb469e375b3 to your computer and use it in GitHub Desktop.

Select an option

Save velrino/727891cfb3c0c06d9e69bbb469e375b3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to convert videos (mainly .mov) to .mp4 with efficient compression
# Usage: ./video.sh /path/to/video.mov
# ./video.sh "/Users/velrino/Documents/screenshots/Screen Recording 2025-11-24 at 15.58.38.mov"
# This script will:
# - Convert video to H.264/AAC MP4 format
# - Compress the file efficiently while maintaining good quality
# - Save the output to ~/Documents/videos-ffmpeg/
#
# Requirements: ffmpeg (install with: brew install ffmpeg)
# Check if a file was provided
if [ -z "$1" ]; then
echo "Usage: $0 <video_path>"
echo "Example: $0 ~/Desktop/video.mov"
exit 1
fi
# Input file
INPUT_FILE="$1"
# Check if file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File '$INPUT_FILE' not found!"
exit 1
fi
# Create output directory if it doesn't exist
OUTPUT_DIR="$HOME/Documents/videos-ffmpeg"
mkdir -p "$OUTPUT_DIR"
# Extract filename without extension
FILENAME=$(basename "$INPUT_FILE")
FILENAME_NO_EXT="${FILENAME%.*}"
# Output file
OUTPUT_FILE="$OUTPUT_DIR/${FILENAME_NO_EXT}.mp4"
# Check if ffmpeg is installed, if not, install it automatically
if ! command -v ffmpeg &> /dev/null; then
echo "⚠️ ffmpeg is not installed!"
echo "πŸ“¦ Installing ffmpeg via Homebrew..."
echo ""
# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
echo "❌ Error: Homebrew is not installed!"
echo "Please install Homebrew first: https://brew.sh"
exit 1
fi
# Install ffmpeg
brew install ffmpeg
# Check if installation was successful
if ! command -v ffmpeg &> /dev/null; then
echo "❌ Error: Failed to install ffmpeg!"
exit 1
fi
echo ""
echo "βœ… ffmpeg installed successfully!"
echo ""
fi
echo ""
echo "🎬 Converting video..."
echo ""
echo "πŸ“₯ Input: $INPUT_FILE"
echo "πŸ“¦ Output: $OUTPUT_FILE"
echo ""
echo ""
# Convert video with optimized settings
# -c:v libx264: H.264 codec (compatible and efficient)
# -crf 23: quality (18-28, where 23 is great balance quality/size)
# -preset slow: slower encoding but better compression
# -c:a aac: AAC audio codec
# -b:a 128k: audio bitrate 128kbps
# -movflags +faststart: optimize for web streaming
ffmpeg -i "$INPUT_FILE" \
-c:v libx264 \
-crf 23 \
-preset slow \
-c:a aac \
-b:a 128k \
-movflags +faststart \
"$OUTPUT_FILE"
# Check if conversion was successful
if [ $? -eq 0 ]; then
echo ""
echo ""
echo "βœ… Conversion completed successfully!"
echo ""
# Show file sizes
ORIGINAL_SIZE=$(du -h "$INPUT_FILE" | cut -f1)
NEW_SIZE=$(du -h "$OUTPUT_FILE" | cut -f1)
echo "πŸ“Š Original size: $ORIGINAL_SIZE"
echo "πŸ“Š New size: $NEW_SIZE"
echo "πŸ“‚ Location: $OUTPUT_FILE"
echo ""
else
echo ""
echo "❌ Error during conversion!"
exit 1
fi
@velrino
Copy link
Author

velrino commented Nov 24, 2025

start

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