diff --git a/plexdb.py b/plexdb.py new file mode 100644 index 0000000..c6c6ba1 --- /dev/null +++ b/plexdb.py @@ -0,0 +1,72 @@ +import os +import sys +import sqlite3 + +def run_db_injection(db_path): + if not os.path.exists(db_path): + print("[-] Error: Plex database not found at {}".format(db_path)) + return False + + print("[*] Connecting to Plex database: {}...".format(db_path)) + try: + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + # Find unanalyzed STRM files + cursor.execute(""" + SELECT media_parts.id, media_items.id, media_parts.file + FROM media_parts + JOIN media_items ON media_parts.media_item_id = media_items.id + WHERE media_parts.file LIKE '%.strm' AND (media_items.width IS NULL OR media_items.width = '') + """) + unanalyzed = cursor.fetchall() + + if not unanalyzed: + print("[+] All STRM files are already analyzed in the database.") + conn.close() + return True + + print("[*] Found {} unanalyzed STRM files. Injecting metadata...".format(len(unanalyzed))) + + for part_id, item_id, file_path in unanalyzed: + # 1. Update Media Item with dummy dimensions and duration (1080p H264 profile) + cursor.execute(""" + UPDATE media_items + SET width=1920, height=1080, duration=7200000, container='mkv', video_codec='h264', audio_codec='aac', + aspect_ratio=1.77, video_frame_rate='24p', size=112 + WHERE id = ? + """, (item_id,)) + + # 2. Delete existing streams for this part to prevent duplicates + cursor.execute("DELETE FROM media_streams WHERE media_part_id = ?", (part_id,)) + + # 3. Insert Video Stream record + cursor.execute(""" + INSERT INTO media_streams ( + media_part_id, stream_type_id, codec, index, language, + bitrate, class, display_title, created_at, updated_at + ) VALUES (?, 1, 'h264', 0, 'eng', 5000000, 'video', '1080p H.264', datetime('now'), datetime('now')) + """, (part_id,)) + + # 4. Insert Audio Stream record + cursor.execute(""" + INSERT INTO media_streams ( + media_part_id, stream_type_id, codec, index, language, + channels, bitrate, class, display_title, created_at, updated_at + ) VALUES (?, 2, 'aac', 1, 'eng', 2, 192000, 'audio', 'AAC Stereo', datetime('now'), datetime('now')) + """, (part_id,)) + + conn.commit() + print("[+] Successfully injected analysis metadata for {} files.".format(len(unanalyzed))) + conn.close() + return True + + except Exception as e: + print("[-] Database operation failed: {}".format(e)) + return False + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python plex_db_helper.py ") + sys.exit(1) + run_db_injection(sys.argv[1])