Recently I started toying around with the idea of whether or not I could make my own streaming platform and host it as an app on one of the smart TVs in my home. Streaming services come and go, licensing rights shift, and shows on your favorite platform can disappear overnight. Just ask Funimation how its platform is holding up after being acquired by Crunchyroll.
Not only would I get to flex some of my coding/build muscles, but I could also gain some hands on experience with building specifically with the TV interface in mind. With hundreds of anime related shows and movies between myself and my partner in our physical media collection, the laborious part would be digitizing all of that media right?
*For clarification and to be up front. I own all of the media discussed in this post on physical disc. No media in these posts were pirated from other sources.
The Goal
I want to side load a native smart TV app environment that:
- Streams anime from my local storage.
- Retrieves the correct metadata for each property (ie; title, description, release year, related poster images).
- Organizes the movies and TV shows similar to other streaming services.
- Has a nostalgic touch to the UI. Sort of a Toonami throwback look.
Roadblock 1
Right now, some of anime collection is stored locally in a directory in my machine, with TV shows in subfolders. The first challenge is making sense of this raw data—automatically pulling in metadata so my collection looks like a proper library rather than just a list of filenames.
I need to write a python script that:
- Scans my anime directory for files and folders.
- Matches each title to metadata from The Movie Database (TMDb).
- Saves that data in a structured format for the user interface.
Prerequisites
Before running the python script, I’ll need:
- A TMDb API key
- Python 3 installed.
- The requests and tmdbv3api libraries (pip install requests tmdbv3api).
Python Script
import os
import json
import re
import requests
from tmdbv3api import TMDb, Movie, TV
# TMDb API setup
tmdb = TMDb()
tmdb.api_key = "YOUR_TMDB_API_KEY"
movie_api = Movie()
tv_api = TV()
# Directory where anime is stored
ANIME_DIR = os.path.expanduser("~/[YOUR_DIRECTORY]/")
# Function to clean file names (remove resolution, codecs, etc.)
def clean_filename(filename):
filename = re.sub(r'\[.*?\]|\(.*?\)|\d{3,4}p|BluRay|DVD|Dual Audio|HEVC|x264|x265', '', filename, flags=re.IGNORECASE)
filename = re.sub(r'\s+', ' ', filename).strip()
return filename
# Function to fetch metadata from TMDb
def fetch_metadata(title):
movie_results = movie_api.search(title)
if movie_results:
movie = movie_results[0]
return {
"title": movie.title,
"type": "Movie",
"release_year": movie.release_date[:4] if movie.release_date else "Unknown",
"overview": movie.overview,
"poster": f"https://image.tmdb.org/t/p/w500{movie.poster_path}" if movie.poster_path else None
}
tv_results = tv_api.search(title)
if tv_results:
show = tv_results[0]
return {
"title": show.name,
"type": "TV Show",
"release_year": show.first_air_date[:4] if show.first_air_date else "Unknown",
"overview": show.overview,
"poster": f"https://image.tmdb.org/t/p/w500{show.poster_path}" if show.poster_path else None
}
return {"title": title, "type": "Unknown", "release_year": "Unknown", "overview": "No data found.", "poster": None}
# Function to scan directory and process files
def scan_directory(directory):
anime_library = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith((".mp4", ".mkv", ".avi")):
clean_title = clean_filename(os.path.splitext(file)[0])
metadata = fetch_metadata(clean_title)
metadata["file_path"] = os.path.join(root, file)
anime_library.append(metadata)
print(f"Processed: {metadata['title']} ({metadata['release_year']})")
return anime_library
# Run the script
anime_library = scan_directory(ANIME_DIR)
# Save the data to a JSON file
with open("anime_library.json", "w", encoding="utf-8") as f:
json.dump(anime_library, f, indent=4)
print("Anime library metadata saved!")
What This Script Accomplishes
- It scans my pointed to local directory for .mp4, .mkv, and .avi files.
- It cleans the filenames by removing unnecessary tags like resolution, codecs, and group names.
- It searches TMDb for matching movie or TV show metadata.
- It stores the results in a JSON file (anime_library.json), which can be used later in the UI.
Now that I have structured metadata for my anime collection, the next step is designing a simple UI to display this information—something inspired by Toonami. In future posts, I’ll start building a basic front end to browse and search the library.