Plex Preloader (avoids HDD spinup latency when starting a Movie or Episode)

Recommended Posts

This was an idea of @frodr. After several testings I came to the following script which:

  1. Caclulates how many Video files (small parts of them) will fit into 50% of the free RAM (amount can be changed)
  2. Obtains the X recent Movies / Episodes (depending on the used path)
  3. Preloads 60MB of the Video file leader and 1MB of the ending into the RAM
  4. Preloads subtitle files that belong to the preloaded Video file


Now, if your disks are sleeping and you start a Movie or Episode through Plex, the client will download the Video parts from RAM and while the buffer is emptied, the HDD spins up and the buffer fills up again. This means all preloaded Movies / Episodes will start directly without delay.



  • It does not reserve any RAM, so your RAM stays fully available
  • RAM preloading is not permanent and will be overwritten through server uploads/downloads or processes over the time. I suggest to execute this script 1x per day (only missing Video files will be touched)
  • For best reliability execute the script AFTER all your backup scripts are completed (as for an example rsync can use the complete available RAM while syncing)
  • If you suffer from buffering at the beginning of a Movie / Episode, try to raise "video_min_size"
  • All preloaded Videos can be found in the script's log (CA User Scripts)



# #####################################
# Script:      Plex Preloader v0.9
# Description: Preloads the recent video files of a specific path into the RAM to bypass HDD spinup latency
# Author:      Marc Gutt
# Changelog:
# 0.9
# - Preloads only subtitle files that belong to preloaded video files
# 0.8
# - Bug fix: In some situations video files were skipped instead of preloading them
# 0.7
# - Unraid dashboard notification added
# - Removed benchmark for subtitle preloading
# 0.6
# - multiple video path support
# 0.5
# - replaced the word "movie" against "video" as this script can be used for TV Shows as well
# - reduced preload_tail_size to 1MB
# 0.4
# - precleaning cache is now optional
# 0.3
# - the read cache is cleaned before preloading starts
# 0.2
# - preloading time is measured
# 0.1
# - first release
# ######### Settings ##################
video_min_size="2000MB" # 2GB, to exclude bonus content
preload_head_size="60MB" # 60MB, raise this value if your video buffers after ~5 seconds
preload_tail_size="1MB" # 10MB, should be sufficient even for 4K
video_ext='avi|mkv|mov|mp4|mpeg' #
sub_ext='srt|smi|ssa|ass|vtt' #
# #####################################
# ######### Script ####################
# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;
# check user settings
video_min_size="${video_min_size//[!0-9.]/}" # float filtering
video_min_size=$(awk "BEGIN { print $video_min_size*1000000}") # convert MB to Bytes
preload_head_size=$(awk "BEGIN { print $preload_head_size*1000000}")
preload_tail_size=$(awk "BEGIN { print $preload_tail_size*1000000}")
# clean the read cache
if [ "$preclean_cache" = "1" ]; then
    sync; echo 1 > /proc/sys/vm/drop_caches
# preload
preload_total_size=$(($preload_head_size + $preload_tail_size))
free_ram=$(free -b | awk '/^Mem:/{print $7}')
free_ram=$(($free_ram / 100 * $free_ram_usage_percent))
echo "Available RAM in Bytes: $free_ram"
preload_amount=$(($free_ram / $preload_total_size))
echo "Amount of Videos that can be preloaded: $preload_amount"
# fetch video files
while IFS= read -r -d '' file; do
    if [[ $preload_amount -le 0 ]]; then
    size=$(stat -c%s "$file")
    if [ "$size" -gt "$video_min_size" ]; then
        benchmark=$(time ( head -c $preload_head_size "$file" ) 2>&1 1>/dev/null )
        echo "Preload $file (${benchmark}s)"
        if awk 'BEGIN {exit !('$benchmark' >= '0.150')}'; then
            preloaded=$((preloaded + 1))
            skipped=$((skipped + 1))
        tail -c $preload_tail_size "$file" > /dev/null
        preload_amount=$(($preload_amount - 1))
        video_path=$(dirname "$file")
        # fetch subtitle files
        find "$video_path" -regextype posix-extended -regex ".*\.($sub_ext)" -print0 | 
            while IFS= read -r -d '' file; do 
                echo "Preload $file"
                cat "$file" >/dev/null
done < <(find "${video_paths[@]}" -regextype posix-extended -regex ".*\.($video_ext)" -printf "%T@ %p\n" | sort -nr | cut -f2- -d" " | tr '\n' '\0')
# notification
if [[ $preloaded -eq 0 ]] && [[ $skipped -eq 0 ]]; then
    /usr/local/emhttp/webGui/scripts/notify -i alert -s "Plex Preloader failed!" -d "No video file has been preloaded (wrong path?)!"
elif [ "$notification" = "1" ]; then
    /usr/local/emhttp/webGui/scripts/notify -i normal -s "Plex Preloader has finished" -d "$preloaded preloaded (from Disk) / $skipped skipped (already in RAM)"


Edited by mgutt
  • Like 4
  • Thanks 2
Link to comment

I'm still experimenting on this. I like to write the preloaded movie parts directly to a swap file, but I can't find a solution for this at the moment (Linux decides on its own which data is swapped). If this would be possible the complete Movie collection could be preloaded to a "small" swap file located on an SSD.


I even played around with vmtouch to make this preloading permanent (as vmtouch can lock it in the RAM), but at the moment vmtouch seems to be buggy in Slackware (vmtouch daemon becomes randomly killed and sometimes its not possible to get any caching status).


Edited by mgutt
  • Thanks 1
Link to comment

Here you can see the RAM usage in the column "buff/cache". Before Plex Preloader:

sync; echo 1 > /proc/sys/vm/drop_caches
free -m
              total        used        free      shared  buff/cache   available
Mem:          64358        1154       60873        1027        2330       61580
Swap:             0           0           0


free -m
              total        used        free      shared  buff/cache   available
Mem:          64358        1216       27655        1027       35485       61405
Swap:             0           0           0

This means it used 35485 - 2330 = 33155 MB as Preload RAM Cache.


After that I spun down all my disks, opened PMP and started a recently added movie and it started without any latency.



I spun down all disks again and started one of my very first added movies and it took 8 seconds until it started playing.



Edited by mgutt
Link to comment
2 hours ago, Mushin said:

Is it possible to also get this working with TV files? Maybe just for the ones displayed in On Deck?

Yes, use the same script and change these values:

video_min_size="2000MB" # 2GB, to exclude bonus content

as follows (set your TV path of course):


While "500MB" is only an estimate of the minimum size of a TV episode. You could even lower it to "1MB" to cover everything.


If you use both scripts parallel (to cache Movies and TV Shows) you should think about reducing this value as well:


Like 25 for TV shows and 25 for Movies or similar.

Edited by mgutt
  • Like 1
Link to comment

Feature Requests from Reddit:

- determine which disks were involved and spin them down after script execution

- multiple path support (to scan movies and tv shows through one script) realised

- obtain recent tv shows through Plex database and add the X next episodes to the cache (alternative request: move complete episodes to SSD cache which could be part of an additional script)




Edited by mgutt
  • Like 2
Link to comment
2 hours ago, mgutt said:

- obtain recent tv shows through Plex database and add the X next episodes to the cache (alternative request: move complete episodes to SSD cache which could be part of an additional script)

In case it helps, I think this is how to get the On Deck from Plex using the API:

wget -q -O- http://$PLEXURL:32400/library/onDeck?X-Plex-Token=$PLEXTOKEN

Set $PLEXURL to your PMS's ip address, and $PLEXTOKEN to your Plex Auth Token, and it'll return with XML. You'll then need to parse out the 'file=' entries to get the filename and path, and replace the returned filepath with whatever the Docker container is mapped to (for example, I pass /tv to my Plex docker container and it maps to /mnt/user/TV on my unRAID).

  • Thanks 2
Link to comment

- I was reading about the precaching and storing on on SSD on Redditt. That's a very interesting idea which would save everybody money if they have to up their Ram or simply another option.

- Could you put in a pre-test or simply a way to say, "Scan 10 files and test" vs a scan everything you can and find out your existing settings are not a enough?  Depending on some peoples Media and Ram it could scan for a long time just to find out they should either up the amount cached or lower. Could be something as simple as:


#####Uncomment below####

#Test Scan only 10 Files

#Full Scan



Could there be a (Cache, Flush and Cache or maybe a Compare and update)? If Changes are made. No idea how you intended to address changes in users scans if they need to update their amounts or if files are added/removed. I'm assuming Plex simply wouldn't show the file, but not sure how removing a file would affect the Cache. 


Are you planning on adding this as a Plugin in the future? Something that could be added to CA and updated or keeping it simple as a User.Script? Both work obviously, but just something for you to think about in the future. 

Link to comment

@kizer Check if the movie is listed in the script's logs and how much time it took to read the file part. It will look similar to this:

Available RAM in Bytes: 32628881400
Amount of Videos that can be preloaded: 534
Preload Video /mnt/user/Movie/EF/Falling Down - Ein ganz normaler Tag (1993)/Falling Down - Ein ganz normaler Tag (1993) 1080P FSK16 EN JP FR IT ES PT IMDB7.6.mkv

real 0m0.545s
user 0m0.007s
sys 0m0.033s

Now repeat the script and check the logs again.


1.) Did the "real" time of this movie fall under 0.100s?

a) If not, then it was re-read from disk and the caching failed. This means other processes are blocking or permanently overwriting your RAM. Possible solutions: More RAM or execute the Plex Preloader script after your processes finished their work

b) If yes, then the video file part is in the RAM and you can try to start the movie while your disks are sleeping.


2.) Did the Movie played directly?

a) If not, which Player did you use and was transcoding needed? Which path do you use to include your video files in your docker container, do they use the same path that was used by the Plex Preloader script?

b) If yes. It works.






Link to comment

When I checked the log in User.Scripts it just said the following and nothing else:


Available RAM in Bytes: 16G Here don't know the exact digits so I just paraphrased it. 

Amount of Videos that can be preloaded: 110


I used Plex to view the File on my AppleTV.  I'm not 100% sure if any Transcoding was needed. Its a MKV with 6CH/AC3 Audio.  I'll look again this Evening for a 2CH video or try it on TV shows since a lot of mine are ripped to 2CH. I have no issues with buying more Ram. I just want to see this work before I lay out the money to 32GB. 


Link to comment

Yes my Given movie path is



OH NO....... I used



I just checked my Log. I see a lot more info populating since adding the s.  🤪


I'll let that run until I get off work and try again. Talk about feeling kinda dumb. 

  • Haha 1
Link to comment
24 minutes ago, kizer said:

or does it run the first path and then run to the second path until the 50% of ram is used up?


The files of all paths are added to one huge list, which is sorted by date and by that the most recent video files of all paths will be preloaded.


Example: If your RAM allows preloading of 100 videos and you added 90 new movies to your collection, it will preload only 10 episodes.


This means if you prefer 25/25 you still need to use one script per path.


Link to comment

Just gave the new Script a try and this is the output.


Script location: /tmp/user.scripts/tmpScripts/Media-Preload-Movies/script

Note that closing this window will abort the execution of this script
Available RAM in Bytes: 6726713300
Amount of Videos that can be preloaded: 110

Preload /mnt/user/Movies/All/Rush.Hour.3.(2007)(720p)(DTS)/Rush.Hour.3.(2007)(720p).srt
Preload /mnt/user/Movies/All/Waiting.(2005)(720p)(AC3)/Waiting.(2005)(720p).srt
Preload /mnt/user/Movies/All/Rush.Hour.2.(2001)(720p)(AC3)/Rush.Hour.2.(2001)(720p).srt
Preload /mnt/user/Movies/All/The.Librarian.The.Curse.Of.The.Judas.Chalice.(2008)(720p)(AC3)/The.Librarian.The.Curse.Of.The.Judas.Chalice.(2008)(720p).srt
Preload /mnt/user/Movies/1080-Bray-Rips/Avatar/
Preload /mnt/user/TV/Shows/Adventure Time/Season 7/
Preload /mnt/user/TV/Shows/Adventure Time/Season 7/
Preload /mnt/user/TV/Shows/Adventure Time/Season 7/
Preload /mnt/user/TV/Shows/Adventure Time/Season 7/
Preload /mnt/user/TV/Shows/Adventure Time/Season 7/
Preload /mnt/user/TV/Shows/Adventure Time/Season 8/
Preload /mnt/user/TV/Shows/Adventure Time/Season 8/
Preload /mnt/user/TV/Shows/Top Secret Recipe/Season 1/
Preload /mnt/user/TV/Shows/Dexter/Season 4/


Link to comment

This script looks really good and very useful. Is there a way to modify this script to pre load parts of films (4K movies) but with drives spinning. I don’t have my drives sleep much. But notice when watching 4K movies. I have a 5-10 second delay before it starts. 1080p ones is only 2-3 secs. I have a small collection of 4K movies. Was wondering if I could pre load about 180mb of each one into ram to see if it helps with start time

Link to comment
5 hours ago, Apollopayne35 said:

Is there a way to modify this script to pre load parts of films (4K movies) but with drives spinning.

No need to modify the script. It does nothing special except of preloading data into the RAM.


Maybe you suffer from a deep idle state. As an example, Seagate Ironwolf and Exos have 4 different idle states, and some of them even reduce the RPM to save energy, which raises latency:


If you want to test only 4K movies you could raise "video_min_size" to "50000MB" (50GB) and if you want to raise the preloading size, change "preload_head_size" to "75MB", "150MB" or "500MB". This should be the sizes which are used through the client:

Link to comment
1 hour ago, kizer said:

Nope. I just cut and pasted your script up above and changed the paths to my Movies and TV shows. 

This is strange. I mean I changed the code, but a) it works for me and b) it was not a huge change


1.) Are you using unraid 6.8.3?

2.) Do you execute the script through CA User Scripts?


Please test this code:


echo "Process substitution test"
while IFS= read -r -d '' file; do
    echo "$file"
done < <(find "$movie_path" -iname "*.mkv" | tail -3 | tr '\n' '\0')

echo "Piping test"
find "$movie_path" -iname "*.mkv" | tail -3 | tr '\n' '\0' | 
    while IFS= read -r -d '' file; do
        echo "$file"

You could even execute it through the WebTerminal. Both tests should return 3 filenames. If not, do you get an error message?

Link to comment

Im running 6.9-RC30

I could fire up my other machine to test on 6.8.3 if you think there are significant changes from 6.8.3 and 6.9-RC-30


This is the output via User.Scripts:


Script location: /tmp/user.scripts/tmpScripts/zzzzzzz-test-zzzzzzzz/script
Note that closing this window will abort the execution of this script
Process substitution test
/mnt/user/Movies/All/xXx -.Return.of.Xander.Cage.(2017)(1080p)/xXx -.Return.of.Xander.Cage.(2017)(1080p).mkv
Piping test
/mnt/user/Movies/All/xXx -.Return.of.Xander.Cage.(2017)(1080p)/xXx -.Return.of.Xander.Cage.(2017)(1080p).mkv

Link to comment
4 hours ago, kizer said:

This is the output via User.Scripts:

Ok, now I'm confused as both code variants work.


Edit v0.7 and search for:

size=$(stat -c%s "$file")

Replace it against:

size=$(stat -c%s "$file")
echo "$file has a size of $size"

After execution. Do you see an massive amount of entries in the logs or does it still only return srt filenames?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.