Jump to content

[Gdrive Rclone VFS] Precache upcoming episodes from Continue Watching


Recommended Posts

I understand this might be a unrealistic feature request, but I do have some ideas how it could be implemented. I did see jrwr's tool but it looks like it predates VFS and only kicks in when you start watching a file. I'd like something that kicks in before that. Hopefully this isn't too impossible.

The problem:
I, like many people store my media on Google Drive and mount it to my system via Rclone mount with VFS caching. Great for large libraries and too small local drives, But there's added latency when you start a episode, taking 5-7 seconds for Rclone to go fetch the file from Gdrive and start filling up it's VFS cache. And then Emby can play it. This also can cause some problems with Chromecast I find, where it gets a little confused by the slow loading and isn't sure if the video stream is happening or not, leaving the loading symbol spinning.

The benefit of VFS is that while that file is cached, if you go to play the same episode it will load a lot quicker, within a second or two because it's cached as a local file now. Chromecast is a lot happier too, I often have to play shows and movies twice to get the loading glitch to resolve itself. 

The solution:
Precaching the files before you click into them. Of course you can't just precache everything so we'll want to limit it to some predictable files. I would suggest the next episode of shows, the continue watching section. Even better if we can specify a limit of how many different shows to precache from the most recently watched (only the most recent/the [5] most recent). My understanding is that when you're watching a episode and hit the credits, the next episode finds it's way into the continue watching section so marathon viewing should also be covered by this idea. Theme files (mp4 and mp3) should also be precached as they cause similar issues when browsing a show.

This way when a user clicks into their next episode it's already ready to go in the cache and starts playing back a lot quicker. 

Technical solution ideas:
Because this will only affect certain users, this feature request is probably best depoyed as a optional plugin.

Rclone VFS by default keeps files in cache for a hour before purging based on cache age. Can be configured to be more but let's keep the defaults in mind. We'll need to keep these precached files alive so this might be best done via Emby's Scheduled Tasks. 

I'm picturing Emby grabbing a list of the most recent episodes from Continue Watching and then going and precaching that episode and show theme. Two main methods come to mind.

Rclone rc cache/fetch
Probably the ideal way to do this. But would require some extra setup from the user to ensure rc is working on their rclone mount. Have the plugin send a command that looks like this either periodically and/or when a new item enters continue watching:

rclone rc cache/fetch chunks=0 file=/show/theme.mp4 file=/show/contwatching1 file2=/show/contwatching2

Built in Scanning
Or utilise Emby's built in scanning. Somethingthat would read the file to give rclone a excuse to go fetch it. Touch, tail, ffmpeg, etc. Whatever has the lowest unneeded processing but still reads the file.

touch $contwatching1 $contwatching2


Alternatively if someone has already got some similar functionality coded I would love to give it a try. Thanks,

Link to comment
Share on other sites

  • 3 weeks later...

Through much learning and stress eating I managed to kludge together half of what I want. 

If anyone is here from Google and using Linux here's half of my solution (my shell scripting ability is terrible so feel free to scoff at my inefficiencies). Specifically watching a episode of something will trigger a precache the next episode within the season. Does not work between seasons, and will only last as long as your rclone's VFS max-age setting.

Step 1: Install Scripter-x from the plugins catalogue.

Step 2: Create a script file somewhere Emby has access to (I use docker so i chucked it into my /config folder), copy the below content into it, and make sure it's executable (chmod +x script.sh)
This script assumes all of your shows and episode filenames are Emby friendly, and include this format of episode number "S01E01". it does not matter if your episodes are nested in another layer of folders (eg both "show/season 1/episode S01E01.mkv" and "show/season 1/episode S01E01/episode S01E01.mkv" will work, and even if you have multiple versions of the same episode, it'll cache em all. 

In plain english; This script is getting some info out of Script-x about what file is being played and once you get past the 20% mark, then does a simple probe of the next episode so that it gets cached into VFS.

# Vars from Scripter-x

nextep=$(( $episode+1 ))

# Cuts content off of the path based on "/"s. 
# Change the number 6 if you need to, to make it cut after the Season folder. 
# eg cutpath will look like "/google/media/tv/showname/season 1/"

cutpath=$(echo "$path" | cut -d/ -f1-6)

# Adding a 0 to the episode number if it's a single digit in the stupidest way possible

case $nextep in

# Magic smoke is kept here

find "$cutpath" -type f -name '*' | grep ${season}E${nextepfix} | while read whatsnextf
ffprobe "$whatsnextf"

Step 3: Setup Script-x to look something like this. gotta click the little+ button and edit pen. Type in the location of the script. Emby docker doesn't have bash but it does have sh, update the using to /bin/sh. Type in the %series.name% etc stuff. And the colourful %playback.position% ones are drag and drop from the top.

Before you say anything, yes i know i gave my script a stupid name, and yes it doesnt use even half of the variables im feeding it. This took me a bit of trial and error to get here. One day ill clean it up.



That's it!

So now when you go to watch a episode of something. Script-x sends a command to your script that you're playing let's say "Season 1/The Expanse S01E01.mkv", and then the script goes off and does a ffprobe of anything in the same Season folder that includes the text string "1E02". 

In my testing, having the episode ffprobed results in a small 10MB-ish file in the cache and brings the loading times from over 10 seconds, down to 1 or 2. Great for marathoning shows. 

Edited by Nebarik
  • Thanks 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...