简体   繁体   中英

How to pad Chinese/Japanese characters so that they are alligned with normal characters, have the same width in .srt file (python)

I wanted to create 3 strings in seperate rows that would be displayed in this way: In terminal

That is, aligned horizontally. The result strings saved in .srt file look like that: In web video player

I already tried replacing spaces with various whitespaces, without success.

The bit of code that I use for padding:

def pad(strings):#"strings" contain 3 strings seen as one column seperated by "|" in terminal
lengths=[2*len(strings[0]),len(strings[1]),len(strings[2])]#japanese/chinese chars are multiplied by 2
max_len=max(lengths)                                       #because in terminal they take up two spaces
for i in range(len(lengths)):
    if lengths[i] < max_len:
        diff_len = max_len-lengths[i]
        append_spaces=(int)(diff_len/2)
        #below I add spaces so that they are alligned, this works in terminal, 
        #not in video player with loaded srt
        strings[i] = " "*append_spaces + strings[i] + " "*append_spaces + " "*(diff_len%2)
return strings

It's important for me to use .srt files, without any styling. That is the only way it'll be recognized properly in most web media players. If there is no way around it, how could I achieve the result seen in terminal with any other subtitle format?

For anyone that stumbled upon the same problem, solution is to use monospaced font, where (at least in my case) every kanji and kana symbol has twice the width of normal character. The only one that worked for me is "unifont_jp-14.0.01.ttf" from here . Having that font installed I'm able to use it in vlc wih .srt file, or use subtitle editor to create subtitles with that font embedded and in more advanced file format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM