Spaces:
Sleeping
Sleeping
Download normalize.py from summersomersault/SRTUploader: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/summersomersault/SRTUploader/resolve/main/normalize.py
- Command line
-
hf download hf://spaces/summersomersault/SRTUploader/normalize.py
-
curl -L -o normalize.py https://huggingface.co/spaces/summersomersault/SRTUploader/resolve/main/normalize.py
1.6 kB
| import re | |
| import os | |
| def normalize_name(name: str, is_filename: bool = False) -> str: | |
| """ | |
| Normalize both filenames and titles for matching | |
| Examples: | |
| $_PA_pecg_Le_1_2.srt → pa-pecg-le-1-2 | |
| $_TT_amadi_Le_1_4_otter_ai.srt → tt-amadi-le-1-4 | |
| $_TT_amadi_Le_3_0_v1 (240p)_otter_ai.srt → tt-amadi-le-3-0 | |
| $_TT_amadi_Le_3_0_v1_otter_ai.srt → tt-amadi-le-3-0 | |
| $_TT_amadi_Le_3_0 (240p)_otter_ai.srt → tt-amadi-le-3-0 | |
| $-PA-pecg-Le-1-2_otter-ai → pa-pecg-le-1-2 | |
| Args: | |
| name: Filename or video title | |
| is_filename: If True, removes file extension first | |
| """ | |
| # Remove extension if it's a filename | |
| if is_filename: | |
| name = os.path.splitext(name)[0] | |
| # Remove video file extensions that sometimes appear in Vimeo titles (.mp4, .mov, .avi, etc.) | |
| name = re.sub(r'\.(mp4|mov|avi|mkv|flv|wmv|m4v)$', '', name, flags=re.IGNORECASE) | |
| # Remove $ prefix (keep the initials like PA, TT, etc.) | |
| # Handles $_, $-, and "$ " (space) separators | |
| name = re.sub(r'^\$[_\- ]', '', name) | |
| # Remove version suffixes like _v1, _v2, etc. | |
| name = re.sub(r'_v\d+', '', name) | |
| # Remove video quality suffixes like (240p), (720p), (1080p), etc. | |
| name = re.sub(r'\s*\(\d+p\)', '', name) | |
| # Remove _otter_ai or _otter-ai suffix (handle both) | |
| name = re.sub(r'_otter[_-]ai$', '', name) | |
| # Normalize separators (_, -, and spaces all become -) | |
| name = re.sub(r'[_ -]+', '-', name) | |
| # Clean up leading/trailing dashes | |
| name = name.strip('-') | |
| return name.lower() |