McCloudS / subgen

Autogenerate subtitles using OpenAI Whisper Model via Jellyfin, Plex, Emby, Tautulli, or Bazarr
MIT License
532 stars 48 forks source link

Enhance post-webhook metadata handling #33

Closed Rikiar73574 closed 8 months ago

Rikiar73574 commented 8 months ago

tried adding a simple function to update the metadata on titles once the subtitles are generated.

  1. Enhanced Post-subtitle Generation Workflow: After subtitles are generated (when the gen_subtitles function is called), there is now an additional step to refresh the metadata of the corresponding Plex library item. This ensures that any updates related to subtitles are immediately reflected in the Plex media library.

  2. Error Handling for Metadata Refresh: A try-except block has been added to catch and log any exceptions that occur during the metadata refresh process. This robust error handling is critical because it provides clear feedback in the event of a failure, ensuring that the user is informed about issues with refreshing metadata. The log message specifies the item_id affected and includes the error message for easier troubleshooting.

  3. New Function refresh_plex_metadata: A dedicated function has been added to encapsulate the logic for refreshing metadata. Its purpose is to send a PUT request to the Plex API for a specific item in order to trigger a metadata refresh. The function includes error handling to raise an exception if the refresh is not initiated successfully, which would be caught by the aforementioned error handling in the webhook processing block.

  4. Handling Misconfigured Webhooks: The message for misconfigured Plex webhooks has been shifted. This message now appears in the else block of the try-except statement, which informs the user if the webhook configuration isn't proper. While this might seem like a minor change, it ensures that the message is clearly associated with webhook configuration errors and not with other operations such as subtitle generation or metadata refreshing.

You can ignore this one if you feel like it changes too much of the original functionality but the other one about the dockerfile im pretty confident was an issue. feel free to hit me up on discord "rikiar" if you want to discuss this i really like having this as a backup for my subtitles, so i'd gladly for a bit on it.

McCloudS commented 8 months ago

Neat idea. I've never run into the problem though. I have "Update my library automatically" (See: https://support.plex.tv/articles/200289306-scanning-vs-refreshing-a-library/) enabled in Plex. Any directory file changes on my system seem to get updated near immediately.

item_id isn't defined anywhere. Did you mean to use plex_json['Metadata']['ratingKey']?. Additionally, the metadata refresh would likely finish before the subtitle generation was complete. Stable-ts/whisper appears to use its own threading system, so subgen.py isn't necessarily executing serially.

You would potentially have to drop it at https://github.com/McCloudS/subgen/blob/113e0a203c41c2603626a63447b278cfbc492442/subgen/subgen.py#L303C18-L303C18 but gen_subtitles doesn't track which webhook it came from.

Rikiar73574 commented 8 months ago

sorry about the item_id, my internet sucks atm so i didn´t really test this. as per the automatic update i personally don't trust those for more complicated setups, so i prefer to force it just in case, had no idea about the separate threading, i'll look into it then should be easy enough to pass it where you mentioned.

Rikiar73574 commented 8 months ago

it appears to work, i can start a movie (with proconmediaplay=true) and without refreshing or closing the movie i can just wait a few minutes and get the subtitles automatically in the list, so i just have to check the subtitles menu every once in a while. i'm not sure if this also happens with "Update my library automatically" where you don´t even have to close the movie to have new subs added.

and the separate threading you mentioned doesn't seem to be an issue

Transcribing with faster-whisper (medium)...
Saved: /movies/The Ballad of Buster Scruggs (2018)/The.Ballad.Of.Buster.Scruggs.2018.1080p.NF.WEB-DL.DDP5.1.X264-KamiKaze.subgen.medium.aa.srt
Transcription of The.Ballad.Of.Buster.Scruggs.2018.1080p.NF.WEB-DL.DDP5.1.X264-KamiKaze.mkv is completed, it took 6 minutes and 0 seconds to complete.
Metadata refresh initiated successfully.
INFO:root:Metadata for item 4243 refreshed successfully.

feel free to let me know if you don't like something about it.

McCloudS commented 8 months ago

Looks good, thanks for the effort! I also modified it to add the refresh for Jellyfin. (Untested, as I don't have my Jellyfin running right now, but the end point is right per: https://[api.jellyfin.org/openapi/jellyfin-openapi-stable.json](https://api.jellyfin.org/openapi/jellyfin-openapi-stable.json))