BingLingGroup / autosub

Command-line utility to transcribe/translate from video/audio/subtitles to subtitles
GNU General Public License v2.0
1.98k stars 244 forks source link

Encoding problem when trying to translate srt file from EN -> CN;英文字幕文件翻译中文时编码问题 #151

Closed Kaiyiccc closed 3 years ago

Kaiyiccc commented 3 years ago

Describe the bug

使用音频直接生成英文字幕正常。试着翻译生成的英文 srt 文件,出现以下报错: When trying to translate an srt file, I get the following error:

AttributeError: 'NoneType' object has no attribute 'encode'

To Reproduce Steps to reproduce the behavior:

Ky-iMac:autosub home$ source test1/bin/activate
(test1) Ky-iMac:autosub home$ autosub -i /Users/home/autosub/test1/bin/output.srt -D zh-CN
No output format specified. Use input format "srt" for output.
Input is a subtitles file.
Traceback (most recent call last):
  File "/Users/home/autosub/test1/bin/autosub", line 33, in <module>
    sys.exit(load_entry_point('autosub==0.5.7a0', 'console_scripts', 'autosub')())
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/autosub/__init__.py", line 170, in main
    cmdline_utils.sub_trans(args,
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/autosub/cmdline_utils.py", line 796, in sub_trans
    translated_text, args.src_language = core.list_to_googletrans(
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/autosub/core.py", line 578, in list_to_googletrans
    translator = googletrans.Translator(
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/googletrans/client.py", line 61, in __init__
    self.client.headers.update({
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/httpx/_models.py", line 480, in update
    headers = Headers(headers)
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/httpx/_models.py", line 393, in __init__
    self._list = [
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/httpx/_models.py", line 394, in <listcomp>
    (normalize_header_key(k, encoding), normalize_header_value(v, encoding))
  File "/Users/home/autosub/test1/lib/python3.8/site-packages/httpx/_utils.py", line 53, in normalize_header_value
    return value.encode(encoding or "ascii")
AttributeError: 'NoneType' object has no attribute 'encode'

Expected behavior

我也试着更改环境默认的 LANG 和 LC_ALL 至 en_US.UTF-8。但这似乎不是问题所在 I also tried setting the LANG and LC_ALL to en_US.UTF-8.

Environment (please complete the following information):

Additional context 坐标北美,应该没有网络问题 I am located in the US so internet access shouldn't be a problem.

PumpkinCheshire commented 3 years ago

Duplicate of #127

This problem has been resolved, git pull to update to 'dev' version.

Or you can try patch to autosub/core.py as a temporary quick resolution:

--- /Users/home/autosub/test1/lib/python3.8/site-packages/autosub/core.py   2020-11-05 16:51:38.116478895 +1100
+++ "/Users/home/autosub/test1/lib/python3.8/site-packages/autosub/core.py" 2020-11-05 16:52:30.469815115 +1100
@@ -576,7 +576,7 @@
         return None

     translator = googletrans.Translator(
-        user_agent=user_agent,
+        user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
         service_urls=service_urls)

     size = 0