Stremio / stremio-addon-sdk

🧙 A Node.js SDK for creating and publishing Stremio add-ons
https://www.stremio.com/addon-sdk
MIT License
656 stars 175 forks source link

Subtitles Encoding issues #265

Closed svetlozardraganov closed 6 months ago

svetlozardraganov commented 6 months ago

Hello,

I'm working on a addon for Cyrillic subtitles and I stuck on encoding issues I don't know how to resolve on my own. Any help would be greatly appreciated.

I found a workaround but it's not ideal and I prefer to resolve the issue rather relying on a workaround. The encoding issue occurs only when working with buffer(memory). When subtitles are downloaded to a file, extracted to a local folder and uploaded to file-sharing service it works as a charm. However when downloading and extracting to buffer/memory it doesn't work.

The interesting part is it doesn't work in Stremio only. If I print the subtitles in NodeJS console it works fine. If I download and open the uploaded subtitles it work fine. If I load the subtitles in VLC it works fine. I tried adding "http://127.0.0.1:11470/subtitles.vtt?from=" as a prefix to the subtitles-URL but unfortunately it doesn't help.

Here is a simplified version of my addon: https://github.com/svetlozardraganov/stremio-hello-world

To switch between buffer and file approach enable/disable the following lines in addons.js

const subtitles_module = require('./lib/subtitles_from_buffer') //<<<<<<<< Load Subs from Buffer
// const subtitles_module = require('./lib/subtitles_from_file')      //<<<<<<<< Load Subs from File

Play any move and the addon will load the following subtitles automatically: https://yavka.net/subs/19089/BG

svetlozardraganov commented 6 months ago

Update: I tried to load the subtitles from a local http server started from NodeJS but again the encoding is not proper. I haven't got much experience on the encoding subject and for me it's not very clear why the same data is:

As mentioned before, the data is displayed properly in other application.

dexter21767-dev commented 6 months ago

i face similar issues before and ended up making my own subtitle converter but didn't have much time to document it. https://github.com/dexter21767/sub2vtt i just tested it with your sub and it handles it fine. u can copy past the implementation from here and add it to your nodejs server. https://github.com/dexter21767/stremio-opensubtitles/blob/5ea834902ee69dc5a58cc8225c368900e73968cc/index.js#L76C1-L103C3

svetlozardraganov commented 6 months ago

Thanks for the details @dexter21767-dev. I'll try to use the provided implementation and will keep you posted for the outcome.

svetlozardraganov commented 6 months ago

Quick update from my side.

I managed to run successfully sub2vtt library and as you confirmed Stremio loaded the subtitles with the right encoding. After looking more carefully I noticed you are using the following header information:

res.setHeader('Content-Type', 'text/vtt;charset=UTF-8');

I decided to test whether adding the charset=UTF-8 to the header of my local http-server alone will help Stremio to load the subtitles with proper encoding and to my surprise it did. No need to convert the subtitles to vtt, Stremio loads them properly after explicitly specifying the encoding in the local-server header.

Not sure if this could be improved in streamio-addon-sdk.

Thanks again @dexter21767-dev, I wouldn't succeed without your invaluable help!

dexter21767-dev commented 6 months ago

@svetlozardraganov great to hear mate, looking forward to see your addon in the community addons list. and hopefully, we will have a better solution for subs in the future. feel free to reach out whenever u need help.