I record my screen recordings as mkv files because it works for my process.
I use a bash script file I called mkv_to_gif.sh to deal with this (don't forget to make the file executable). At the end the total megapixels maybe more than 50 but Blender Stackexchange still allows posting.
Note: I only tested using Ubuntu 20.04 but it's very quick.
Tested with the above animated images as mkv files and converted to animated gifs using script below.
Note: I converted animated images to mkv files first using the code below.
ffmpeg -i test1.gif -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags faststart -pix_fmt yuv420p test1.mkv
ffmpeg -i test2.gif -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags faststart -pix_fmt yuv420p test2.mkv


#!/bin/bash
# Jan 02 2025
# Tested with 400x714, may need to limit to 640x480 or less
# Run using ./mkv_to_gif.sh animated_mkv.mkv
input_file="$1"
output_file="${input_file%.*}.gif"
# Get original dimensions and frame count
dimensions=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=width,height,nb_read_packets -of csv=p=0 "$input_file")
width=$(echo $dimensions | cut -d',' -f1)
height=$(echo $dimensions | cut -d',' -f2)
frames=$(echo $dimensions | cut -d',' -f3)
# Calculate total megapixels (width * height * frames)
calculate_total_megapixels() {
awk "BEGIN {printf \"%.2f\", $1 * $2 * $3 / 1000000}"
}
total_megapixels=$(calculate_total_megapixels $width $height $frames)
echo "Original dimensions: ${width}x${height}"
echo "Original frames: $frames"
echo "Original total megapixels: $total_megapixels"
# Set initial fps and colors
fps=15
colors=256
# Function to reduce dimensions
reduce_dimensions() {
local factor=0.99 # Reduced the factor to make significant dimension changes
width=$(awk "BEGIN {print int($width * $factor)}")
height=$(awk "BEGIN {print int($height * $factor)}")
total_megapixels=$(calculate_total_megapixels $width $height $frames)
}
# Reduce dimensions if initially over 50 megapixels
while (( $(echo "$total_megapixels > 50" | bc -l) )); do
reduce_dimensions
done
# Convert to GIF
ffmpeg -i "$input_file" -vf "fps=$fps,scale=$width:$height:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=$colors[p];[s1][p]paletteuse" -y "$output_file"
# Check file size and reduce quality if necessary
while [ $(stat -c%s "$output_file") -gt 2044723 ] || (( $(echo "$total_megapixels > 50" | bc -l) )); do
if (( $(echo "$total_megapixels > 50" | bc -l) )); then
reduce_dimensions
elif [ $fps -gt 5 ]; then
fps=$((fps - 1))
frames=$((frames * fps / 15)) # Adjust frame count based on new fps
elif [ $colors -gt 64 ]; then
colors=$((colors / 2))
else
reduce_dimensions
fi
total_megapixels=$(calculate_total_megapixels $width $height $frames)
ffmpeg -i "$input_file" -vf "fps=$fps,scale=$width:$height:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=$colors[p];[s1][p]paletteuse" -y "$output_file"
done
# Display final dimensions and file size
if command -v identify >/dev/null 2>&1; then
final_dimensions=$(identify -format "%wx%h" "$output_file")
final_width=${final_dimensions%x*}
final_height=${final_dimensions#*x}
final_frames=$(identify -format "%n\n" "$output_file" | wc -l)
final_total_megapixels=$(calculate_total_megapixels $final_width $final_height $final_frames)
echo "Final dimensions: $final_dimensions"
echo "Final frames: $final_frames"
echo "Final total megapixels: $final_total_megapixels"
else
echo "ImageMagick's 'identify' command not found. Unable to determine final dimensions and total megapixels."
fi
final_size=$(du -h "$output_file" | cut -f1)
echo "Final file size: $final_size"
Code Explanation:
This Bash script converts an MKV video file to a GIF format while optimizing its size and quality. Here's a breakdown of the code:
Input and Output:
- The script takes an MKV file as input and generates a GIF with the same name.
Initial Analysis:
- Uses ffprobe to get the original video dimensions and frame count.
- Calculates the total megapixels (width * height * frames).
Initial Settings:
- Sets initial fps (frames per second) to 15 and colors to 256.
Dimension Reduction:
- If the total megapixels exceed 50, it reduces the dimensions by a factor of 0.99 until it's below 50 megapixels.
Initial Conversion:
- Uses ffmpeg to convert the MKV to GIF with the current settings.
Quality Reduction Loop:
- If the resulting GIF is larger than ~2MB or exceeds 50 megapixels, it enters a loop to reduce quality:
a. Reduces dimensions if still over 50 megapixels.
b. Decreases fps if it's above 5.
c. Reduces color count if it's above 64.
d. If all else fails, reduces dimensions again.
- After each adjustment, it reconverts the video and checks the size again.
Final Output:
- If ImageMagick is available, it uses the 'identify' command to get the final dimensions and frame count.
- Displays the final dimensions, frame count, total megapixels, and file size.
This script aims to create a GIF that balances quality and file size, making iterative adjustments to achieve this balance.
Width × height × number of framesdefinitely seems to be it. I had a 400kB gif with 288 frames of 175,420 pixels each (that comes to 50,520,960 pixels) and got this error. I dropped about 7 frames to be safe, bringing it to 49,293,020 pixels…but I also increased the number of colors, bringing the total file size to ~750kB. The new, larger file was able to be uploaded. If the limit is actually useful, I don't mind it being in place…but it needs a more clear error message. $\endgroup$