Advertisement
Guest User

GetPlexSyncedItems.ps1

a guest
Nov 18th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Basic script to copy and properly rename Plex synced files on Windows
  2. # Made because of a bunch of limitations between plex and windows and chromecast
  3. # 1. Plex Windows app wont cast to chromecast
  4. # 2. Plex Web app will cast to chrome cast but wont access local synced content
  5. # 3. Plex Web app will download but not transpose
  6. # 4. older chromecast does not support x265 decoding, so needed transpose
  7. #
  8. # Plex stores the transposed synced files in a non-user-friendly manner using GUIDs as subdirecotries and all files are called "File.*"
  9. # This script finds the files and extracts the associated metadata, creates a friendly filename and copies to the specified destination.
  10. #
  11. # limited testing, no warranty/support. Use at your own risk.
  12.  
  13.  
  14. # Set $destination to where you would like your files to be placed
  15. $destination="C:\PlexFiles"
  16.  
  17. # If you have not moved you synced items in the Plex windows app Settings this should work as is
  18. #If you have moved them update relevant paths, taking care to retain necessary subdirs, it should point to valid sync directory.
  19. $root="$env:LOCALAPPDATA\Packages\CAF9E577.Plex_aam28m9va5cke\LocalState\SyncStorage\sync"
  20.  
  21. #used to unzip the metadata XML
  22. Function DeGZip-File{
  23.     Param(
  24.         $infile,
  25.         $outfile      
  26.         )
  27.  
  28.     $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
  29.     $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
  30.     $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress)
  31.  
  32.     $buffer = New-Object byte[](1024)
  33.     while($true){
  34.         $read = $gzipstream.Read($buffer, 0, 1024)
  35.         if ($read -le 0){break}
  36.         $output.Write($buffer, 0, $read)
  37.         }
  38.  
  39.     $gzipStream.Close()
  40.     $output.Close()
  41.     $input.Close()
  42. }
  43.  
  44.  
  45. # search for .mkv, *.avi, *,mp4 (add more to the include if you need to)
  46. $files=Get-ChildItem -path "$root\*" -Recurse -include *.mkv,*.avi,*.mp4
  47.  
  48. # if destination directory does not exist create it
  49. if (-not(Test-Path $destination)){New-Item -Type Directory -Path $destination}
  50.  
  51. #loop for each file, decide if episode or movie. get metadata and make new filename. Copy file from sync to destination using new filename
  52. foreach ($file in $files){
  53.     $library=(get-item $file.Directory).Parent.Parent
  54.     $ID=split-path $file.PSParentPath -leaf
  55.     $metadata="$($library.FullName)\metadata\$ID"
  56.     DeGZip-File -infile $metadata\index.xml.gz -outfile $metadata\index.xml
  57.     [xml]$xml=Get-Content $metadata\index.xml
  58.     [string]$type=$xml.LastChild.ChildNodes.Type
  59.  
  60.     if($type -eq "episode"){
  61.  
  62.         $showname=$xml.LastChild.ChildNodes.grandparentTitle
  63.         [int]$seasonnum=($xml.LastChild.ChildNodes.parentTitle).Split(" ")[1]
  64.         [int]$Episodenum=$xml.LastChild.ChildNodes.index
  65.         $title=$xml.LastChild.ChildNodes.title
  66.         $extension=$file.extension
  67.         $filename=("$showname"+"."+"S$(“{0:D2}” -f $seasonnum)"+"E$(“{0:D2}” -f $Episodenum)"+"."+"$Title"+"$extension").replace(" ",".")
  68.         #$filename
  69.         remove-item $metadata\index.xml -Force
  70.         Write-Output "Identified File $filename.  Copying...."
  71.         Copy-Item $file.FullName $destination\$filename
  72.         Write-Output "$filename File Transferred to $destination."
  73.     }
  74.  
  75.     if($type -eq "movie"){
  76.         $title=$xml.LastChild.ChildNodes.title
  77.         $extension=$file.extension
  78.         $filename=$title.Replace(" ",".")+$extension
  79.         #$filename
  80.         remove-item $metadata\index.xml -Force
  81.         Write-Output "Identified File $filename.  Copying...."
  82.         Copy-Item $file.FullName $destination\$filename
  83.         Write-Output "$filename File Transferred to $destination."
  84.     }
  85. }
  86. # pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement