Find and replace exact chapter titles in mpv.
This script scans existing chapter titles and replaces them using an explicit, user-defined mapping. Only full, case-sensitive matches are modified. It is designed for controlled normalization of chapter names.
The script does not guess, partially match, or infer intent. If no exact match is found, nothing is changed.
The behavior of this script is controlled by user-editable variables at the top of the file:
-
WORD_MAP
A table defining exactfind → replacepairs.Example:
["Intro"] = "Opening" ["Credits"] = "End credits"Rules:
- Matching is full-title only
- Matching is case-sensitive
- Partial matches are ignored
-
AUTO_SAVE_MODE
Initial auto-save format (xmlorchap_txt) -
AUTO_RENAME_ENABLED
Whether replacements run automatically on file load (disabled by default) -
DEBOUNCE_DELAY
Delay (in seconds) before auto-renaming executes on file load Debounce does not affect manual runs
By editing WORD_MAP, you can adapt the script to:
- Different naming conventions
- Streaming-service labels
- Personal or library-wide standards
- Renames chapters using an explicit find → replace map
- Applies only exact, case-sensitive matches
- Renames multiple chapters in one pass
- Preserves chapter order
- Preserves chapter timestamps
- Leaves non-matching chapters untouched
- Auto-saves chapter files after renaming
- Integrates with the framework-wide auto-save toggle
A chapter is eligible for renaming if:
- Its title exactly matches a key in
WORD_MAP - Case must match exactly
- The entire title must match (no substrings)
Examples:
| Original title | Result |
|---|---|
Intro |
Opening |
intro |
unchanged |
Intro Scene |
unchanged |
Credits |
End credits |
credits |
unchanged |
This strict behavior avoids unintended renaming.
1. The current chapter list is read
2. Each chapter title is compared against WORD_MAP
3. Matching titles are replaced
4. All replacements occur in one operation
5. Chapters are auto-saved
If no matches are found, the script exits without changes.
When replacements occur, the OSD shows:
- Number of chapters renamed
- Which original titles were matched
Example:
Found & Replaced 3 chapters (Intro, Credits)
If no matches are found:
No chapters found to replace.
| Key | Action |
|---|---|
| Alt+F | Apply find & replace immediately |
| Ctrl+F | Toggle auto-rename on file load |
This script does not define a keybinding for toggling auto-save mode.
- Normalizing inconsistent chapter names
- Fixing capitalization differences
- Renaming service-specific labels
- Cleaning up chapters after detection
- Enforcing preferred wording across a library
Auto-save runs automatically after a successful rename operation.
Supported modes:
- XML (MKVToolNix compatible)
- CHAPTERxx TXT (mp4-compatible)
Auto-save mode is stored locally per script instance.
This script listens for the shared framework message: script-message toggle_auto_save_mode
When received:
- The local auto-save mode switches between XML ↔ CHAPTERxx TXT
- The new mode applies to subsequent rename operations
This script does not emit the toggle message itself.
The auto-save toggle is expected to be emitted by another script.
In the default framework setup:
- create_chapter.lua defines the Shift+X keybinding
- Pressing Shift+X broadcasts
toggle_auto_save_mode find_&_replace.luareacts to that broadcast
If create_chapter.lua is not loaded:
- Auto-save mode remains at its initial value
- Renaming still works
- Auto-save cannot be toggled at runtime
This dependency is intentional and keeps scripts loosely coupled.
When auto-save is enabled:
- Chapter files are rewritten immediately after renaming
- Output path is derived from the media filename
- Existing files are overwritten without confirmation
Formats:
- Matroska chapters (MKVToolNix compatible)
- Deterministic ChapterUID generation
- Generated edition is marked as the default edition
- Two-digit chapter numbering (CHAPTER01, CHAPTER02, …)
- Compatible with MP4 and MKV tooling
- Fully platform-independent
- No UI dialogs or OS-specific features are used
- create_chapter.lua (for Shift+X auto-save toggle broadcast)