Last active
August 19, 2024 17:48
-
-
Save eliottrobson/3f2f81c6abed9e4e11822844dba7cd6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ################################################################################ | |
| # # | |
| # Script Name: plex_ondeck_preload.sh # | |
| # Description: Preloads media files from Plex’s “On Deck” section for faster # | |
| # access and improved performance. # | |
| # # | |
| # Author: eliottrobson # | |
| # Date: 2024-08-19 # | |
| # Version: 1.1 # | |
| # # | |
| ################################################################################ | |
| # # | |
| # Notes: # | |
| # - Now supports all the users with access to a server # | |
| # - Set the following variables as required. # | |
| # # | |
| ################################################################################ | |
| # Server settings | |
| PLEX_SERVER='http://***:32400' | |
| PLEX_TOKEN='***' | |
| PLEX_DATA_FOLDER='/mnt/user/media' | |
| # Cache Settings | |
| PRELOAD_HEAD_SIZE="400MB" | |
| PRELOAD_TAIL_SIZE="10MB" | |
| PRELOAD_THRESHOLD="0.5" | |
| PRELOAD_VIDEOS='avi|mkv|mov|mp4|mpeg' # https://support.plex.tv/articles/203824396-what-media-formats-are-supported/ | |
| PRELOAD_SUBTITLES='srt|smi|ssa|ass|vtt' # https://support.plex.tv/articles/200471133-adding-local-subtitles-to-your-media/#toc-1 | |
| ################################################################################ | |
| # Script # | |
| ################################################################################ | |
| # Load account + users data | |
| account_response=$(curl --no-progress-meter --location "$PLEX_SERVER/myplex/account?X-Plex-Token=$PLEX_TOKEN" --header 'Accept: application/json') | |
| account_username=$(echo "$account_response" | jq -r '.MyPlex.username') | |
| identity_response=$(curl --no-progress-meter --location "$PLEX_SERVER/identity?X-Plex-Token=$PLEX_TOKEN" --header 'Accept: application/json') | |
| identity_machine=$(echo "$identity_response" | jq -r '.MediaContainer.machineIdentifier') | |
| users_response=$(curl --no-progress-meter --location "https://plex.tv/api/servers/$identity_machine/shared_servers?X-Plex-Token=$PLEX_TOKEN") | |
| users_tokens=("$account_username:$PLEX_TOKEN") | |
| while IFS= read -r users_response_server; do | |
| username=$(echo "$users_response_server" | sed -n 's/.*username="\([^"]*\)".*/\1/p') | |
| access_token=$(echo "$users_response_server" | sed -n 's/.*accessToken="\([^"]*\)".*/\1/p') | |
| users_tokens=("${users_tokens[@]}" "$username:$access_token") | |
| done < <(echo "$users_response" | grep '<SharedServer') | |
| # Loop over each user | |
| for user in "${users_tokens[@]}"; do | |
| username="${user%%:*}" | |
| access_token="${user##*:}" | |
| echo "Loading on-deck for $username" | |
| ondeck_response=$(curl --no-progress-meter --location "$PLEX_SERVER/library/onDeck?X-Plex-Token=$access_token" --header 'Accept: application/json') | |
| media_files=$(echo "$ondeck_response" | jq -r '.MediaContainer.Metadata // [] | .[]?.Media // [] | .[]?.Part // [] | .[]?.file') | |
| # Loop over each media file | |
| while IFS= read -r media_file; do | |
| array_file=${media_file/\/data/$PLEX_DATA_FOLDER} | |
| if [[ ! $array_file =~ \.($PRELOAD_VIDEOS)$ ]]; then | |
| echo "Skipped $array_file as not valid video file" | |
| break | |
| fi | |
| # Preload head | |
| TIMEFORMAT=%R | |
| seconds=$( { time head -c "$PRELOAD_HEAD_SIZE" "$array_file" >/dev/null; } 2>&1 ) | |
| if awk 'BEGIN {exit !('"$seconds"' >= '"$PRELOAD_THRESHOLD"')}'; then | |
| preloaded=$((preloaded + 1)) | |
| echo "Preloaded $array_file in $seconds seconds" | |
| else | |
| echo "Skipped $array_file as loading needed only $seconds" | |
| skipped=$((skipped + 1)) | |
| fi | |
| # Preload tail | |
| tail -c "$PRELOAD_TAIL_SIZE" "$array_file" > /dev/null | |
| # Preload subtitles | |
| array_path=$(dirname "$array_file") | |
| find "$array_path" -not -path '*/.*' -regextype posix-extended -regex ".*\.($PRELOAD_SUBTITLES)" -print0 | | |
| while IFS= read -r -d '' file; do | |
| echo "Preloaded $file" | |
| cat "$file" >/dev/null | |
| done | |
| done <<< "$media_files" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment