Muhammad Tuseeq
Muhammad Tuseeq

Reputation: 69

Detecting the encoding of srt file and changing it

I'm trying to read a srt file that is in hebrew. The encoding is supposed to be cp1255 but it is not reading with this one. I can read it with utf-8 but then it do not follow the rules of hebrew language. I want to store the file in cp1255 format after reading it using 'pysubs2' library in python. Is there any way to do this?

Upvotes: 0

Views: 3195

Answers (1)

iPzard
iPzard

Reputation: 2368

Old question, but figured I'd post in case anyone else is trying to do this. I've done something similar like this below.

import chardet

# Sniff out encoding method
with open(subtitle_input_path, 'rb') as f:
  rawdata = b''.join([f.readline() for _ in range(10)])

# Encoding method and method whitelist
encoding_method = chardet.detect(rawdata)['encoding']
encoding_method_whitelist = ['utf8', 'ascii']

# If encoding method will cause issues, convert it to utf-8
if encoding_method not in encoding_method_whitelist:

  # Read the old file's content
  with open(subtitle_input_path, encoding=encoding_method) as subtitle_file:
    subtitle_text = subtitle_file.read()

  # Convert to utf-8 and write to file
  with open(subtitle_input_path,'w', encoding='utf8') as subtitle_file:
    subtitle_file.write(subtitle_text)

Upvotes: 1

Related Questions