Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Basic script to copy and properly rename Plex synced files on Windows
- # Made because of a bunch of limitations between plex and windows and chromecast
- # 1. Plex Windows app wont cast to chromecast
- # 2. Plex Web app will cast to chrome cast but wont access local synced content
- # 3. Plex Web app will download but not transpose
- # 4. older chromecast does not support x265 decoding, so needed transpose
- #
- # Plex stores the transposed synced files in a non-user-friendly manner using GUIDs as subdirecotries and all files are called "File.*"
- # This script finds the files and extracts the associated metadata, creates a friendly filename and copies to the specified destination.
- #
- # limited testing, no warranty/support. Use at your own risk.
- # Set $destination to where you would like your files to be placed
- $destination="C:\PlexFiles"
- # If you have not moved you synced items in the Plex windows app Settings this should work as is
- #If you have moved them update relevant paths, taking care to retain necessary subdirs, it should point to valid sync directory.
- $root="$env:LOCALAPPDATA\Packages\CAF9E577.Plex_aam28m9va5cke\LocalState\SyncStorage\sync"
- #used to unzip the metadata XML
- Function DeGZip-File{
- Param(
- $infile,
- $outfile
- )
- $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
- $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
- $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
- $buffer = New-Object byte[](1024)
- while($true){
- $read = $gzipstream.Read($buffer, 0, 1024)
- if ($read -le 0){break}
- $output.Write($buffer, 0, $read)
- }
- $gzipStream.Close()
- $output.Close()
- $input.Close()
- }
- # search for .mkv, *.avi, *,mp4 (add more to the include if you need to)
- $files=Get-ChildItem -path "$root\*" -Recurse -include *.mkv,*.avi,*.mp4
- # if destination directory does not exist create it
- if (-not(Test-Path $destination)){New-Item -Type Directory -Path $destination}
- #loop for each file, decide if episode or movie. get metadata and make new filename. Copy file from sync to destination using new filename
- foreach ($file in $files){
- $library=(get-item $file.Directory).Parent.Parent
- $ID=split-path $file.PSParentPath -leaf
- $metadata="$($library.FullName)\metadata\$ID"
- DeGZip-File -infile $metadata\index.xml.gz -outfile $metadata\index.xml
- [xml]$xml=Get-Content $metadata\index.xml
- [string]$type=$xml.LastChild.ChildNodes.Type
- if($type -eq "episode"){
- $showname=$xml.LastChild.ChildNodes.grandparentTitle
- [int]$seasonnum=($xml.LastChild.ChildNodes.parentTitle).Split(" ")[1]
- [int]$Episodenum=$xml.LastChild.ChildNodes.index
- $title=$xml.LastChild.ChildNodes.title
- $extension=$file.extension
- $filename=("$showname"+"."+"S$(“{0:D2}” -f $seasonnum)"+"E$(“{0:D2}” -f $Episodenum)"+"."+"$Title"+"$extension").replace(" ",".")
- #$filename
- remove-item $metadata\index.xml -Force
- Write-Output "Identified File $filename. Copying...."
- Copy-Item $file.FullName $destination\$filename
- Write-Output "$filename File Transferred to $destination."
- }
- if($type -eq "movie"){
- $title=$xml.LastChild.ChildNodes.title
- $extension=$file.extension
- $filename=$title.Replace(" ",".")+$extension
- #$filename
- remove-item $metadata\index.xml -Force
- Write-Output "Identified File $filename. Copying...."
- Copy-Item $file.FullName $destination\$filename
- Write-Output "$filename File Transferred to $destination."
- }
- }
- # pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement