diff --git a/.gitignore b/.gitignore index b6e4761..b6a361a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.DS_Store + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a39b6ae --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +pysubs2==1.0.0 +googletrans==3.1.0a0 +google-trans-new==1.1.9 +chardet==3.0.4 +requests==2.27.1 \ No newline at end of file diff --git a/setup.py b/setup.py index 8dcfa79..d3532b2 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -import setuptools +from setuptools import find_packages, setup with open('README.md', 'r') as fh: long_description = fh.read().strip() @@ -6,19 +6,19 @@ with open('version.txt', 'r') as fh: version = fh.read().strip() -setuptools.setup( +SETUP_ARGS = dict( name='translatesubs', version=version, license='Apache-2.0', author='Montvydas Klumbys', - author_email='motnvydas.klumbys@gmail.com', + author_email='montvydas.klumbys@gmail.com', description='It is a tool to translate subtitles into any language, that is supported by google translator', long_description=long_description, long_description_content_type='text/markdown', url='https://github.com/Montvydas/translatesubs', download_url=f'https://github.com/Montvydas/translatesubs/archive/v_{version}.tar.gz', keywords=['SUBTITLES', 'TRANSLATE'], - packages=setuptools.find_packages(), + packages=find_packages(), install_requires=[ 'pysubs2==1.0.0', 'googletrans==3.1.0a0', @@ -36,7 +36,11 @@ python_requires='>=3.6', entry_points={ 'console_scripts': [ - 'translatesubs=translatesubs.translatesubs:main' + 'translatesubs=translatesubs.main:main' ] }, ) + + +if __name__ == "__main__": + setup(**SETUP_ARGS) diff --git a/translatesubs/translatesubs.py b/translatesubs/main.py similarity index 96% rename from translatesubs/translatesubs.py rename to translatesubs/main.py index 0438b0b..d6f00bf 100644 --- a/translatesubs/translatesubs.py +++ b/translatesubs/main.py @@ -1,19 +1,20 @@ #!/usr/bin/env python +from translatesubs.managers.language_manager import LanguageManager +from translatesubs.managers.subs_manager import SubsManager +from translatesubs.utils.constants import AVAILABLE_TRANSLATORS, TRANSLATORS_PRINT, DEFAULT_SEPS_PRINT, USE_DEFAULT_SEPS, \ + DEFAULT_SEPS, SEP_MAX_LENGTH, SUB_FORMATS + import argparse import logging import sys from typing import List import os -from .language_manager import LanguageManager -from .subs_manager import SubsManager -from .constants import AVAILABLE_TRANSLATORS, TRANSLATORS_PRINT, DEFAULT_SEPS_PRINT, USE_DEFAULT_SEPS, DEFAULT_SEPS, \ - SEP_MAX_LENGTH, SUB_FORMATS - """ Future Development: 1) Automatically detect the subs language using "from_lang" argument, which would be more automatic than subs_track. +2) Add official google translate api and ability to setup your google api key. """ diff --git a/translatesubs/translator/__init__.py b/translatesubs/managers/__init__.py similarity index 100% rename from translatesubs/translator/__init__.py rename to translatesubs/managers/__init__.py diff --git a/translatesubs/language_manager.py b/translatesubs/managers/language_manager.py similarity index 97% rename from translatesubs/language_manager.py rename to translatesubs/managers/language_manager.py index e59c839..1ce7029 100644 --- a/translatesubs/language_manager.py +++ b/translatesubs/managers/language_manager.py @@ -3,9 +3,9 @@ import re import logging from typing import List, Tuple, Iterator -from translatesubs.translator.itranslator import ITranslator -from translatesubs.translator.language import Language -from translatesubs.constants import ENDS_OF_SENTENCES, DEFAULT_SEPS, SEP_MAX_LENGTH +from translatesubs.translators.itranslator import ITranslator +from translatesubs.translators.language import Language +from translatesubs.utils.constants import ENDS_OF_SENTENCES, DEFAULT_SEPS, SEP_MAX_LENGTH class LanguageManager: diff --git a/translatesubs/subs_manager.py b/translatesubs/managers/subs_manager.py similarity index 83% rename from translatesubs/subs_manager.py rename to translatesubs/managers/subs_manager.py index efe1572..e682aa0 100644 --- a/translatesubs/subs_manager.py +++ b/translatesubs/managers/subs_manager.py @@ -52,21 +52,20 @@ def update_subs(self, main_subs: List[str], secondary_subs: List[str], merge: bo # original --> secondary # translated --> main for main, secondary, sub, origin_sub in zip(main_subs, secondary_subs, self.subs, self.origin_subs): - main = Sub.merge_multiline(main, int(char_limit)) - secondary = Sub.merge_multiline(secondary, int(char_limit * 100 / secondary_scale)) - # 1. For now ignore the in-line based styling e.g. bold single word. # 2. Replace \n with \N as otherwise the same sub will be treated as separate event aka next sub. - # NOTE: When writing into plaintext, \n is replaced with \N. But we also want to add custom styling.. + # NOTE: When writing into plaintext, \n is replaced with \N. But we also want to add custom styling.. + main = Sub.merge_multiline(main, int(char_limit)) main = SubsManager._replace_with_capital_newline(main) - secondary = SubsManager._replace_with_capital_newline(secondary) - - secondary = SubsManager._style_secondary(secondary, merge, secondary_scale) - - origin_sub.text = f'{sub.open_style}{main}{sub.close_style}' - + if merge: - origin_sub.text = f'{sub.open_style}{main}{secondary}{sub.close_style}' + secondary = Sub.merge_multiline(secondary, int(char_limit * 100 / secondary_scale)) + secondary = SubsManager._replace_with_capital_newline(secondary) + secondary = SubsManager._style_secondary(secondary, secondary_scale) + else: + secondary = "" + + origin_sub.text = f'{sub.open_style}{main}{secondary}{sub.close_style}' def save_subs(self, subs_out: str): self.origin_subs.save(subs_out) @@ -79,11 +78,11 @@ def extract_from_video(video_in: str, subs_track: int, subs_out: str) -> bool: return status.returncode == 0 @staticmethod - def _style_secondary(text: str, merge: bool, scale: int) -> str: + def _style_secondary(text: str, scale: int) -> str: # Make text smaller than original and add 50% transparency - note it's HEX, not decimal. open_style = f'\\N{{\\fscx{scale}\\fscy{scale}\\alpha&H75&}}' close_style = '\\N{\\fscx100\\fscy100\\alpha&H00&}' - return f'{open_style if merge else ""}{text}{close_style if merge else ""}' + return f'{open_style}{text}{close_style}' @staticmethod def _replace_with_capital_newline(multiline): diff --git a/translatesubs/translators/__init__.py b/translatesubs/translators/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/translatesubs/translator/google_trans_new.py b/translatesubs/translators/google_trans_new.py similarity index 94% rename from translatesubs/translator/google_trans_new.py rename to translatesubs/translators/google_trans_new.py index 74f664e..bb4e0f2 100644 --- a/translatesubs/translator/google_trans_new.py +++ b/translatesubs/translators/google_trans_new.py @@ -3,9 +3,9 @@ import logging import re -from translatesubs.translator.itranslator import ITranslator -from translatesubs.translator.language import Language -from translatesubs.translator.translated import Translated +from translatesubs.translators.itranslator import ITranslator +from translatesubs.translators.language import Language +from translatesubs.translators.translated import Translated class GoogleTransNew(ITranslator): diff --git a/translatesubs/translator/googletrans.py b/translatesubs/translators/googletrans.py similarity index 96% rename from translatesubs/translator/googletrans.py rename to translatesubs/translators/googletrans.py index a7d405d..e9770ac 100644 --- a/translatesubs/translator/googletrans.py +++ b/translatesubs/translators/googletrans.py @@ -2,10 +2,10 @@ from typing import List, Iterator import logging import re -from translatesubs.translator.itranslator import ITranslator -from translatesubs.translator.language import Language -from translatesubs.translator.translated import Translated -from translatesubs.tools import nth +from translatesubs.translators.itranslator import ITranslator +from translatesubs.translators.language import Language +from translatesubs.translators.translated import Translated +from translatesubs.utils.tools import nth """ The API has problems with pronunciations: diff --git a/translatesubs/translator/itranslator.py b/translatesubs/translators/itranslator.py similarity index 79% rename from translatesubs/translator/itranslator.py rename to translatesubs/translators/itranslator.py index 663bd36..4c10783 100644 --- a/translatesubs/translator/itranslator.py +++ b/translatesubs/translators/itranslator.py @@ -1,6 +1,6 @@ from typing import List, Iterator -from translatesubs.translator.translated import Translated -from translatesubs.translator.language import Language +from translatesubs.translators.translated import Translated +from translatesubs.translators.language import Language from abc import ABC, abstractmethod diff --git a/translatesubs/translator/language.py b/translatesubs/translators/language.py similarity index 100% rename from translatesubs/translator/language.py rename to translatesubs/translators/language.py diff --git a/translatesubs/translator/translated.py b/translatesubs/translators/translated.py similarity index 100% rename from translatesubs/translator/translated.py rename to translatesubs/translators/translated.py diff --git a/translatesubs/utils/__init__.py b/translatesubs/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/translatesubs/constants.py b/translatesubs/utils/constants.py similarity index 83% rename from translatesubs/constants.py rename to translatesubs/utils/constants.py index 72badbc..bd0a389 100644 --- a/translatesubs/constants.py +++ b/translatesubs/utils/constants.py @@ -1,5 +1,5 @@ -from translatesubs.translator.googletrans import GoogleTrans -from translatesubs.translator.google_trans_new import GoogleTransNew +from translatesubs.translators.googletrans import GoogleTrans +from translatesubs.translators.google_trans_new import GoogleTransNew AVAILABLE_TRANSLATORS = {'googletrans': GoogleTrans, # Does not keep newlines for pronunciation only diff --git a/translatesubs/tools.py b/translatesubs/utils/tools.py similarity index 100% rename from translatesubs/tools.py rename to translatesubs/utils/tools.py diff --git a/truncated.ass b/truncated.ass index e8de364..af0ff44 100644 --- a/truncated.ass +++ b/truncated.ass @@ -103,8 +103,6 @@ Style: sign_29078_298_Boys__High_Schoo,Trebuchet MS,35,&H00111113,&H000000FF,&H0 [Events] Format: Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text -Dialogue: 0,0:00:04.94,0:00:11.90,sign_119_1_Court_,Sign,0000,0000,0000,,Court A, Current Match: Karasuno (Miyagi)\NCourt A, Current Match: Inarizaki (Hyogo)\NCourt B, Current Match: Sentoku (Hiroshima)\NCourt B, Current Match: Iseoka (Aichi)\NCourt C, Current Match: Yamato Girls' (Ehime)\NCourt C, Current Match: Rato (Chiba)\NCourt D, Current Match: Nekoma (Tokyo)\NCourt D, Current Match: Sarukawa Tech (Ishikawa) -Dialogue: 0,0:00:13.86,0:00:15.99,volley-main,Mika,0000,0000,0000,,Is this the match you wanted to see, Suguru? Dialogue: 0,0:00:17.26,0:00:18.28,volley-main,Guy,0000,0000,0000,,Yeah. Dialogue: 0,0:00:18.91,0:00:22.08,Connect,Sign,0000,0000,0000,,Connect Dialogue: 0,0:00:19.49,0:00:20.96,volley-main,Mika,0000,0000,0000,,Nekoma? diff --git a/version.txt b/version.txt index f477849..373f8c6 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.2.2 \ No newline at end of file +0.2.3 \ No newline at end of file