Created
April 11, 2025 09:38
-
-
Save basdp/4d1602e1d60185ab0759abf68d0750df to your computer and use it in GitHub Desktop.
Remove foreign languages from MKV file(s), e.g. Russian, Ukrainian, etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Ensure mkvmerge is installed | |
if ! command -v mkvmerge &> /dev/null; then | |
echo "Error: mkvmerge is not installed. Please install it first." | |
exit 1 | |
fi | |
# Ensure jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "Error: jq is not installed. Please install it first." | |
exit 1 | |
fi | |
# Check if an input file is provided | |
if [ "$#" -lt 1 ]; then | |
echo "Usage: $0 <input.mkv>" | |
exit 1 | |
fi | |
for INPUT_FILE in "$@"; do | |
#OUTPUT_FILE="filtered_${INPUT_FILE}" | |
TMP_DIR="$(dirname "${INPUT_FILE}")" | |
OUTPUT_FILE="$(mktemp --suffix=.mkv --tmpdir="${TMP_DIR}")" | |
echo "Processing: $INPUT_FILE" | |
# Get track info from mkvmerge JSON output | |
TRACKS_JSON=$(mkvmerge -J "$INPUT_FILE") | |
audio_tracks=($(echo "$TRACKS_JSON" | jq -r '.tracks[] | select(.type == "audio" and (.properties.language == "eng" or .properties.language == "dut")) | .id')) | |
subtitle_tracks=($(echo "$TRACKS_JSON" | jq -r '.tracks[] | select(.type == "subtitles" and (.properties.language == "eng" or .properties.language == "dut")) | .id')) | |
echo "Remaining audio tracks: ${audio_tracks[@]}" | |
echo "Remaining subtitle tracks: ${subtitle_tracks[@]}" | |
# Prepare track arguments for mkvmerge | |
track_args=() | |
if [ ${#audio_tracks[@]} -gt 0 ]; then | |
track_args+=("-a" "$(IFS=,; echo "${audio_tracks[*]}")") | |
fi | |
if [ ${#subtitle_tracks[@]} -gt 0 ]; then | |
track_args+=("-s" "$(IFS=,; echo "${subtitle_tracks[*]}")") | |
fi | |
# Run mkvmerge to create the filtered file | |
mkvmerge -o "$OUTPUT_FILE" --no-attachments "${track_args[@]}" "$INPUT_FILE" | |
echo "Writing merged file back to $INPUT_FILE..." | |
mv --force "$OUTPUT_FILE" "$INPUT_FILE" | |
echo "Finished $INPUT_FILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment