UnicodeDecodeError: 'cp950' in moviepy SubtitlesClip(srt, generator)

134 Views Asked by At

how could i fix the problem about srt file process

my project is a image convert to video include srt text then merge all in one new video but stuck the srt stage now

when srt content include traditional chinese text or char will show error (even i use traditional chinese font file) but if all english text content will no error

my python code

import os
import glob
from moviepy.editor import CompositeAudioClip, VideoFileClip, AudioFileClip
from moviepy.video.tools.subtitles import SubtitlesClip
from PyQt6.QtGui import QColor
from moviepy.editor import *


srt = ("./test.srt")
generator = lambda txt: TextClip(txt, font='./TaipeiSansTCBeta-Bold.ttf',fontsize=20, color='white')
sub = SubtitlesClip(srt, generator).set_position((250,250))

my srt content

1
00:00:00,000 --> 00:00:03,000
繁體中文(this is traditional chinese text)

2
00:00:04,000 --> 00:00:07,000
bbbbbbbbbbbb

3
00:00:08,000 --> 00:00:11,000
ccccccccccc

4
00:00:12,000 --> 00:00:15,000
ddddddddddddddd

my error message

C:\Users\uting\PycharmProjects\imgs_to_video_v1\venv\Scripts\python.exe C:/Users/uting/PycharmProjects/imgs_to_video_v1/test/test01.py
Traceback (most recent call last):
  File "C:\Users\uting\PycharmProjects\imgs_to_video_v1\test\test01.py", line 11, in <module>
    sub = SubtitlesClip(srt, generator).set_position((250,250))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\uting\PycharmProjects\imgs_to_video_v1\venv\Lib\site-packages\moviepy\video\tools\subtitles.py", line 42, in __init__
    subtitles = file_to_subtitles(subtitles)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\uting\PycharmProjects\imgs_to_video_v1\venv\Lib\site-packages\moviepy\video\tools\subtitles.py", line 154, in file_to_subtitles
    for line in f:
UnicodeDecodeError: 'cp950' codec can't decode byte 0x81 in position 36: illegal multibyte sequence

Process finished with exit code 1
2

There are 2 best solutions below

1
hukuto7750 On BEST ANSWER

Finally I find the key problem

not code

Need to install imagemagick

And the srt file need to save as ansi Into Coding structure

compile will successful

0
Boaz Yakubov On

The likely encoding is UTF-8 (it's the standard encoding for Python 3 source code).

If that's the case, changing open("somefile.srt") to open("somefile.srt", encoding="utf-8") would specify the encoding explicitly, overriding the locale default, which should allow you to read it in correctly.

For idiomatic code, you'd also want to use a with statement (to guarantee deterministic closing of the file), making it:

import os
from moviepy.editor import SubtitlesClip, TextClip

srt = "./test.srt"

# create textclip with the font
def generator(txt):
    return TextClip(txt, font='./TaipeiSansTCBeta-Bold.ttf', fontsize=20, color='white')

# Read the SRT file with utf8 encoding.
with open(srt, 'r', encoding='utf-8') as f:
    subtitles = f.read()

# Create a SubtitlesClip
sub = SubtitlesClip(subtitles, generator).set_position((250, 250))

perhaps this question might be of use to you MoviePy not displaying non-English characters