Shell
4 mo. ago
A script to compress all videos in a directory using ffmpeg.
_#!/bin/bash
# Default valuesvcodec="libx265"acodec="aac"abitrate="128k"output_format="mkv"crf=28preset="medium"
# Dynamic video tag variableif [[ "$vcodec" == "libx265" ]]; then video_tag="-tag:v hvc1"elif [[ "$vcodec" == "libx264" ]]; then video_tag="-tag:v avc1"else video_tag=""fi
# Function to show usageusage() {echo "Usage: $0 [options]"echo "Options:"echo " --vcodec [libx264|libx265] Video codec (default: libx265)"echo " --acodec [aac|mp3] Audio codec (default: aac)"echo " --abitrate [128k|192k] Audio bitrate (default: 128k)"echo " --output-format [mp4|mkv] Output format (default: mkv)"echo " --crf [value] Constant Rate Factor (default: 28)"echo " --preset [preset] Encoding preset (default: medium)"echo " --help Show this help message"exit 1}
# Parse command-line argumentswhile [[ $# -gt 0 ]]; docase "$1" in --vcodec) vcodec="$2" shift 2 ;; --acodec) acodec="$2" shift 2 ;; --abitrate) abitrate="$2" shift 2 ;; --output-format) output_format="$2" shift 2 ;; --crf) crf="$2" shift 2 ;; --preset) preset="$2" shift 2 ;; --help) usage ;; *) echo "Unknown option: $1" usage ;;esacdone
# Create the output directory if it doesn't existmkdir -p output
# Process video filesfor file in *.{mp4,mov,avi,mkv}; do# Skip if no files are found[ ! -e "$file" ] && continue
# Get creation date from metadatatimestamp=$(ffprobe -v quiet -select_streams v:0 -show_entries format_tags=creation_time -of default=noprint_wrappers=1:nokey=1 "$file")
# Format the timestamp to your desired format YYYY-MM-DD-HH-MM-SSformatted_timestamp=$(date -j -f "%Y-%m-%dT%H:%M:%S" "${timestamp%.*}" "+%Y-%m-%d-%H-%M-%S" 2>/dev/null)
# If the timestamp is empty, use the current time instead[ -z "$formatted_timestamp" ] && formatted_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
# Compress the videoffmpeg -i "$file" -vcodec "$vcodec" -crf "$crf" -preset "$preset" -c:a aac "$acodec" -b:a "$abitrate" "$video_tag" "output/${formatted_timestamp}.${output_format}"done
# x265, aac, mkv./video_convert.sh
# x264, aac, mp4./video_convert.sh --vcodec libx264 --acodec aac --output-format mp4