Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Created August 9, 2023 13:56
Show Gist options
  • Select an option

  • Save Cdaprod/51391cda49b9542432e5a6dbccbc0b2e to your computer and use it in GitHub Desktop.

Select an option

Save Cdaprod/51391cda49b9542432e5a6dbccbc0b2e to your computer and use it in GitHub Desktop.
Interactive Jupyter Notebook guiding users on how to index a Synology NAS using Python. Created by David Cannan. Adapt and use as your own workspace.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "495d70c9",
"metadata": {},
"source": [
"<a href=\"https://github.com/Cdaprod\">\n",
" <img src=\"https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png\" width=\"20\" align=\"left\" style=\"margin-right:10px\"> My name is David Cannan, follow me on GitHub!\n",
"</a>\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "9c41c192",
"metadata": {},
"source": [
"\n",
"# Interactive Tutorial: Indexing Media on Synology NAS\n",
"\n",
"In this tutorial, we'll guide you through the process of indexing various media files on your Synology NAS. \n",
"This includes scanning for files, extracting metadata, storing it in a SQLite database, and then visualizing the data using Pandas.\n"
]
},
{
"cell_type": "markdown",
"id": "3c5d64e2",
"metadata": {},
"source": [
"\n",
"## Setup\n",
"\n",
"Before we start, ensure you have the required libraries installed. Run the cell below to install them:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fc0e050",
"metadata": {},
"outputs": [],
"source": [
"\n",
"!pip install moviepy Pillow pandas sqlite3\n"
]
},
{
"cell_type": "markdown",
"id": "57c8c8af",
"metadata": {},
"source": [
"\n",
"## Accessing the Synology NAS\n",
"\n",
"First, ensure your Synology NAS is accessible from your network. \n",
"You may need to mount it on your system where this Jupyter notebook is running.\n",
"Set the `NAS_PATH` variable to point to the mounted directory.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8928fe9",
"metadata": {},
"outputs": [],
"source": [
"NAS_PATH = '/path/to/your/synology/mounted/folder'"
]
},
{
"cell_type": "markdown",
"id": "0e66fc66",
"metadata": {},
"source": [
"\n",
"## Script to Index Media Files\n",
"\n",
"Below is the script that carries out the media indexing process:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ca8d1fc",
"metadata": {},
"outputs": [],
"source": [
"\n",
"import os\n",
"import sqlite3\n",
"import pandas as pd\n",
"from PIL import Image, ExifTags\n",
"import moviepy.editor as mp\n",
"\n",
"# 1. Access the Synology NAS\n",
"NAS_PATH = '/path/to/your/synology/mounted/folder'\n",
"\n",
"# 2. Scan for All Files\n",
"def get_all_files_from_directory(directory):\n",
" for root, _, files in os.walk(directory):\n",
" for file in files:\n",
" yield os.path.join(root, file)\n",
"\n",
"# 3. Determine File Type\n",
"def determine_file_type(file_path):\n",
" if file_path.lower().endswith(('.png', '.jpg', '.jpeg')):\n",
" return \"image\"\n",
" elif file_path.lower().endswith(('.mp4', '.mkv', '.avi')):\n",
" return \"video\"\n",
" else:\n",
" return \"other\"\n",
"\n",
"# 4. Extract Metadata Based on File Type\n",
"def get_metadata(file_path, file_type):\n",
" data = {\"path\": file_path}\n",
" if file_type == \"image\":\n",
" with Image.open(file_path) as img:\n",
" data[\"width\"], data[\"height\"] = img.size\n",
" for tag, value in img._getexif().items():\n",
" if tag in ExifTags.TAGS:\n",
" data[ExifTags.TAGS[tag]] = value\n",
" elif file_type == \"video\":\n",
" clip = mp.VideoFileClip(file_path)\n",
" data[\"duration\"] = clip.duration\n",
" data[\"fps\"] = clip.fps\n",
" data[\"width\"], data[\"height\"] = clip.size\n",
" clip.reader.close()\n",
" clip.audio.reader.close_proc()\n",
" return data\n",
"\n",
"# 5. Store in a Database\n",
"def store_data_in_db(data_list):\n",
" connection = sqlite3.connect('media.db')\n",
" cursor = connection.cursor()\n",
" \n",
" # Create tables for different types of media\n",
" cursor.execute('''\n",
" CREATE TABLE IF NOT EXISTS media (\n",
" id INTEGER PRIMARY KEY,\n",
" path TEXT NOT NULL,\n",
" type TEXT,\n",
" width INTEGER,\n",
" height INTEGER,\n",
" duration REAL,\n",
" fps REAL\n",
" )\n",
" ''')\n",
" \n",
" cursor.executemany('''\n",
" INSERT INTO media (path, type, width, height, duration, fps)\n",
" VALUES (:path, :type, :width, :height, :duration, :fps)\n",
" ''', data_list)\n",
" \n",
" connection.commit()\n",
" connection.close()\n",
"\n",
"# Gather data and store in DB\n",
"all_files = get_all_files_from_directory(NAS_PATH)\n",
"data_list = []\n",
"for file_path in all_files:\n",
" file_type = determine_file_type(file_path)\n",
" metadata = get_metadata(file_path, file_type)\n",
" metadata[\"type\"] = file_type\n",
" data_list.append(metadata)\n",
"\n",
"store_data_in_db(data_list)\n",
"\n",
"# 6. Display Data with Pandas\n",
"connection = sqlite3.connect('media.db')\n",
"df = pd.read_sql_query(\"SELECT * FROM media\", connection)\n",
"print(df)\n"
]
},
{
"cell_type": "markdown",
"id": "6d0cdb8f",
"metadata": {},
"source": [
"\n",
"## Conclusion\n",
"\n",
"Congratulations on creating a media index of your Synology NAS! \n",
"This interactive guide provides a foundation, and you can extend the functionalities based on your requirements.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment