Skip to content

Commit

Permalink
Some restructuring of the file system and cleanup. Prepared for v_0.2…
Browse files Browse the repository at this point in the history
….3 release.
  • Loading branch information
Montvydas committed Feb 24, 2023
1 parent 592ece1 commit 7a2562d
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
14 changes: 9 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import setuptools
from setuptools import find_packages, setup

with open('README.md', 'r') as fh:
long_description = fh.read().strip()

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[email protected]',
author_email='montvydas[email protected]',
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',
Expand All @@ -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)
11 changes: 6 additions & 5 deletions translatesubs/translatesubs.py → translatesubs/main.py
Original file line number Diff line number Diff line change
@@ -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.
"""


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down
File renamed without changes.
File renamed without changes.
Empty file added translatesubs/utils/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions truncated.ass
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.2
0.2.3

0 comments on commit 7a2562d

Please sign in to comment.