Plex Media Server - fix media date added

Post Reply
K6XTC
Site Admin
Posts: 23
Joined: Sat Oct 26, 2019 12:09 am

Plex Media Server - fix media date added

Post by K6XTC » Tue Jan 14, 2020 7:54 pm

I was working on my Plex Media Server and somehow the date added for all my media was reset to the current date. Most annoying as the Recently Added list isn't correct. Plex doesn't make this setting available in its UI and information on what, or how, to update is spotty at best.

I have written a PHP script that will scan a directory, locate each media file in Plex's database and changes the added date to the date the media file was last updated on disk.

Code: Select all

 <?php

/*
Project:          fix-plex-added.php
Script Purpose:   Updates added date for each media item in a Plex Media Server database to the date
                  the media file was last updated.
Date:             29 November 2020
Author:           Duane Davis

Modify $filepath to reflect the path to your media files. If you have multiple directories to
process you can leave it as "." and run this script from each directory.

Modify $plexdb to reflect the path/name of your Plex database.

NOTES:
   This script was tested with PlexMediaServer version 1.18.4.2171i under Debian GNU/Linux 9
   (stretch). It might not work with a different version or in a different environment.

   This script will create a backup of your database each time it is run. You may delete these
   backups once you verify results of any changes.

   PlexMediaServer should be stopped before running this on your live database.
*/


$filepath = ".";
$plexdb = "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db";

if (!is_dir($filepath)) {
   exit('ERROR: file path does not exist');
}

if (!is_file($plexdb)) {
   exit('ERROR: Plex database does not exist');
}

$result = $plexdb.'-'.time();
print "Creating backup of database at $result.";
copy($plexdb,$result);

$db = new SQLite3($plexdb);

if(!$db) {
   echo $db->lastErrorMsg();
} else {
   echo "Opened Plex database successfully\n";
}

foreach (scandir($filepath) as $file) {
   if ($file !== '.' && $file !== '..') {
      $fdate = date('Y-m-d H:i:s', filemtime($file));

      print "Processing $file\n \t Last modified: \t $fdate\n";

      $result = $db->querySingle('SELECT "media_item_id" FROM "media_parts" WHERE "file" LIKE "%'.$file.'%"', true);

      if ($result) {
         $media_item_id = $result["media_item_id"];

         $result = $db->querySingle('SELECT "metadata_item_id" FROM "media_items" WHERE "id" = "'.$media_item_id.'"', true);
         if ($result) {
            $metadata_item_id = $result["metadata_item_id"];

            $result = $db->querySingle('SELECT "added_at" FROM "metadata_items" WHERE "id" = "'.$metadata_item_id.'"', true);

            if ($result) {
               $added_at = $result["added_at"];

               print "\t Plex added on: \t $added_at\n";

               if ($added_at === $fdate) {
                  print "\t Status: \t \t update not needed.";
               } else {
                  print "\t Status: \t \t updating.";
                  $result = $db->exec('UPDATE "metadata_items" SET "added_at" = "'.$fdate.'" WHERE "id" = "'.$metadata_item_id.'"');

               }

            } else {
               exit("ERROR: Sql query failed (table: metadata_items)");
            }

         } else {
            exit("ERROR: Sql query failed (table: media_items)");
         }

      } else {
         print "\t Status: \t \t not found in Plex database.";
      }

      print "\n";
   }

lirwin4
Posts: 1
Joined: Thu Feb 10, 2022 5:23 pm

Re: Plex Media Server - fix media date added

Post by lirwin4 » Thu Feb 10, 2022 5:26 pm

A bit of a noob here. Could someone please explain how do I add this to Plex?

K6XTC
Site Admin
Posts: 23
Joined: Sat Oct 26, 2019 12:09 am

Re: Plex Media Server - fix media date added

Post by K6XTC » Sun Jul 10, 2022 2:33 am

Sorry for the long delay in responding. For some reason the system never notified me of your post.

The script is not "added" to plex. It accesses, and modifies, the database files directly. You need to have PHP installed.

Post Reply