Last active
August 17, 2025 21:43
-
-
Save pynappo/5acd57abcdbcaae5bf99657bec444a28 to your computer and use it in GitHub Desktop.
Reencoding mpvs to upload as discord clips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| datetime=$(date +%Y-%m-%d_%H-%M-%S) | |
| output_dir="$datetime" | |
| output_dir_discord="$datetime/discord-sized" | |
| if [[ ! -d "$output_dir_discord" ]]; then | |
| mkdir -p "$output_dir_discord" | |
| fi | |
| reencoded_files=() | |
| for file in *.mkv; do | |
| # Check if the file exists | |
| if [[ ! -f "$file" ]]; then | |
| echo "Error: File $file not found. Skipping." | |
| continue | |
| fi | |
| filename=$(basename "$file") | |
| filename="${filename%.*}" | |
| output_file="$output_dir_discord/${filename}.mp4" | |
| target_mb=$((9 * 1024 * 1024)) | |
| audio_bytes=$(ffprobe -v error -select_streams a:0 -show_entries packet=size -of default=nokey=1:noprint_wrappers=1 "$file" | awk '{s+=$1} END {print s}' ) | |
| duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file") | |
| video_kbps=$(echo "scale=0; (($target_mb - $audio_bytes) * 8 / $duration / 1024)" | bc) | |
| # Prevent video bitrate from being negative if audio is too large | |
| if ((video_kbps < 0)); then | |
| video_kbps=100 | |
| fi | |
| # Perform two-pass encoding | |
| ffmpeg -y -i "$file" -c:v libx264 -b:v "${video_kbps}k" -pass 1 -an -f null /dev/null && \ | |
| ffmpeg -y -i "$file" -c:v libx264 -b:v "${video_kbps}k" -pass 2 -c:a copy "$output_file" | |
| echo "Re-encoded: $file to $output_file" | |
| reencoded_files+=("$output_file") | |
| echo "Moved: $file to $output_dir" | |
| cp "$file" "$output_dir" | |
| done | |
| echo "Finished processing all files, output:" | |
| for encoded_file in "${reencoded_files[@]}"; do | |
| echo "$output_dir_discord/$encoded_file" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment