BASH Post Services

Viewing: 1748893717_mp4crop.sh

Try wget https://bash.commongrounds.cc/uploads/1748893717_mp4crop.sh from the console

Raw File Link

#!/bin/bash

# Check if required arguments are provided
if [ $# -ne 3 ]; then
    echo "Usage: $0 input_video.mp4 output_video.mp4 crop_percentage"
    echo "Example: $0 input.mp4 output.mp4 80"
    exit 1
fi

input_file="$1"
output_file="$2"
crop_percent="$3"

# Validate crop percentage
if ! [[ "$crop_percent" =~ ^[0-9]+$ ]] || [ "$crop_percent" -lt 1 ] || [ "$crop_percent" -gt 100 ]; then
    echo "Error: Crop percentage must be an integer between 1 and 100"
    exit 1
fi

# Check if input file exists
if [ ! -f "$input_file" ]; then
    echo "Error: Input file does not exist"
    exit 1
fi

# Check if ffmpeg is installed
if ! command -v ffmpeg &> /dev/null; then
    echo "Error: FFmpeg is not installed"
    exit 1
fi

# Get video dimensions using ffprobe
width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 "$input_file")
height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$input_file")

# Calculate target height based on percentage
target_height=$((height * crop_percent / 100))

# Ensure target height is even (required by some codecs)
if [ $((target_height % 2)) -ne 0 ]; then
    target_height=$((target_height - 1))
fi

# Calculate target width to maintain 16:9 aspect ratio
target_width=$((target_height * 16 / 9))

# Ensure target width is even
if [ $((target_width % 2)) -ne 0 ]; then
    target_width=$((target_width - 1))
fi

# Calculate crop offsets to center the crop
x_offset=$(((width - target_width) / 2))
y_offset=$(((height - target_height) / 2))

# Run FFmpeg crop command
ffmpeg -i "$input_file" -vf "crop=$target_width:$target_height:$x_offset:$y_offset" -c:a copy "$output_file"

if [ $? -eq 0 ]; then
    echo "Video cropped successfully to $output_file"
else
    echo "Error: Video cropping failed"
    exit 1
fi
BASH to Home