diff --git a/plex/plex-analyze-cli.py b/plex/plex-analyze-cli.py new file mode 100644 index 0000000..1d186ea --- /dev/null +++ b/plex/plex-analyze-cli.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# Plex analayze all files that are missing analyzation info +# OS: Ubunti 16.04 ( in case of other OS's make sure to change paths ) +# Usage: python3 plex-analyze-cli.py + +import subprocess +import sqlite3 + +# Define some variables +DBPath = '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db' # This is the default path. +concurrentCount = 100 # This is how many files will be analyzed in 1 command. Plex handles them one at a time but this reduces the number of calls to the scanner and speeds things up. +analyzeScript = "/scripts/plex/reanalyzeItem.sh" # Path to your refreshMetadataItem script. + +conn = sqlite3.connect(DBPath) + +c = conn.cursor() +c.execute('''Select media_items.metadata_item_id As id, metadata_items.title As title + From media_items Inner Join metadata_items On media_items.metadata_item_id = metadata_items.id + Where media_items.bitrate Is Null And Not metadata_items.metadata_type = "12"''') + +items = c.fetchall() +conn.close() + +def analyzeThese(ids): + ids = ids[:-1] + print("Going to analyze the following ID's: {0}".format(ids)) + subprocess.check_call([analyzeScript, ids]) + +print("There are {0} files that need to be analyzed.".format(str(len(items)))) + +itemString = '' +itemCount = len(items) +count = 0 +for row in items: + itemString = itemString + str(row[0]) + ',' # This adds the current item to the ongoing string. + count = count + 1 + itemCount = itemCount - 1 # Keep an up to date number of remaining items. + if itemCount >= concurrentCount: + if count >= concurrentCount: + # Analyze these files. + analyzeThese(itemString) + itemString = '' # Reset the itemString. + else: + if itemCount <= 0 and itemString != '': + analyzeThese(itemString) + itemString = '' # Reset the itemString. + +print("Finished analyzing all items.") \ No newline at end of file diff --git a/plex/plex-analyze.cli b/plex/plex-analyze.cli deleted file mode 100644 index 5d1f2af..0000000 --- a/plex/plex-analyze.cli +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 -# Plex analayze all files that are missing analyzation info -# OS: Ubunti 16.04 ( in case of other OS's make sure to change paths ) - -from subprocess import call -import os -import requests -import sqlite3 -import sys - -conn = sqlite3.connect('/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db') - -c = conn.cursor() -c.execute('select metadata_item_id from media_items where bitrate is null') -items = c.fetchall() -conn.close() - -print("To analyze: " + str( len(items) )) - -for row in items: - os.system('LD_LIBRARY_PATH=/usr/lib/plexmediaserver PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/var/lib/plexmediaserver/Library/Application\ Support /usr/lib/plexmediaserver/Plex\ Media\ Scanner -a -o ' + str(row[0])) - os.system('sleep 1') - print(str(row[0])) diff --git a/plex/reanalyzeItem.sh b/plex/reanalyzeItem.sh new file mode 100644 index 0000000..7f911cc --- /dev/null +++ b/plex/reanalyzeItem.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Re-analyze an item or items. +# Usage: bash reanalyzeItem.sh {itemID} +# bash reanalyzeItem.sh {itemID,itemID} +# bash reanalyzeItem.sh 12345 +# bash reanalyzeItem.sh 12345,67788,23425 + +echo "Input items are: ${1}" + +PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR='/var/lib/plexmediaserver/Library/Application Support' LD_LIBRARY_PATH='/usr/lib/plexmediaserver' /usr/lib/plexmediaserver/Plex\ Media\ Scanner \ + --verbose \ + --analyze \ + --item ${1} + +exit 0