Skip to content

Instantly share code, notes, and snippets.

@Whil-
Last active January 30, 2026 08:22
Show Gist options
  • Select an option

  • Save Whil-/4494a1875f40ad34c836e746c1d117cf to your computer and use it in GitHub Desktop.

Select an option

Save Whil-/4494a1875f40ad34c836e746c1d117cf to your computer and use it in GitHub Desktop.
Script that can copy subtitles from /Subs or /Subtitles folders to parents, so that Plex can work with them. (https://www.reddit.com/r/PleX/comments/fpp2f2/any_way_to_have_plex_use_the_subs_folder_or/)
param(
[Parameter(Mandatory=$true)]
[string]$PathToMediaFolder
)
function Copy-IfLarger {
param(
[Parameter(Position=0, Mandatory=$true)]
[string]$sourceFilePath,
[Parameter(Position=1, Mandatory=$true)]
[string]$targetFilePath
)
$sourceSize = (Get-Item -LiteralPath $sourceFilePath).length
$targetExist = Test-Path -LiteralPath $targetFilePath
$targetSize = if ($targetExist) {(Get-Item -LiteralPath $targetFilePath).length} else {0}
if ($sourceSize -gt $targetSize) {
Write-Host "Copying $sourceFilePath to $targetFilePath"
Copy-Item -LiteralPath $sourceFilePath $targetFilePath
if ($targetExist) { Write-Host "Replaced existing subtitle since new subtitle was larger than existing" }
} else {
Write-Host "Didn't copy $sourceFilePath due to existing subtitle being bigger or if same size"
}
}
function Copy-Subtitles {
param(
[Parameter(Position=0, Mandatory=$true)]
[System.IO.DirectoryInfo]$SourcePath,
[Parameter(Position=1, Mandatory=$true)]
[System.IO.DirectoryInfo]$TargetDir,
[Parameter(Position=2, Mandatory=$true)]
[System.IO.DirectoryInfo]$Name
)
Write-Verbose "Beginning copy of subtitles from $SourcePath"
$files = (Get-ChildItem -LiteralPath $SourcePath -File)
Write-Verbose "Found $($files.length) files"
foreach($file in $files) {
Switch -Regex ($file.name) {
'Swe' {
Write-Host "Found Swedish Subtitle"
$new_name = Join-Path $TargetDir "$($Name.Name).sv$($file.Extension)"
Copy-IfLarger $file.FullName $new_name
}
'Eng' {
Write-Host "Found English Subtitle"
$new_name = Join-Path $TargetDir "$($Name.Name).en$($file.Extension)"
Copy-IfLarger $file.FullName $new_name
}
'Romani' {
Write-Host "Found Romanian Subtitle"
$new_name = Join-Path $TargetDir "$($Name.Name).ro$($file.Extension)"
Copy-IfLarger $file.FullName $new_name
}
}
}
}
if (-not (Test-Path -LiteralPath $PathToMediaFolder)) {
Write-Error "This path does not exist. Please double check it."
exit 4
}
if (Test-Path -LiteralPath "$($PathToMediaFolder)/Subs") {
Write-Verbose "Found Subs folder."
$directory = Join-Path $PathToMediaFolder "/Subs"
} elseif (Test-Path -LiteralPath "$($PathToMediaFolder)/Subtitles") {
Write-Verbose "Found a Subtitles file"
$directory = Join-Path $PathToMediaFolder "/Subtitles"
} else {
Write-Verbose "Found no subtitle folder within $PathToMediaFolder"
exit 0
}
$folders = Get-ChildItem -LiteralPath $directory -Recurse -Directory | Where-Object({$_.GetFiles().Count -ne 0})
Write-Verbose "$($folders.Count) folder(s) have been found."
Write-Verbose "Beginning Copy of Subtitles."
if ($folders.Count -gt 0) {
foreach($subfolder in $folders) {
Copy-Subtitles -SourcePath $subfolder -TargetDir $PathToMediaFolder -Name $subfolder
}
} else {
Copy-Subtitles -SourcePath $directory -TargetDir $PathToMediaFolder -Name $PathToMediaFolder
}
Write-Verbose "Script has been completed & Plex can now read these files."
@mtopolinski
Copy link

cannot get this to work on Windows. When I remove the functions and run the raw code it keeps telling me LiteralPath is null. Any ideas?

@andrewpayne68
Copy link

Example usage, with fd utility to search through a disk/path:

fd Subs --type directory /mnt/media/ -x pwsh ~/path-to/Rename-Subtitles.ps1 {//} -Verbose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment