A script that scans a directory and its subdirectories for MKV files containing a specific language in their tracks. You can use mkvmerge or mkvinfo to extract track information and a shell script or Python script to automate the process.

A bash script that does this:


Bash Script Example

#!/bin/bash

# Directory to scan (default: current directory)
SEARCH_DIR="${1:-.}"

# Language to search for (e.g., "fre" for French)
LANGUAGE="fre"

# Temporary file to store results
RESULT_FILE="found_files.txt"

# Clear result file
> "$RESULT_FILE"

# Function to check for the language in a file
check_language() {
    local file="$1"
    if mkvmerge -i "$file" | grep -q "language: $LANGUAGE"; then
        echo "$file" >> "$RESULT_FILE"
    fi
}

# Scan directory recursively
find "$SEARCH_DIR" -type f -name "*.mkv" | while read -r file; do
    check_language "$file"
done

echo "Files containing language '$LANGUAGE' listed in $RESULT_FILE."

How to Use:

  1. Save the script as scan_mkv_language.sh.
  2. Make it executable:
    chmod +x scan_mkv_language.sh
    
  3. Run the script, specifying the directory to scan and the language to search for:
    ./scan_mkv_language.sh /path/to/directory
    

By default, it searches the current directory (.). To modify the language, edit the LANGUAGE="fre" line or pass it as an argument.


Python Script Example

If you prefer Python, here’s an equivalent script:

import os
import subprocess

# Directory to scan
search_dir = input("Enter the directory to scan (default: current directory): ") or "."
# Language to search for
language = input("Enter the language code to search for (e.g., 'fre' for French): ")

# File to store results
result_file = "found_files.txt"

def check_language(file_path, language):
    try:
        # Run mkvmerge to get track info
        output = subprocess.check_output(["mkvmerge", "-i", file_path], text=True)
        # Check if the language is in the output
        return f"language: {language}" in output
    except subprocess.CalledProcessError:
        return False

# Scan directory recursively
with open(result_file, "w") as result:
    for root, _, files in os.walk(search_dir):
        for file in files:
            if file.endswith(".mkv"):
                file_path = os.path.join(root, file)
                if check_language(file_path, language):
                    result.write(file_path + "\n")

print(f"Files containing language '{language}' listed in {result_file}.")

How to Use:

  1. Save the Python script as scan_mkv_language.py.
  2. Run the script:
    python3 scan_mkv_language.py
    
  3. Enter the directory to scan and the language code (e.g., fre for French).

Notes:

  • Both scripts rely on MKVToolNix (mkvmerge) to read MKV track information.
  • Modify the language code (LANGUAGE or language variable) for your needs.
  • Results will be stored in found_files.txt in the script’s directory.