Python Script

Batch Subtitle Word Editor

This powerful Python script automates the tedious task of removing specific, unwanted words or phrases (like "[ Applause ]" or other annotations) from multiple .srt subtitle files at once. It processes an entire folder, including all its subfolders, and saves the cleaned files in a new 'updated' directory, ensuring your original files are never modified.

How to Use This Script:

  1. Save the Script: Click the "Copy Script" button to copy the Python code. Paste it into a text editor and save the file as `srt_word_deleter.py`.
  2. Organize Your Files: Place all the `.srt` files you wish to edit into a single main folder. The script will automatically find all of them.
  3. Run from Terminal: Open your terminal or command prompt, navigate to the directory where you saved the script, and run it with the command: python srt_word_deleter.py
  4. Follow the Prompts: The script will ask for the path to your main SRT folder and the exact words you want to delete. You can enter multiple words separated by a semicolon (e.g., `[ Music ]; [ Laughter ]`).
  5. Find Your Clean Files: After processing, a new folder named "updated" will appear in your original folder, containing all the modified subtitle files with their original folder structure intact.

import os

def delete_words_in_srt_files():
    """
    Looks through all .srt files in a specified folder (and its subfolders),
    deletes certain words, and saves the updated files in an "updated" subfolder.
    """

    source_directory = input("Enter the path to the folder containing the .srt files: ")

    if not os.path.isdir(source_directory):
        print(f"Error: Directory '{source_directory}' not found.")
        return

    words_to_delete_input = input(
        "Enter the words to delete (separate multiple words with a semicolon ';'): "
    )
    words_to_delete = [w.strip() for w in words_to_delete_input.split(';') if w.strip()]

    if not words_to_delete:
        print("No words to delete were provided. Exiting.")
        return

    words_to_delete.sort(key=len, reverse=True)

    print(f"\nWords to delete: {words_to_delete}")
    print("\nStarting file processing...")
    print("NOTE: Deletion is case-sensitive. E.g., 'Word' will not delete 'word'.")

    updated_files_count = 0

    for root, _, files in os.walk(source_directory):
        for filename in files:
            if filename.lower().endswith(".srt"):
                filepath = os.path.join(root, filename)
                print(f"Processing: {filepath}")

                try:
                    with open(filepath, 'r', encoding='utf-8') as f:
                        content = f.read()

                    original_content = content

                    for word in words_to_delete:
                        content = content.replace(word, '')

                    if content != original_content:
                        relative_path = os.path.relpath(root, source_directory)
                        updated_subfolder_path = os.path.join(source_directory, "updated", relative_path)
                        os.makedirs(updated_subfolder_path, exist_ok=True)

                        output_filepath = os.path.join(updated_subfolder_path, filename)

                        with open(output_filepath, 'w', encoding='utf-8') as f:
                            f.write(content)
                        print(f"  -> Updated and saved to: {output_filepath}")
                        updated_files_count += 1
                    else:
                        print("  -> No changes needed.")

                except Exception as e:
                    print(f"  -> Error processing {filepath}: {e}")

    print(f"\nProcessing complete. {updated_files_count} files were updated.")
    print(f"Updated files are saved in the 'updated' subfolder within '{source_directory}'.")

if __name__ == "__main__":
    delete_words_in_srt_files()