Skip to content

Instantly share code, notes, and snippets.

@Luocy7
Created November 20, 2023 08:52
Show Gist options
  • Save Luocy7/922c31e2cb4d5dd4a275f1e313df8af7 to your computer and use it in GitHub Desktop.
Save Luocy7/922c31e2cb4d5dd4a275f1e313df8af7 to your computer and use it in GitHub Desktop.
batch script for removing old subtitles and adding ass format subtitles in the same directory as an MKV file
#!/bin/bash
# Set default values
current_dir="$(pwd)"
work_dir=$(gum input --placeholder "work_dir (default: $current_dir)")
test -z "$work_dir" && work_dir=$current_dir
with_subdir="true"
output_dir="$work_dir/output"
if [ ! -d "$output_dir" ]; then
# If not, create the directory
mkdir -p "$output_dir"
echo "Directory created: $output_dir"
else
echo "Directory already exists: $output_dir"
fi
# Function to process mkv files
process_mkv() {
local mkv_file="$1"
mkv_filename=$(basename "$mkv_file")
mkv_filename_no_suffix="${mkv_filename%.*}"
directory=$(dirname "$mkv_file")
ass_path="$directory/$mkv_filename_no_suffix.ass"
output_path="$output_dir/$mkv_filename_no_suffix.mkv"
if [ -e "$ass_path" ]; then
gum spin --spinner dot --title "Processing file: $mkv_file..." -- ffmpeg -hide_banner -loglevel warning -y \
-i "$mkv_file" -i "$ass_path" -c copy -map 0 -map -0:s -map 1 -metadata:s:s:0 language=chi "$output_path"
echo "$mkv_file -- Processed Done!"
fi
}
# Process mkv files in the given directory
process_files() {
local dir="$1"
for mkv_file in "$dir"/*.mkv; do
if [ -f "$mkv_file" ]; then
process_mkv "$mkv_file"
fi
done
}
# Process mkv files in subdirectories recursively
process_subdirs() {
local dir="$1"
# Process files in the current directory
process_files "$dir"
subdirs="$dir/*"
# Process subdirectories
if [ "$with_subdir" = "true" ]; then
for subdir in "${subdirs[@]}"; do
if [ -d "$subdir" ];then
process_subdirs "$subdir"
fi
done
fi
}
# Start processing
process_subdirs "$work_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment