Skip to content

Commit 9c1749a

Browse files
FEAT: Add option to delete other subtitle tracks after setting default in Default Subtitles Track Switcher
1 parent b9d4420 commit 9c1749a

File tree

1 file changed

+15
-11
lines changed
  • Default Subtitles Track Switcher

1 file changed

+15
-11
lines changed

Default Subtitles Track Switcher/main.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
This script scans all video files inside a specified directory and sets the
99
preferred subtitle track (e.g., Portuguese or Brazilian Portuguese) as the
1010
default subtitle. It keeps all existing audio and video streams unchanged.
11+
Optionally, it can remove other subtitle tracks after setting the default.
1112
1213
The script works by:
1314
- Detecting subtitle streams using ffprobe
1415
- Selecting a preferred subtitle track according to priority defined
1516
in the LANGUAGES dictionary
1617
- Removing all existing default dispositions from subtitle streams
1718
- Applying the "default" disposition to the chosen subtitle stream
19+
- Optionally removing other subtitle tracks
1820
- Saving changes to a temporary file and optionally replacing the original
1921
2022
This version no longer removes subtitle tracks; it only changes the default
@@ -38,10 +40,10 @@
3840
- Priority-based subtitle selection (e.g., pt-BR > pt > others)
3941
- Fully preserved audio and video streams
4042
- Default subtitle switch without re-encoding (stream copy)
43+
- Optional removal of other subtitle tracks
4144
- Optional sound notification when finished
4245
4346
TODOs:
44-
- Add removal of unwanted subtitle tracks
4547
- Support CLI arguments for input directory and options
4648
- Add logging and improved exception handling
4749
@@ -52,7 +54,7 @@
5254
- tqdm (for progress bar)
5355
5456
Assumptions & Notes:
55-
- Video and audio streams are untouched; only subtitle dispositions change
57+
- Video and audio streams are untouched; subtitle dispositions change and optionally other subtitles are removed
5658
- Supported formats: .mkv, .mp4, .avi, .mov
5759
- Works on Windows, Linux, and macOS (automatic FFmpeg installation included)
5860
"""
@@ -80,6 +82,7 @@ class BackgroundColors: # Colors for the terminal
8082
VERBOSE = False # Set to True to output verbose messages
8183
INPUT_DIR = r"./Input" # Path to the directory with video files
8284
DELETE_OLD_FILES = True # Set to True to replace original files with cleaned versions
85+
DELETE_OTHER_SUBTITLES = False # Set to True to delete other subtitle tracks after setting the default
8386
FILES_FORMAT = (".mkv", ".mp4", ".avi", ".mov") # Tuple of video file extensions to process
8487
LANGUAGES = { # Dictionary of languages and their possible subtitle codes
8588
"Portuguese": ["Brazilian", "Brazilian Portuguese", "Portuguese", "pt-BR", "pt", "pt-PT"], # Portuguese subtitle codes
@@ -271,12 +274,12 @@ def choose_default_subtitle(subtitle_streams):
271274

272275
def set_default_subtitle(full_path):
273276
"""
274-
On the given cleaned file (already produced by remove_unwanted_subtitles),
275-
clear dispositions of all subtitle streams and set the chosen subtitle as default.
277+
Clear dispositions of all subtitle streams and set the chosen subtitle as default.
278+
Optionally removes other subtitle tracks if DELETE_OTHER_SUBTITLES is True.
276279
Works in-place by producing a temporary file and optionally replacing the original
277280
if DELETE_OLD_FILES is True.
278281
279-
:param full_path: Path to the cleaned video file
282+
:param full_path: Path to the video file
280283
:return: True on success, False on failure
281284
"""
282285

@@ -293,12 +296,13 @@ def set_default_subtitle(full_path):
293296
base, ext = os.path.splitext(full_path) # Split file path into base and extension
294297
temp_output = f"{base}_default{ext}" # Temporary output file path
295298

296-
cmd = ["ffmpeg", "-i", full_path, "-map", "0", "-c", "copy"] # Base ffmpeg command to copy all streams
297-
298-
for s in subtitle_streams: # Loop through subtitle streams
299-
cmd += [f"-disposition:s:{s['sub_pos']}", "0"] # Clear disposition for all subtitle streams
300-
301-
cmd += [f"-disposition:s:{chosen_pos}", "default", temp_output, "-y"] # Set chosen subtitle as default and specify output file
299+
if DELETE_OTHER_SUBTITLES: # If we want to delete other subtitle tracks, map only video, audio, and the chosen subtitle, set it as default
300+
cmd = ["ffmpeg", "-i", full_path, "-map", "0:v", "-map", "0:a", "-map", f"0:s:{chosen_pos}", "-c", "copy", "-disposition:s:0", "default", temp_output, "-y"]
301+
else: # Just change dispositions, keep all streams
302+
cmd = ["ffmpeg", "-i", full_path, "-map", "0", "-c", "copy"] # Base ffmpeg command to copy all streams
303+
for s in subtitle_streams: # Loop through subtitle streams
304+
cmd += [f"-disposition:s:{s['sub_pos']}", "0"] # Clear disposition for all subtitle streams
305+
cmd += [f"-disposition:s:{chosen_pos}", "default", temp_output, "-y"] # Set chosen subtitle as default and specify output file
302306

303307
try: # Run ffmpeg command
304308
proc = subprocess.run(cmd, capture_output=True, text=True, check=True) # Capture output

0 commit comments

Comments
 (0)