Skip to content

Instantly share code, notes, and snippets.

@FlyingFathead
Last active December 25, 2024 02:31
Show Gist options
  • Save FlyingFathead/358757d8ed0dd01e9cd183d67026a9d0 to your computer and use it in GitHub Desktop.
Save FlyingFathead/358757d8ed0dd01e9cd183d67026a9d0 to your computer and use it in GitHub Desktop.
Ffmpeg-based bash script to dump & recode all relevant portions of DVD-video ISO's in a directory
#!/bin/bash
# ffmpeg_dump-a-video-dvd.sh
# === INFO ===
# This is just a simple processing script that does the following:
#
# 1) Takes in all the ISO files in the directory; intended for video-DVD's.
# 2) Extracts the contents (according to the minimum duration; change the variable below).
# 3) Re-encodes the contents with HEVC/H.265, deinterlaces the video, and creates an output directory for each disc.
#
# The script is provided AS-IS, and is to be used AT YOUR OWN RISK.
# Use only for allowed backups of legitimate copies!
# FlyingFathead 2024 // https://github.com/FlyingFathead
# thanks as usual to ChaosWhisperer for digital ghost code
# === prerequisites ===
# (on Debian/Ubuntu tree Linux distros)
# $ sudo apt-get install p7zip-full
# $ sudo apt-get install ffmpeg
# $ sudo apt-get install bc
# $ sudo apt-get install libdvd-pkg
# === options ===
# Set the minimum duration in seconds (e.g., 20 minutes = 1200 seconds)
# This avoids extracting unneeded extras like title screens and such.
MIN_DURATION=1200
# Set the video and audio encoding options
VIDEO_CODEC="hevc_nvenc" # Video codec (e.g., hevc_nvenc for NVIDIA GPU encoding)
VIDEO_PRESET="slow" # Encoding speed/quality tradeoff (slower = better quality)
VIDEO_CQ=26 # Constant Quality (CQ) level (lower = better quality, larger file)
AUDIO_CODEC="aac" # Audio codec (e.g., aac)
AUDIO_BITRATE="128k" # Audio bitrate (e.g., 128k)
# Log file
LOG_FILE="iso_processing.log"
echo "Log started at $(date)" > "$LOG_FILE"
# Ensure the script is run from the directory with the ISO files
echo "Starting the ISO processing script in directory: $(pwd)" | tee -a "$LOG_FILE"
# Loop over each ISO file in the current directory
for iso in *.iso; do
output_dir="$(basename "$iso" .iso)_output"
echo "Processing ISO: $iso" | tee -a "$LOG_FILE"
mkdir -p "$output_dir" || { echo "Failed to create output directory $output_dir" | tee -a "$LOG_FILE"; continue; }
# Extract the ISO using 7z
echo "Extracting $iso to $output_dir" | tee -a "$LOG_FILE"
7z x "$iso" -o"$output_dir" 2>&1 | tee -a "$LOG_FILE"
if [ $? -ne 0 ]; then
echo "Failed to extract $iso" | tee -a "$LOG_FILE"
continue
fi
# Process each .VOB file in the extracted VIDEO_TS directory
for vob_file in "$output_dir/VIDEO_TS/"*.VOB; do
if [ -f "$vob_file" ]; then
duration=$(ffmpeg -i "$vob_file" 2>&1 | grep "Duration" | awk '{print $2}' | tr -d ',' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
if (( $(echo "$duration >= $MIN_DURATION" | bc -l) )); then
base_name=$(basename "$vob_file" .VOB)
output_file="$output_dir/${base_name}_h265_nvenc.mp4"
echo "Re-encoding $vob_file to $output_file with deinterlacing" | tee -a "$LOG_FILE"
ffmpeg -i "$vob_file" \
-vf yadif \
-c:v $VIDEO_CODEC \
-preset $VIDEO_PRESET \
-cq $VIDEO_CQ \
-c:a $AUDIO_CODEC \
-b:a $AUDIO_BITRATE \
"$output_file" 2>&1 | tee -a "$LOG_FILE"
if [ $? -ne 0 ]; then
echo "Failed to re-encode $vob_file" | tee -a "$LOG_FILE"
else
echo "Successfully re-encoded $vob_file to $output_file" | tee -a "$LOG_FILE"
fi
else
echo "Skipping $vob_file (duration: $duration seconds)" | tee -a "$LOG_FILE"
fi
else
echo "No .VOB files found in $output_dir/VIDEO_TS/" | tee -a "$LOG_FILE"
fi
done
# Remove the VIDEO_TS and AUDIO_TS directories as they are no longer needed
echo "Removing VIDEO_TS and AUDIO_TS directories from $output_dir" | tee -a "$LOG_FILE"
rm -rf "$output_dir/VIDEO_TS" "$output_dir/AUDIO_TS"
echo "Finished processing $iso" | tee -a "$LOG_FILE"
done
echo "All ISOs have been processed." | tee -a "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment