-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleansubtitles
More file actions
131 lines (94 loc) · 3.44 KB
/
cleansubtitles
File metadata and controls
131 lines (94 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""
cleansubtitlles: Remove style, formatting tags, and smileys from subtitle files.
This script scans the current directory for all .srt, .ass, and .ssa subtitle files.
It allows you to select which files to clean (or press Enter to clean all).
For each selected file, it removes:
- ASS/SSA style tags like {\an3}, {\b1}, etc.
- Any curly-brace tags { ... }
- HTML-like tags such as <font ...>, </font>, <b>, </b>, etc.
- Smiley emoticons like :-), :-(, :-O, :-/, ;-), etc.
The cleaned subtitles overwrite the original files.
Usage:
python3 cleansubtitlles
# Follow the prompt to select which subtitle files to clean.
This is useful for stripping out formatting/styles for compatibility or readability.
"""
import os
import sys
import re
from pathlib import Path
# ----------------------------------------
# Cleaning rules
# ----------------------------------------
def clean_subtitle_line(line):
# Remove style tags like {\an3}, {\b1}, etc.
line = re.sub(r'\{\\.*?\}', '', line)
# Remove any remaining curly-brace tags
line = re.sub(r'\{.*?\}', '', line)
# Remove HTML-like tags such as <font ...>, </font>, <b>, </b>, etc.
line = re.sub(r'<[^>]+>', '', line)
# Remove smileys/emoticons
line = re.sub(r'(:-?[()O/s&])|(:[pD])|(;-\?)|([;:][oO/-]?[D>p])|(;-\))', '', line)
return line.strip()
def clean_subtitle_file(input_path, output_path=None):
with open(input_path, 'r', encoding='utf-8', errors='ignore') as infile:
lines = infile.readlines()
cleaned_lines = [clean_subtitle_line(line) for line in lines]
if output_path is None:
output_path = input_path # overwrite file
with open(output_path, 'w', encoding='utf-8') as outfile:
for line in cleaned_lines:
outfile.write(line + '\n')
# ----------------------------------------
# File selection
# ----------------------------------------
def list_subtitle_files():
exts = ('.srt', '.ass', '.ssa')
files = [f for f in sorted(os.listdir()) if f.lower().endswith(exts)]
for idx, file in enumerate(files, 1):
print(f"{idx}: {file}")
return files
def expand_range(text):
""" Expand '1,3-5,7' → [1,3,4,5,7] """
result = []
for part in text.split(","):
part = part.strip()
if "-" in part:
a, b = part.split("-")
if a.isdigit() and b.isdigit():
result.extend(range(int(a), int(b) + 1))
elif part.isdigit():
result.append(int(part))
return result
def get_user_selection(files):
selection = input(
"Enter numbers of subtitle files to clean "
"(comma-separated, ranges OK), or press Enter for all: "
).strip()
# Press Enter → all files
if not selection:
return files
indices = expand_range(selection)
selected_files = [
files[i - 1] for i in indices if 1 <= i <= len(files)
]
return selected_files
# ----------------------------------------
# Main
# ----------------------------------------
def main():
files = list_subtitle_files()
if not files:
print("No subtitle files found in this folder.")
sys.exit(0)
selected_files = get_user_selection(files)
if not selected_files:
print("No valid files selected.")
sys.exit(0)
for file in selected_files:
clean_subtitle_file(file)
print(f"✔ Cleaned: {file}")
print("\nAll done!\n")
if __name__ == "__main__":
main()