Last active
September 27, 2024 13:28
-
-
Save pandamoon21/7dac03ff8415a554093e9b3e1b666809 to your computer and use it in GitHub Desktop.
A simple python script to generate subtitles langcodes from specific folder & specific extension (optional) using pymediainfo
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
# Subtitle list generator | |
# Author: pandamoon21 | |
# Inspired by https://github.com/rsgrt/sublist | |
import subprocess | |
import sys | |
from pathlib import Path | |
from pymediainfo import MediaInfo | |
def iterate_folder(folder_path, target_ext): | |
if target_ext: | |
list_files = [ | |
x for x in folder_path.iterdir() if x.is_file() and x.suffix == f".{target_ext}" | |
] | |
else: | |
list_files = [x for x in folder_path.iterdir() if x.is_file()] | |
return list_files | |
def mediainfo_file(file): | |
return MediaInfo.parse(file) | |
def fetch_lang_list(media_info): | |
langcode_list = [] | |
for track in media_info.tracks: | |
if track.track_type == "Text": | |
track_lang = track.language | |
langcode_list.append(track_lang) | |
return langcode_list | |
def start(folder, target_ext): | |
final_list = [] | |
list_files = iterate_folder(folder, target_ext) | |
for n, file in enumerate(list_files): | |
print(f"Process: {n}/{len(list_files)}", end="\r") | |
langcode_list = fetch_lang_list(mediainfo_file(file)) | |
final_list.append({ | |
"name": Path(file).name, | |
"langcodes": sorted(langcode_list) | |
}) | |
return final_list | |
# Start! | |
try: | |
input_folder = sys.argv[1] # Input folder | |
except Exception: | |
input_folder = "" | |
try: | |
target_ext = sys.argv[2] # Target extension without dot, e.g. mp4, mkv, etc (optional) | |
except Exception: | |
target_ext = "" | |
if not input_folder: | |
folder_path = Path.cwd().absolute() | |
else: | |
folder_path = Path(input_folder).absolute() | |
# Get filename & subtitles list (dict) | |
final_list = start(folder_path, target_ext) | |
for x in final_list: | |
print(f"Name: {x['name']}") | |
print("Subs: {}".format(", ".join(x['langcodes']))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment