Advertisement
Guest User

GetPlexSyncedItemsV2.ps1

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