Last active
September 10, 2025 09:54
-
-
Save TomHeinemeyer/74807031bea9b2b356f29fa580b69e3f to your computer and use it in GitHub Desktop.
This script recursively sets the default language for all MKVs in the path it is executed in. It also unsets the default flag for any subtitles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pathlib | |
| import subprocess | |
| import simplejson as json | |
| files = [*pathlib.Path('.').glob('**/*.mkv')] | |
| total_items = len(files) | |
| unprocessed_items = [] | |
| languages = {} | |
| for file_index, file in enumerate(files): | |
| mediainfo = subprocess.run(['mediainfo', file, '--Output=JSON'], capture_output=True, text=True).stdout | |
| parsed_mediainfo = json.loads(mediainfo) | |
| tracks = parsed_mediainfo['media']['track'] | |
| audio_tracks = [i for i in tracks if i['@type'] == 'Audio'] | |
| subtitle_tracks = [i for i in tracks if i['@type'] == 'Text'] | |
| # Calculating the percentage based on the current index, casting | |
| # the resulting float to a string and cutting it to 2 decimals. | |
| progress_percentage = f'{(file_index + 1) / total_items * 100:.2f}' | |
| progress = f'Processed {progress_percentage}% of {total_items} items' | |
| if len(audio_tracks) < 2: | |
| unprocessed_items.append(file) | |
| print(progress) | |
| continue | |
| args = [] | |
| for audio_track in audio_tracks: | |
| language = audio_track['Language'] | |
| uid = audio_track['UniqueID'] | |
| is_default_track = audio_track['Default'] == 'Yes' | |
| # Taking advantage of the uniqueness of keys in a dictionary to map all the found languages. | |
| languages[language] = '' | |
| if language == 'en' and not is_default_track: | |
| args.append(f'--edit track:={uid} --set flag-default=1') | |
| elif language != 'en' and is_default_track: | |
| args.append(f'--edit track:={uid} --set flag-default=0') | |
| for subtitle_track in subtitle_tracks: | |
| is_default_track = subtitle_track['Default'] == 'Yes' | |
| if not is_default_track: | |
| continue | |
| uid = subtitle_track['UniqueID'] | |
| args.append(f'--edit track:={uid} --set flag-default=0') | |
| if len(args) == 0: | |
| print(progress) | |
| continue | |
| command = ['mkvpropedit', f'"{file}"'] | |
| command.extend(args) | |
| separator = " " | |
| command = separator.join(command) | |
| subprocess.run(command, shell=True) | |
| print(progress) | |
| print('\nFound languages:') | |
| for language in languages: | |
| print(f'- {language}') | |
| if len(unprocessed_items) > 0: | |
| print('\nFound items with no second language:') | |
| for unprocessed_item in unprocessed_items: | |
| print(f'- {str(unprocessed_item)}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| simplejson==3.20.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment