Skip to content

Commit b9d4420

Browse files
FEAT: Add option to remove other audio tracks after setting default in Default Audio Track Switcher
1 parent e1615c9 commit b9d4420

File tree

1 file changed

+15
-8
lines changed
  • Default Audio Track Switcher

1 file changed

+15
-8
lines changed

Default Audio Track Switcher/main.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
Created : 2025-10-26
77
Description :
88
This script recursively searches for video files in the specified input directory
9-
and inverts their default audio track, making the non-default track the default.
9+
and sets the default audio track, optionally removing other audio tracks.
1010
When more than two audio tracks are detected, the script prompts the user to
1111
manually select which one should become the default.
1212
1313
Key features include:
1414
- Recursive search for video files with specified extensions
1515
- Automatic detection of audio tracks using ffprobe
16-
- Swapping the default and secondary audio track
16+
- Setting the default audio track (prioritizing English tracks)
17+
- Optional removal of other audio tracks after setting the default
1718
- Manual selection of default track for videos with more than two audio tracks
1819
- Progress bar visualization for processing
1920
- Integration with ffmpeg for audio track manipulation
2021
2122
Usage:
2223
1. Set the INPUT_DIR constant to the folder containing your video files.
23-
2. Execute the script:
24+
2. Optionally set REMOVE_OTHER_AUDIO_TRACKS to True to remove other audio tracks after setting the default.
25+
3. Execute the script:
2426
$ python swap_audio_tracks.py
25-
3. Select the desired audio track when prompted (if more than two exist).
27+
4. Select the desired audio track when prompted (if more than two exist).
2628
2729
Outputs:
2830
- Video files in the same directory with swapped or updated default audio track
@@ -69,6 +71,7 @@ class BackgroundColors: # Colors for the terminal
6971
VERBOSE = False # Set to True to output verbose messages
7072
INPUT_DIR = "./Input/" # Root directory to search for videos
7173
VIDEO_FILE_EXTENSIONS = [".mkv", ".mp4", ".avi"] # List of video file extensions to process
74+
REMOVE_OTHER_AUDIO_TRACKS = False # Set to True to remove other audio tracks after setting the default
7275

7376
# Sound Constants:
7477
SOUND_COMMANDS = {"Darwin": "afplay", "Linux": "aplay", "Windows": "start"} # The commands to play a sound for each operating system
@@ -344,6 +347,7 @@ def determine_default_track(audio_tracks, video_path):
344347
def apply_audio_track_default(video_path, audio_tracks, default_track_index):
345348
"""
346349
Apply the default audio track disposition to the video file using ffmpeg.
350+
Optionally removes other audio tracks if REMOVE_OTHER_AUDIO_TRACKS is True.
347351
348352
:param video_path: Path to the video file
349353
:param audio_tracks: List of audio track strings
@@ -358,12 +362,15 @@ def apply_audio_track_default(video_path, audio_tracks, default_track_index):
358362
video_path = video_path if video_path.endswith(ext) else os.rename(video_path, root + ext) or (root + ext) # Rename if needed
359363
temp_file = root + ".tmp" + ext # Temporary file path with correct extension order
360364

361-
cmd = ["ffmpeg", "-y", "-i", video_path, "-map", "0", "-c", "copy"] # Base ffmpeg command (with overwrite flag)
365+
if REMOVE_OTHER_AUDIO_TRACKS: # If removing other audio tracks
366+
cmd = ["ffmpeg", "-y", "-i", video_path, "-map", "0:v", "-map", f"0:a:{default_track_index}", "-c", "copy", temp_file]
367+
else: # If keeping all audio tracks
368+
cmd = ["ffmpeg", "-y", "-i", video_path, "-map", "0", "-c", "copy"] # Base ffmpeg command to copy all streams
362369

363-
for i in range(num_tracks): # For each audio track
364-
cmd += ["-disposition:a:" + str(i), "0"] # Clear all dispositions
370+
for i in range(num_tracks): # For each audio track
371+
cmd += ["-disposition:a:" + str(i), "0"] # Clear all dispositions
365372

366-
cmd += ["-disposition:a:" + str(default_track_index), "default", temp_file] # Set the selected track as default and define output file
373+
cmd += ["-disposition:a:" + str(default_track_index), "default", temp_file] # Set the selected track as default and define output file
367374

368375
verbose_output(f"{BackgroundColors.GREEN}Executing ffmpeg command:{BackgroundColors.CYAN} {' '.join(cmd)}{Style.RESET_ALL}") # Output the ffmpeg command if verbose is True
369376

0 commit comments

Comments
 (0)