The Movie Database Support

Hello Community,

If anyone has a common interest, I am working on a query to update metadata or object attributes of local MP4 and MKV files stored on disk locally.
The script is close to working but I could use some pointers from the expert community because I am a beginner at using this API.

I want to use the API to pull in the movie data and update what is possible in the local files.

Fields of Interest if they map from TMDB to the MP4 movie files

Title The title of the movie Year The year of release of the movie Director The name of the movie's director Genre The genre(s) of the movie Description/Plot A brief description or plot summary of the movie Rating The rating or parental guidance information Studio The production studio or company Cast The actors or cast members in the movie Tags Additional keywords or tags associated with the movie Artwork/Poster The movie's artwork or poster image Language The primary language of the movie Subtitles The available subtitles for the movie Duration/Length The duration or length of the movie in seconds IMDb ID The IMDb (Internet Movie Database) identifier TMDb ID The TMDb (The Movie Database) identifier Original Title The original title of the movie (if different from the localized title)

*POWERSHELL SCRIPT START HERE: *

Clear the prior screen results

Clear-Host 

Start in the Movies directory

CD "C:\MOVIES"

Set your TMDb API key - replace with your own key

$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Set the directory path where your movies are located

$moviesPath = "C:\MOVIES"

Define a function to update the movie metadata

function Update-MovieMetadata {
    param (
        [string]$filePath,
        [string]$title,
        [int]$year,
        [string]$releaseDate,
        [string]$certification,
        [int]$runtime,
        [string]$genres,
        [string]$productionCompanies,
        [string]$countries,
        [string]$spokenLanguages,
        [int]$tmdbId,
        [string]$imdbId,
        [string]$tagline,
        [string]$plot
    )

Retrieve the file's existing metadata

    $existingMetadata = (Get-ItemProperty -Path $filePath).PSObject.Properties | Where-Object {$_.Name -like 'Metadata*'}

    # Update the metadata properties
    $existingMetadata.Title = $title
    $existingMetadata.Year = $year
    $existingMetadata.'Release date' = $releaseDate
    $existingMetadata.Certification = $certification
    $existingMetadata.Runtime = $runtime
    $existingMetadata.Genres = $genres
    $existingMetadata.Production = $productionCompanies
    $existingMetadata.Country = $countries
    $existingMetadata.'Spoken languages' = $spokenLanguages
    $existingMetadata.'TMDB ID' = $tmdbId
    $existingMetadata.'IMDB ID' = $imdbId
    $existingMetadata.Tagline = $tagline
    $existingMetadata.Plot = $plot

Set the updated metadata to the file
    Set-ItemProperty -Path $filePath -Name $existingMetadata.Name -Value $existingMetadata.Value
}

Get a list of all .mp4 and .mkv files in the specified directory

$movieFiles = Get-ChildItem -Path $moviesPath -Include *.mp4, *.mkv -Recurse

Iterate through each movie file and update its metadata

foreach ($file in $movieFiles) {
    # Extract the movie name from the file name 
    # The replace is to remove the year from the search becaue I was getting null or wrong results with it present. 
    $movieName = $file.BaseName -replace '\s*\(.*?\)'

Construct the API URL

    $apiUrl = "https://api.themoviedb.org/3/search/movie?api_key=$apiKey&query=$movieName"

Invoke the API and retrieve the movie details $response = Invoke-RestMethod -Uri $apiUrl

if ($response.total_results -gt 0) {
    # Retrieve the first movie result
    $movieData = $response.results[0]

    # Extract the desired metadata from the movie data
    $title = $movieData.title
    $year = $movieData.release_date.Substring(0, 4)
    $releaseDate = $movieData.release_date
    $certification = $movieData.certification
    $runtime = $movieData.runtime
    $genres = ($movieData.genres | Select-Object -ExpandProperty name) -join ', '
    $productionCompanies = ($movieData.production_companies | Select-Object -ExpandProperty name) -join ', '
    $countries = ($movieData.production_countries | Select-Object -ExpandProperty name) -join ', '
    $spokenLanguages = ($movieData.spoken_languages | Select-Object -ExpandProperty name) -join ', '
    $tmdbId = $movieData.id
    $imdbId = $movieData.imdb_id
    $tagline = $movieData.tagline
    $plot = $movieData.overview

Update the movie metadata Update-MovieMetadata -filePath $file.FullName -title $title -year $year -releaseDate $releaseDate -certification $certification -runtime $runtime -genres $genres -productionCompanies $productionCompanies -countries $countries -spokenLanguages $spokenLanguages -tmdbId $tmdbId -imdbId $imdbId -tagline $tagline -plot $plot

    Write-Host "Metadata updated for $($file.Name)"
}
else {
    Write-Host "No movie found for $($file.Name)"
}

}

1 reply (on page 1 of 1)

Jump to last post

Hi @RedStapler, you're more likely to get replies if you ask specific questions. I don't see any real questions, just a description of what you are doing with some code. I'm not sure what you want to know??

Can't find a movie or TV show? Login to create it.

Global

s focus the search bar
p open profile menu
esc close an open window
? open keyboard shortcut window

On media pages

b go back (or to parent when applicable)
e go to edit page

On TV season pages

(right arrow) go to next season
(left arrow) go to previous season

On TV episode pages

(right arrow) go to next episode
(left arrow) go to previous episode

On all image pages

a open add image window

On all edit pages

t open translation selector
ctrl+ s submit form

On discussion pages

n create new discussion
w toggle watching status
p toggle public/private
c toggle close/open
a open activity
r reply to discussion
l go to last reply
ctrl+ enter submit your message
(right arrow) next page
(left arrow) previous page

Settings

Want to rate or add this item to a list?

Login