Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Fixed color code revering issues and allow it to run on python 3.8.x #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
/test_smis/*.ass
__pycache__
.vscode
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you revert this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is automatically added by my IDE setting.

7 changes: 4 additions & 3 deletions build.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ fi

# Install the core packages.
pip install \
'PyInstaller==3.3.1' \
'beautifulsoup4==4.6.0' \
'chardet==3.0.4'
'PyInstaller===4.3' \
'beautifulsoup4===4.9.3' \
'chardet===4.0.0'

# Build the binary.
python -OO -m PyInstaller \
Expand Down Expand Up @@ -41,3 +41,4 @@ echo "sha256sum: $(cat "$SMI2ASS_BIN.sha256") ($SMI2ASS_BIN.sha256)"

# Build a test site with the binary to make sure it really works.
"build/dist/smi2ass.$OS_CLASSIFIER" 'test_smis/Psycho-Pass - S01E15.smi'
"build/dist/smi2ass.$OS_CLASSIFIER" 'test_smis/Bakemonogatari - 01.smi'
Empty file modified install.sh
100755 → 100644
Empty file.
Empty file modified os_classifier.sh
100755 → 100644
Empty file.
31 changes: 18 additions & 13 deletions smi2ass.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
major = sys.version_info.major
minor = sys.version_info.minor

if major == 3 and minor == 7:
if major == 3 and minor >= 7:
import html
elif major == 3 and minor < 7:
from html.parser import HTMLParser
else:
print ('version 3.x needed')

default_lang_code = 'kor'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you let me know why this had to be removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought if the subtitle file only incused single language in the subtitle it is meaningless to add kor on the file name. Since already know what language is in the subtitle file.

default_font_name = 'sans-serif'
default_font_name = 'Malgun Gothic'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using fonts like "Noto Sans CJK KR" instead, given Malgun Gothic is not available in non-Windows?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of my mistake, while using and testing this program, I modified the ass to match with my subtitle settings.


# lang class for multiple language subtitle
langCode = {'KRCC':'kor','KOCC':'kor','KR':'kor','KO':'kor','KOREANSC':'kor','KRC':'kor',
Expand All @@ -47,10 +46,12 @@
"""[Script Info]
;This is an Advanced Sub Station Alpha v4+ script.
;Converted by smi2ass
Title: 0
ScriptType: v4.00+
Collisions: Normal
PlayResX: 384
PlayResY: 288
PlayDepth: 0
PlayResX: 1920
PlayResY: 1080
Timer: 100.0000

"""
Expand All @@ -59,7 +60,7 @@
"""
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,""" + default_font_name + """,22,&H00ffffff,&H0000ffff,&H00000000,&H80000000,0,0,0,0,100,100,0,0.00,1,1,1,2,20,20,20,1
Style: Default,""" + default_font_name + """,16,&H00FFFFFF,&H0000FFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,1

"""

Expand Down Expand Up @@ -336,18 +337,19 @@ def smi2ass_internal (sln):
if not col == None:
hexcolor = re.search('[0-9a-fA-F]{6}',color['color'].lower()) # bad cases : '23df34'
if hexcolor is not None:
converted_color = '{\\c&H' + hexcolor.group(0)[::-1]+'&}' + color.text + '{\\c}'
converted_color = '{\\c&H' + rgb2bgr(hexcolor.group(0)[::]) +'&}' + color.text + '{\\c}'
else:
try:
converted_color = '{\\c&H' + css3_names_to_hex[color['color'].lower()][::-1].replace('#','&}') + color.text + '{\\c}'
hexConvert = rgb2bgr(css3_names_to_hex[color['color'].lower()][::].replace('#',''))
converted_color = '{\\c&H' + hexConvert + '&}' + color.text + '{\\c}'
except: # bad cases : 'skybule'
converted_color = color.text
print('Failed to convert a color name: %s' % color['color'].lower())
color.replaceWith(converted_color)

contents = p_tags.text
contents = re.sub(r'smi2ass_unicode\(([0-9]+)\)', r'&#\1;', contents)
if minor == 7:
if minor >= 7:
contents = html.unescape(contents)
else:
parser = HTMLParser()
Expand All @@ -356,10 +358,8 @@ def smi2ass_internal (sln):
if len(contents.strip()) != 0:
line = 'Dialogue: 0,%s,%s,Default,,0000,0000,0000,,%s\n' % (tcstart,tcend, contents)
ass_lines.append(line)

return ass_lines


def ms2timecode(ms):
hours = int(ms / 3600000)
ms -= hours * 3600000
Expand All @@ -371,7 +371,6 @@ def ms2timecode(ms):
timecode = '%01d:%02d:%02d.%02d' % (hours, minutes, seconds, ms)
return timecode


def separate_by_lang(smi_lines):
#prepare multilanguage dict with languages separated list
multiLanguageDict = defaultdict(list)
Expand Down Expand Up @@ -482,8 +481,14 @@ def separate_by_lang(smi_lines):
longlang.append('')
return multiLanguageDictSorted, longlang

def rgb2bgr(rgb):
# Converting RGB based color code to BGR color code.
# Based on the ASS documentation, ASS useses BGR
return rgb[4:6] + rgb[2:4] + rgb[0:2]

for smi_path in sys.argv[1:]:
# Print what file is currently working on
print(smi_path)
# Open as binary and detect the encoding.
smi_file = open(smi_path, 'rb')
smi_encoding = chardet.detect(smi_file.read())['encoding']
Expand All @@ -495,7 +500,7 @@ def separate_by_lang(smi_lines):
ass_dict = smi2ass(smi_sgml)
for lang in ass_dict:
if len(lang) == 0:
ass_path = smi_path[:smi_path.rfind('.')] + '.' + default_lang_code + '.ass'
ass_path = smi_path[:smi_path.rfind('.')] + '.ass'
else:
ass_path = smi_path[:smi_path.rfind('.')] + '.' + lang + '.ass'

Expand Down
Loading