You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
scripts/replace_anime_subs_asssrc.sh

79 lines
1.9 KiB

#!/bin/bash
# Ensure mkvmerge is installed
if ! command -v mkvmerge &> /dev/null; then
echo "Error: mkvmerge is not installed. Please install mkvtoolnix."
exit 1
fi
# Default directories
VIDEO_DIR=""
SUBTITLE_DIR=""
MUXED_DIR=""
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
--vidsrc)
VIDEO_DIR="$2"
shift 2
;;
--subsrc)
SUBTITLE_DIR="$2"
shift 2
;;
-o)
MUXED_DIR="$2"
shift 2
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Ensure required arguments are provided
if [[ -z "$VIDEO_DIR" || -z "$SUBTITLE_DIR" || -z "$MUXED_DIR" ]]; then
echo "Usage: $0 --vidsrc <video_directory> --subsrc <subtitle_directory> -o <output_directory>"
exit 1
fi
# Create output directory
mkdir -p "$MUXED_DIR"
# Process each video file
for video_file in "$VIDEO_DIR"/*.mkv; do
[[ -e "$video_file" ]] || continue
# Extract SxxExx pattern
episode_id=$(basename "$video_file" | grep -oE 'S[0-9]{2}E[0-9]{2}')
if [[ -z "$episode_id" ]]; then
echo "Skipping $video_file (no episode pattern found)"
continue
fi
# Find matching subtitle files
mapfile -t subtitle_files < <(find "$SUBTITLE_DIR" -type f -name "*${episode_id}*.ass")
if [[ ${#subtitle_files[@]} -eq 0 ]]; then
echo "Warning: No subtitle files found for $video_file"
continue
fi
# Define output file
output_file="$MUXED_DIR/${episode_id}.mkv"
echo "Muxing: $video_file + subtitles (${subtitle_files[*]}) -> $output_file"
# Mux using mkvmerge (video+audio from video file, all matching .ass subtitle files, remove existing subtitles)
mkvmerge -o "$output_file" \
--no-subtitles "$video_file" \
"${subtitle_files[@]}"
done
echo "Muxing complete."