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)"
}
}
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by robbie3999
on June 23, 2023 at 9:42 AM
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??