Python Script

Python Script to Bulk Edit Subtitle Words

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 "Copy Script" below and save the code as `srt_word_deleter.py`.
  2. Organize Your Files: Place all the `.srt` files you want to edit into a single main folder.
  3. Run from Terminal: Open your terminal, navigate to the script's directory, and run it: python srt_word_deleter.py
  4. Follow the Prompts: The script will ask for the folder path and the words to delete (e.g., `[ Music ]; [ Laughter ]`).
  5. Find Your Clean Files: A new "updated" folder will appear, containing all modified subtitle files.

import os

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

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

    words_to_delete_input = input(
        "Enter words to delete (separate 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.")

    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.")

if __name__ == "__main__":
    delete_words_in_srt_files()