jellyfin-kodi icon indicating copy to clipboard operation
jellyfin-kodi copied to clipboard

Subtitles don't save on Kodi Jellyfin

Open Jon12345 opened this issue 2 years ago • 15 comments

Describe the bug

Please see: https://www.reddit.com/r/jellyfin/comments/rvzy9e/subtitles_dont_save_on_kodi_jellyfin/

To Reproduce

  1. Download subtitles for a video using Kodi
  2. Play the video and confirm subtitles are working
  3. Exit Kodi
  4. Restart Kodi and play the same video
  5. Observe that subtitles no longer display, and need to be downloaded again

Expected behavior Subtitles should display after restarting Kodi and playing a video that had local subtitles downloaded

System (please complete the following information):

  • OS: Kodi for Android TV, Jellyfin on Windows
  • Jellyfin Version: 10.7.5
  • Kodi Version: 19.3
  • Addon Version: 0.7.7
  • Playback Mode: Add-On

Jon12345 avatar Jun 19 '22 14:06 Jon12345

This is more or less by design, and not a simple problem to solve. When you download subtitles through a third party addon (such as opensubtitles), it downloads them to a generic location on the disk and names it Stream.language.srt, setting it to active on the current playing file. And that's where the problem lies, because how are we to know if that generically named file belongs to video A or video B?

As mentioned in the linked reddit thread, it should be possible to find when a subtitle has been downloaded and upload that to the server so it can be picked up the next time a video is watched, but I haven't tested what all that will require yet.

mcarlton00 avatar Jun 19 '22 18:06 mcarlton00

This works when using Kodi without Jellyfin, though. How does Kodi know which subtitle file is for which video?

Also - what's the recommended way to retrieve subtitles? I assume it's by using the Jellyfin subtitles plugin, but I've had many cases where it didn't retrieve any subtitles, or it retrieved the wrong file. This is where using Kodi to fix it helps.

Jon12345 avatar Jun 19 '22 23:06 Jon12345

To be blunt, I don't have a damn clue. The code in the opensubtitles addon that seems to be doing it doesn't make any sense. https://github.com/juokelis/service.subtitles.opensubtitles/blob/Matrix/service.py#L185-L186. This is basically saying "Add this subtitle file to the currently displayed menu item on screen". It's not persistent, so as far as I'm aware it shouldn't work across multiple plays, or even multiple menu opens for that matter. Possibly some internal Kodi function that doesn't work because of all the database hackery we're forced to do to make the addon work, but I'm not aware of what it could be. I don't see any flags in the database, but I could easily be missing it if that's how it works.

But the talk of opensubtitles introduces another problem, because not all subtitle addons download to the same location or use the same naming scheme. What I referenced above (Stream.language.srt in a generic location) was a different addon. The opensubtitles addon downloads sub files to $KODI_HOME/userdata/addon_data/service.subtitles.opensubtitles/$filehash.srt. Which means that in order to parse that, we have to go digging through other addons' private data storage, which is a gross breach of trust and highly discouraged by the kodi devs. Plus needing to know what subtitle addons are installed, and how each of them work and store their files. So it becomes basically impossible for us to effectively parse what's out there. We could in theory do a search of the kodi directory for all subtitle files. That sounds terrible, since nothing is effectively named and we'd have no idea if it was for the present file or left over from a previous play.

The only remotely viable method of doing this would be to automatically upload a copy of the downloaded sub file to the Jellyfin server immediately when another addon brought it in. The first problem would be figuring out how to possibly catch that event, but this again brings us going into other addon's data folders. On top of that, it probably requires admin access on the server to be allowed to upload at all, not to mention likely needing to run a media scan after it's been uploaded so it gets picked up, and possibly write permission to your media directories (I'm not sure where subs are stored in the server). There's not really anything about this that's going to be straightforward or clean, so at this moment I'm not sure how it could be implemented.

mcarlton00 avatar Jun 22 '22 00:06 mcarlton00

I would strongly recommend adding the subtitles to Jellyfin directly, either manually, next to the movie file, or via one of the Jellyfin subtitle plugins.

I'd say this is completely out of scope for the jf4kodi addon.

oddstr13 avatar Jun 22 '22 22:06 oddstr13

There's an endpoint in the server specifically for uploading subtitles, so it's not entirely outside the realm of possibility. There's definitely going to be challenges before we even reach out to the server though.

mcarlton00 avatar Jun 22 '22 23:06 mcarlton00

If you want to, by all means… I think it belongs as a server plugin or an external program. I do see the potential UX benefit, but I get a icky feeling when thinking about the backend code spaghetti

oddstr13 avatar Jun 22 '22 23:06 oddstr13

Hopefully the solution can be decoupled from OpenSubtitles and other subtitle addons. Subtitles can be added to Kodi without those addons, eg by browsing the filesystem, so there must be a mapping somewhere between video and subtitle filepath. Something about jellyfin-kodi is breaking that mapping.

I'd think uploading subtitles to the Jellyfin server would be out of scope (but a very nice enhancement).

Jon12345 avatar Jun 23 '22 07:06 Jon12345

Just adding my finding regarding this issue.

I am currently using Jellyfin addon on Kodi in native mode. In this mode everything working correctly.

Kodi download new subtitles using the subtitle engine (opensubtitles is just a provider or other subtitle provider) and store the file next to the video (if possible) or to a custom folder if defined in Kodi setup. The downloaded subtitle file is saved with correct filename (since we use native mode , Kodi knows the filename + path) and Kodi is able to use the stored subtitle file on next playback.

When trying to to use JellyCon or Jellyfin addon on "non native" mode in Kodi :

The download subtitle feature of kodi doesn't know the correct filename after downloading new subtitle, like described here , proper subtitles are showing as long as playback continues, but the subtitles are saved as stream.en.srt or something like that, so, next time kodi re-play the file it cant pick the correct stored subtitle file.

As mentioned , The workaround is to use Jellyfin to download subtitles and have them ready for Kodi or any other playback app/device (stop using Kodi's subtitle download feature).

If we want some solution for this scenario and to be able to use kodi subtitle download feature we need to find a solution to either one of the issues.

1.Proper (original) filename get passed to Kodi This way Kodi will download and store the subtitle with correct filename on custom folder , Kodi should be able to pick it up again on next playback. Optionally , Users can use a home made script to move subtitles file to video file directories (if they wish to have them available directly on Jellyfin for next times or just to be more organized)

2.Make kodi use Jellyfin API to upload file after downloading and store in Jellyfin.

I guess Jellyfin already has an API that will allow something like this , but i am quite sure Kodi does not have this option and it will be needed to be "baked" into kodi's subtitle download feature.

Option 1 should be much easier and does not require anything to be added/changed in kodi (other than setting a custom subtitle folder, which can be an shared folder between several devices)

hagaygo avatar Nov 10 '23 11:11 hagaygo

If there is an API endpoint available in the Jellyfin backend, it should not be hard to build something for this inside the Kodi plugin.

But it would make more sense knowing Kodi.... To create an additional plugin that does the subtitle part EG asks Jellyfin to search open subtitles etc., then expose results, so the user can choose. Maybe sort by best match. And then when the user selects, Jellyfin will pull the subtitle and stream it with the video.

That way, all works as intended.

I have been reading up on the API https://api.jellyfin.org/#tag/Subtitle

it looks like there is a subtitle endpoint that allows for search of remote subtitles, there is a function to download remote subtitle. and there is one to get the remote subtitle.

These function should be all that is needed to make something work....

@mcarlton00, @oddstr13 Do you guys know if a single Kodi plugin could hook into both the video parts and add to the Kodi subtitle system? But then instead of downloading to Kodi directly instruct Jellyfin to go and fetch the sub?

Or less simple but maybe a workaround. Search subs via Jelly endpoint, Let Jellyfin fetch the sub, then get the sub and run it locally during that session while Jellyfin keeps the sub for the next play ?

sanderkooger avatar Jan 03 '24 19:01 sanderkooger

1.Proper (original) filename get passed to Kodi This way Kodi will download and store the subtitle with correct filename on custom folder , Kodi should be able to pick it up again on next playback. Optionally , Users can use a home made script to move subtitles file to video file directories (if they wish to have them available directly on Jellyfin for next times or just to be more organized)

Regarding this , I found that when generating/copying a link from jellyfin's web gui (Copy Stream URL option) and playing it using VLC , for few miliseconds , vlc tilte shows the link as "filename" but then shows the proper/correct filename on the window title of vlc window.

So i guess it is possible to pass the correct filename to KODI.

hagaygo avatar Jan 04 '24 06:01 hagaygo

IMO, the only semi reasonable way we could do this is to download subtitles through jellyfin, not using Kodi's download functionality directly. For all the reasons I pointed out above, intercepting subtitle downloads from other addons is a nightmare of spaghetti code and involves us going into places we don't wanna go. The downside of this is that it effectively limits us to just opensubtitles, as we obviously don't have near as many subtitle provider plugins as kodi does.

Rapid fire responses to some of the other things brought up:

and store the file next to the video (if possible) or to a custom folder if defined in Kodi setup.

The problem here is that it involves manual setup of something completely unrelated to the addon, that involves switching the settings level to "advanced" to be able to see it. My opinion is that if we're going to do this, we're going to do it right, not some halfway implemented thing. Also, only really works with native mode, which is basically the red headed step child of this client.

original filenames

I don't think this is as important as it seems here. If we're downloading subtitles and want them available for reuse, they should be in the server. Otherwise again, I think it's just a halfway implemented thing. Additionally, I've had automated subtitle downloads/search working for years without issue and it only recently has changed to be garbage. I suspect this was an upstream Kodi change, but I haven't had enough time in the day to do much dev work lately to confirm that.

We could theoretically fool it into doing something like jellyfin.en.srt so that we know which file we're supposed to upload to the server. It would certainly be easier to parse than whatever generically named thing the subtitle providers hand out. However, this does bring up another point: assuming we do downloads directly and upload to the jellyfin server, when do we choose to upload the subtitle? Personally, it's not uncommon for me to have to download 2-3 different versions before I find one that works as expected and has correct timing. Uploading different subs one after another sounds like a pain to sort out.

Do you guys know if a single Kodi plugin could hook into both the video parts and add to the Kodi subtitle system? But then instead of downloading to Kodi directly instruct Jellyfin to go and fetch the sub?

Most likely yes. The only thing I'm not sure about is timing. ie: We do the search. We download a subtitle from the provider to jellyfin server. Is that sub available immediately, or does it need a library scan to work? Can we download it from the server to the client immediately or is there some processing delay? It will need some testing to see how things fall. And like I mentioned before, I'm not sure what the permissions within the server are for downloading/uploading subtitles, so that could be a factor.

Copy Stream URL

Different api endpoints. This one specifically is for downloading, so it makes sense it would have a filename associated with the source file. The URL we use for playback is a universal endpoint that can be used for direct play or transcoding, so it has a consistent filename associated to it.

TLDR: basically nothing about this is easy or straightforward

mcarlton00 avatar Jan 04 '24 13:01 mcarlton00

@mcarlton00

Thanks for your thorough response.

As i mentioned on option 1, if the addon would pass the correct filename to KODI player on non-native playback advanced users would be able to use KODI's subtitle downloader.

This by itself is reasonable behavior for the addon (passing the correct filename to KODI) regardless it would only help advanced users.

hagaygo avatar Jan 04 '24 14:01 hagaygo

@mcarlton00 I'm having some issues on the Kodi API part for subtitles. What I was thinking is expanding the Kodi subtitle menu. But use Jellyfin to do the subtitling. If you know what I mean.

Workflow would look a bit like:

  1. User presses download subtitle button in the subtitle menu in Kodi player.
  2. Kodi opens up the download subtitle dialog. This is where we hijack and send API req to Jellyfin (https://api.jellyfin.org/#tag/Subtitle/operation/SearchRemoteSubtitles) and return results (pref with perfect matches on top).
  3. When a subtitle is selected by the user, Jellyfin grabs the subtitle And does the Jellyfin workflow.

At this step, the following should happen from a user's perspective. The subtitle should automagically start working. But, From this moment, I am also a bit at a loss. Because I don't know how Kodi internally functions, but there are two options.

  1. If possible, let Jellyfin immediately serve the subtitle the normal Jellyfin way.
  2. Use the https://api.jellyfin.org/#tag/Subtitle/operation/GetSubtitleWithTicks endpoint to get the SRT file and let Kodi stream that for the session. Repeat sessions would use the info that now has been stored in Jellyfin.

I glanced over https://github.com/opensubtitles/service.subtitles.opensubtitles_by_opensubtitles/blob/master/service.py and the way they hook into the subtitle menu. I think we could do the same to make this work.

Does that sound right? I think this would very much uncomplicate things, as we let Jellyfin do all the heavy lifting, and only use the data inside Kodi. But I could be wrong.

sanderkooger avatar Jan 04 '24 14:01 sanderkooger

May I also suggest a server side solution (could be a Jellyfin plugin)

  • Set up Kodi to download subtitle in a share network folder (WebDAV, SMB anything...) named for instance "your_share/kodi_subtitles"
  • On server side, you have a python script watching this folder (using pyinotify), everytime a new subtitle file gets added - and this is where it gets kind of gross - it checks which media file is currently playing with Jellyfin API 😜, so it copies this subtitle file to the corresponding media folder next to the video .... ok this is gross because it can't distinguish between multiples playing sessions which one has added this subtitle

philamp avatar Mar 31 '24 11:03 philamp

May I also suggest a server side solution (could be a Jellyfin plugin)

* Set up Kodi to download subtitle in a share network folder (WebDAV, SMB anything...) named  for instance "your_share/kodi_subtitles"

* On server side, you have a python script watching this folder (using pyinotify), everytime a new subtitle file gets added - and > this is where it gets kind of gross - it checks which media file is currently playing with Jellyfin API 😜, so it copies this subtitle file > to the corresponding media folder next to the video .... ok this is gross because it can't distinguish between multiples playing 

sessions which one has added this subtitle

I made a little app that does something similar.

When executed , it checks for stream*.srt files on kodi's download subtitle folder (shared folder) and check jellyfin play log and if finds a match it uploads the subtitle into jellyfin via API.

While it works just fine most of the time (as you can imagine , the timing and log matching is very fragile),

I think the first step should be that kodi will store the correct filename and not stream.en.srt or something like that. in omega rc2 they even accidentally removed the the subtitle storage on kodi's custom subtitle folder (https://github.com/xbmc/xbmc/pull/24752) When a correct filename will be stored, A plugin in jellyfin might be able to pick up the files or a script would just copy the file to the folder where the video file sits.

hagaygo avatar Mar 31 '24 12:03 hagaygo