Skip to content

Instantly share code, notes, and snippets.

@neo22s
Created January 13, 2025 08:50
Show Gist options
  • Save neo22s/5f51735a9cdd10b1252aff0f81d7392e to your computer and use it in GitHub Desktop.
Save neo22s/5f51735a9cdd10b1252aff0f81d7392e to your computer and use it in GitHub Desktop.
Estimates the total resize in video conversion
#!/bin/bash
INPUT_FOLDER="/" #Aet the folder to analyze here
# Define the desired new bitrate (in kbps)
DESIRED_BITRATE=3000 # Adjust this value as needed
SAVINGS_THRESHOLD=20 # Minimum savings percentage required to process a file
# Initialize totals for files that meet processing criteria
total_original_size_to_process=0
total_estimated_size_to_process=0
total_duration_to_process=0 # Total duration in seconds for filtered files
# Global totals
total_original_size=0
total_estimated_size=0
total_duration=0
echo "File Path, Current Size (MB), Estimated New Size (MB), Savings (%), Size Change Indicator"
# Iterate through all valid .mp4 files
while IFS= read -r -d '' input_file; do
# Get the current size in bytes
current_size=$(stat -f%z "$input_file" 2>/dev/null)
if [[ -z "$current_size" || "$current_size" -eq 0 ]]; then
continue # Skip invalid files
fi
# Convert size to MB
current_size_mb=$(echo "scale=2; $current_size / (1024 * 1024)" | bc)
# Get the video duration in seconds
duration=$(ffprobe -v error -select_streams v:0 -show_entries stream=duration \
-of default=noprint_wrappers=1:nokey=1 "$input_file" 2>/dev/null)
if [[ -z "$duration" || "$duration" == "N/A" ]]; then
continue # Skip files with invalid durations
fi
# Calculate the estimated new size in bytes and MB
estimated_size_bytes=$(echo "($DESIRED_BITRATE * 1000) * $duration / 8" | bc)
estimated_size_mb=$(echo "scale=2; $estimated_size_bytes / (1024 * 1024)" | bc)
# Update global totals
total_original_size=$(echo "$total_original_size + $current_size" | bc)
total_estimated_size=$(echo "$total_estimated_size + $estimated_size_bytes" | bc)
total_duration=$(echo "$total_duration + $duration" | bc)
# Calculate savings percentage
if (( $(echo "$current_size_mb > 0" | bc -l) )); then
savings_percentage=$(echo "scale=2; (1 - $estimated_size_mb / $current_size_mb) * 100" | bc)
else
savings_percentage=0
fi
# Determine if the file should be processed based on the threshold
size_change_indicator="="
if (( $(echo "$savings_percentage >= $SAVINGS_THRESHOLD" | bc -l) )); then
size_change_indicator="-"
# Update totals for files worth processing
total_original_size_to_process=$(echo "$total_original_size_to_process + $current_size" | bc)
total_estimated_size_to_process=$(echo "$total_estimated_size_to_process + $estimated_size_bytes" | bc)
total_duration_to_process=$(echo "$total_duration_to_process + $duration" | bc)
elif (( $(echo "$estimated_size_mb > $current_size_mb" | bc -l) )); then
size_change_indicator="+"
savings_percentage=0 # No savings if size increases
fi
# Output the information for each file
echo "\"$input_file\", $current_size_mb, $estimated_size_mb, $savings_percentage%, $size_change_indicator"
done < <(find "$INPUT_FOLDER" -type f -name "*.mp4" ! -name ".*" -print0)
# Convert totals to GB
total_original_size_to_process_gb=$(echo "scale=2; $total_original_size_to_process / (1024 * 1024 * 1024)" | bc)
total_estimated_size_to_process_gb=$(echo "scale=2; $total_estimated_size_to_process / (1024 * 1024 * 1024)" | bc)
total_savings_to_process_gb=$(echo "scale=2; $total_original_size_to_process_gb - $total_estimated_size_to_process_gb" | bc)
# Estimated processing time for filtered files
average_processing_speed=1.5 # Adjust this multiplier based on your system
estimated_processing_time_to_process=$(echo "$total_duration_to_process * $average_processing_speed" | bc)
estimated_hours_to_process=$(echo "$estimated_processing_time_to_process / 3600" | bc)
remaining_seconds=$(echo "$estimated_processing_time_to_process % 3600" | bc)
estimated_minutes_to_process=$(echo "$remaining_seconds / 60" | bc)
# Summary for files worth processing
echo
echo "Summary for Files Worth Processing:"
echo "-----------------------------------------"
echo "Total Original Size: $total_original_size_to_process_gb GB"
echo "Total Estimated Size: $total_estimated_size_to_process_gb GB"
echo "Total Savings: $total_savings_to_process_gb GB"
echo "-----------------------------------------"
echo "Estimated Total Processing Time: ${estimated_hours_to_process}h ${estimated_minutes_to_process}m"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment