Skip to content

Commit

Permalink
Removed Subliminal and added native OpenSubtitles downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Rouzax committed Feb 27, 2024
1 parent 1b90985 commit 99e3e33
Show file tree
Hide file tree
Showing 11 changed files with 523 additions and 228 deletions.
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,20 @@ Only 1 instance of the script will be running and if other downloads complete du

Script will do the following:
1. Unpack or copy to Temporary Processing folder defined in config.json (copy from config-sample.json)
2. If torrent label is TV show as defined in JSON Label
- Start [Subliminal](https://github.com/Diaoul/subliminal) to see if there are missing subtitles we can download
- Will strip all srt subtitle languages not in JSON WantedLanguages
- Extract all SRT subtitles in JSON WantedLanguages
- Rename all srt files from alpha3 to alpha2 codes as defined in JSON LanguageCodes
- File.3.en.srt
- File.en.srt
- File.nl.srt
2. If torrent label is TV show as defined in config.json Label
- Will strip all srt subtitle languages not in config.json WantedLanguages
- Extract all SRT subtitles in config.json WantedLanguages
- Will try to download missing subs from OpenSubtitles.com
- Renames all srt files from ISO 639-2 (3 letter codes) to ISO 639-1 (2 letter codes) as defined in LanguageCodes.json
- Clean up the srt subtitles using [Subtitle Edit](https://github.com/SubtitleEdit/subtitleedit)
- Remove Hearing Impaired.
- Fix Common errors.
- Start Medusa Import
3. If torrent label is Movie as defined in JSON Label
- Start [Subliminal](https://github.com/Diaoul/subliminal) to see if there are missing subtitles we can download
- Will strip all srt subtitle languages not in JSON WantedLanguages
- Extract all SRT subtitles in JSON WantedLanguages
- Rename all srt files from alpha3 to alpha2 codes as defined in JSON LanguageCodes
- File.3.en.srt
- File.en.srt
- File.nl.srt
3. If torrent label is Movie as defined in config.json Label
- Will strip all srt subtitle languages not in config.json WantedLanguages
- Extract all SRT subtitles in config.json WantedLanguages
- Will try to download missing subs from OpenSubtitles.com
- Renames all srt files from ISO 639-2 (3 letter codes) to ISO 639-1 (2 letter codes) as defined in LanguageCodes.json
- Clean up the srt subtitles using [Subtitle Edit](https://github.com/SubtitleEdit/subtitleedit)
- Remove Hearing Impaired.
- Fix Common errors.
Expand Down Expand Up @@ -69,5 +63,4 @@ The following external tools need to be available and the path defined in the `c
- [WinRar](https://www.rarlab.com/download.htm)
- [MKVMerge](https://mkvtoolnix.download/)
- [MKVExtract](https://mkvtoolnix.download/)
- [Subtitle Edit](https://github.com/SubtitleEdit/subtitleedit)
- [Subliminal](https://github.com/Diaoul/subliminal)
- [Subtitle Edit](https://github.com/SubtitleEdit/subtitleedit)
114 changes: 73 additions & 41 deletions TorrentScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ param(
[switch]$NoCleanUp
)

# Get function definition files.
$Functions = @( Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -ErrorAction SilentlyContinue )
# Dot source the files
ForEach ($import in @($Functions)) {
Try {
# dotsourcing a function script
.$import.FullName
} Catch {
Write-Error -Message "Failed to import function $($import.FullName): $_"
}
}

# User Variables
try {
$configPath = Join-Path $PSScriptRoot 'config.json'
$Config = Get-Content $configPath -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
$Config = Get-Content $configPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
} catch {
Write-Host 'Exception:' $_.Exception.Message -ForegroundColor Red
Write-Host 'Invalid config.json file' -ForegroundColor Red
Expand All @@ -51,13 +63,30 @@ try {
# Reading Language Code lookup
try {
$LanguageCodesPath = Join-Path $PSScriptRoot 'LanguageCodes.json'
$LanguageCodes = Get-Content $LanguageCodesPath -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
$LanguageCodes = Get-Content $LanguageCodesPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
# Create a hashtable for language code lookup
$LanguageCodeLookup = @{}
foreach ($langCodeKey in $LanguageCodes.PSObject.Properties.Name) {
$langCode = $LanguageCodes.$langCodeKey
$639_2 = $langCode.'639-2'
$639_2_B = $langCode.'639-2/B'
$639_1 = $langCode.'639-1'

if ($639_2) {
$LanguageCodeLookup[$639_2] = $639_1
}

if ($639_2_B) {
$LanguageCodeLookup[$639_2_B] = $639_1
}
}
} catch {
Write-Host 'Exception:' $_.Exception.Message -ForegroundColor Red
Write-Host 'Invalid LanguageCodes.json' -ForegroundColor Red
exit 1
}


# Log Date format
$LogFileDateFormat = Get-Date -Format $Config.DateFormat

Expand All @@ -80,8 +109,6 @@ $WinRarPath = $Config.Tools.WinRarPath
$MKVMergePath = $Config.Tools.MKVMergePath
$MKVExtractPath = $Config.Tools.MKVExtractPath
$SubtitleEditPath = $Config.Tools.SubtitleEditPath
$SubliminalPath = $Config.Tools.SubliminalPath
$MailSendPath = $Config.Tools.MailSendPath

# Import qBittorrent Settings
$qBittorrentHost = $Config.qBittorrent.Host
Expand Down Expand Up @@ -112,29 +139,21 @@ $SMTPport = $Config.Mail.SMTPport
$SMTPuser = $Config.Mail.SMTPuser
$SMTPpass = $Config.Mail.SMTPpass

# OpenSubtitle User and Pass for Subliminal
# OpenSubtitle User and Pass for OpenSubtitle.com
$OpenSubUser = $Config.OpenSub.User
$OpenSubPass = $Config.OpenSub.Password
$omdbAPI = $Config.OpenSub.omdbAPI
$OpenSubAPI = $Config.OpenSub.API
$OpenSubHearing_impaired = $Config.OpenSub.hearing_impaired
$OpenSubForeign_parts_only = $Config.OpenSub.foreign_parts_only
$OpenSubMachine_translated = $Config.OpenSub.machine_translated
$OpenSubAI_translated = $Config.OpenSub.ai_translated

# Language codes of subtitles to keep
$WantedLanguages = $Config.WantedLanguages
$SubtitleNamesToRemove = $Config.SubtitleNamesToRemove

# Get function definition files.
$Functions = @( Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 -ErrorAction SilentlyContinue )
# Dot source the files
ForEach ($import in @($Functions)) {
Try {
# dotsourcing a function script
.$import.FullName
} Catch {
Write-Error -Message "Failed to import function $($import.FullName): $_"
}
}

# Test additional programs
$Tools = @($WinRarPath, $MKVMergePath, $MKVExtractPath, $SubtitleEditPath, $SubliminalPath, $MailSendPath)
$Tools = @($WinRarPath, $MKVMergePath, $MKVExtractPath, $SubtitleEditPath)
foreach ($Tool in $Tools) {
Test-Variable-Path -Path $Tool
}
Expand All @@ -150,6 +169,7 @@ Write-Host 'All checks done' -ForegroundColor DarkYellow
#* Start of script
Clear-Host
$ScriptTitle = "Torrent Script"

try {
Write-Ascii $ScriptTitle -ForegroundColor DarkYellow
} catch {
Expand Down Expand Up @@ -284,8 +304,8 @@ if ($RarFile) {
} elseif (-not $RarFile -and $SingleFile) {
Write-HTMLLog -Column1 '*** Single File ***' -Header
$FileCopyParams = @{
source = DownloadRootPath
Destination = ProcessPathFull
source = $DownloadRootPath
Destination = $ProcessPathFull
File = $DownloadName
DownloadLabel = $DownloadLabel
DownloadName = $DownloadName
Expand All @@ -295,8 +315,8 @@ if ($RarFile) {
} elseif (-not $RarFile -and $Folder) {
Write-HTMLLog -Column1 '*** Folder ***' -Header
$FileCopyParams = @{
source = DownloadRootPath
Destination = ProcessPathFull
source = $DownloadPath
Destination = $ProcessPathFull
File = '*.*'
DownloadLabel = $DownloadLabel
DownloadName = $DownloadName
Expand All @@ -318,31 +338,42 @@ if ($DownloadLabel -eq $TVLabel -or $DownloadLabel -eq $MovieLabel) {
} else {
$VideoContainer = $false
}

if ($VideoContainer) {
# Download any missing subs with Subliminal
$SubliminalParams = @{
Source = $ProcessPathFull
OpenSubUser = $OpenSubUser
OpenSubPass = $OpenSubPass
omdbAPI = $omdbAPI
WantedLanguages = $WantedLanguages
SubliminalPath = $SubliminalPath
DownloadLabel = $DownloadLabel
DownloadName = $DownloadName
}
Start-Subliminal @SubliminalParams

# Remove unwanted subtitle languages and extract wanted subtitles and rename
$MKVToolnixImportParams = @{
Source = $ProcessPathFull
MKVMergePath = $MKVMergePath
MKVExtractPath = $MKVExtractPath
WantedLanguages = $WantedLanguages
SubtitleNamesToRemove = $SubtitleNamesToRemove
LanguageCodes = $LanguageCodes
LanguageCodeLookup = $LanguageCodeLookup
}
Start-MKVSubtitleStrip @MKVToolnixImportParams


# Download missing subtitles using OpenSubtitles.com API
$OpenSubLanguages = @()
foreach ($language in $WantedLanguages) {
$OpenSubLanguages += $LanguageCodeLookup[$language]
}
$OpenSubParams = @{
Source = $ProcessPathFull
OpenSubUser = $OpenSubUser
OpenSubPass = $OpenSubPass
OpenSubAPI = $OpenSubAPI
OpenSubHearing_impaired = $OpenSubHearing_impaired
OpenSubForeign_parts_only = $OpenSubForeign_parts_only
OpenSubMachine_translated = $OpenSubMachine_translated
OpenSubAI_translated = $OpenSubAI_translated
WantedLanguages = $OpenSubLanguages
}
if ($DownloadLabel -eq $TVLabel) {
$OpenSubParams.Add("Type", "episode")
} elseif ($DownloadLabel -eq $MovieLabel) {
$OpenSubParams.Add("Type", "movie")
}
Start-OpenSubtitlesDownload @OpenSubParams

# Clean up Subs
$SubtitleEditParams = @{
Source = $ProcessPathFull
Expand All @@ -355,13 +386,13 @@ if ($DownloadLabel -eq $TVLabel -or $DownloadLabel -eq $MovieLabel) {

Write-HTMLLog -Column1 '*** MKV Files ***' -Header
foreach ($Mkv in $mkvFiles) {
Write-HTMLLog -Column1 ' ' -Column2 $Mkv.name
Write-HTMLLog -Column2 $Mkv.name
}
$SrtFiles = Get-ChildItem -LiteralPath $ProcessPathFull -Recurse -Filter '*.srt'
if ($SrtFiles.Count -gt 0) {
Write-HTMLLog -Column1 '*** Subtitle Files ***' -Header
foreach ($Srt in $SrtFiles) {
Write-HTMLLog -Column1 ' ' -Column2 $srt.name
Write-HTMLLog -Column2 $srt.name
}
}
} else {
Expand Down Expand Up @@ -415,7 +446,8 @@ if ($DownloadLabel -eq $TVLabel -or $DownloadLabel -eq $MovieLabel) {

# Cleanup the Process Path folders
if ($NoCleanUp) {
Write-HTMLLog -Column1 'Cleanup' -Column2 'NoCleanUp switch was given at command line, leaving files'
Write-HTMLLog -Column1 'Cleanup' -Column2 'NoCleanUp switch was given at command line, leaving files:'
Write-HTMLLog -Column2 "$ProcessPathFull"
} else {
try {
If (Test-Path -LiteralPath $ProcessPathFull) {
Expand Down
19 changes: 6 additions & 13 deletions config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"WinRarPath": "C:\\Program Files\\WinRAR\\rar.exe",
"MKVMergePath": "C:\\Program Files\\MKVToolNix\\mkvmerge.exe",
"MKVExtractPath": "C:\\Program Files\\MKVToolNix\\mkvextract.exe",
"SubtitleEditPath": "C:\\GitHub\\SubtitleEdit\\SubtitleEdit.exe",
"SubliminalPath": "C:\\GitHub\\subliminal\\Scripts\\subliminal.exe"
"SubtitleEditPath": "C:\\GitHub\\SubtitleEdit\\SubtitleEdit.exe"
},
"Mail": {
"To": "[email protected]",
Expand All @@ -26,7 +25,11 @@
"OpenSub": {
"User": "user",
"Password": "Password123",
"omdbAPI": "2028C39D"
"API": "2028C39D",
"hearing_impaired" : "exclude",
"foreign_parts_only" : "exclude",
"machine_translated" : "exclude",
"ai_translated" : "include"
},
"WantedLanguages": [
"eng",
Expand All @@ -35,16 +38,6 @@
"SubtitleNamesToRemove": [
"Forced"
],
"LanguageCodes": [
{
"alpha3": "eng",
"alpha2": "en"
},
{
"alpha3": "dut",
"alpha2": "nl"
}
],
"qBittorrent": {
"Host": "http://localhost",
"Port": "8080",
Expand Down
4 changes: 3 additions & 1 deletion functions/Get-Input.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ function Get-Input {

# Loop until a valid input is provided (if Required switch is used)
while ($Required -and ($null -eq $UserInput -or $UserInput -eq '')) {
$UserInput = Read-Host -Prompt "Required | $Message"
Write-Host "Required | " -ForegroundColor DarkRed -NoNewline
$UserInput = Read-Host -Prompt $Message
$UserInput = $UserInput.Trim()
}

# If Required switch is not used, get input without validation
if (-not $Required) {
Write-Host "Optional | " -ForegroundColor DarkCyan -NoNewline
$UserInput = Read-Host -Prompt "$Message"
$UserInput = $UserInput.Trim()
}
Expand Down
2 changes: 1 addition & 1 deletion functions/Import-Radarr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Import-Radarr {
[Parameter(Mandatory = $true)]
[int]$RadarrTimeOutMinutes,

[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $false)]
[string]$TorrentHash,

[Parameter(Mandatory = $true)]
Expand Down
24 changes: 5 additions & 19 deletions functions/Start-MKVSubtitleStrip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Specifies an array of subtitle names to be removed.
Subtitles with matching names will be excluded from the extraction.
Example: @("Forced")
.PARAMETER LanguageCodes
Specifies an array of language code mappings.
.PARAMETER LanguageCodeLookup
Specifies an Hashtable of language code mappings.
Each item in the array should be a hashtable with 'alpha3' and 'alpha2' keys
representing the three-letter and two-letter language codes, respectively.
Example: @(@{alpha3="eng"; alpha2="en"}, @{alpha3="dut"; alpha2="nl"})
Expand Down Expand Up @@ -51,7 +51,7 @@ function Start-MKVSubtitleStrip {
[array]$SubtitleNamesToRemove,

[Parameter(Mandatory = $true)]
[Object]$LanguageCodes
[hashtable]$LanguageCodeLookup
)

# Make sure needed functions are available otherwise try to load them.
Expand Down Expand Up @@ -233,16 +233,6 @@ function Start-MKVSubtitleStrip {

# Rename extracted subs to correct 2 county code based on $LanguageCodes
if ($SubsExtracted) {
# Create a hashtable for efficient lookup
$LanguageCodeLookup = @{}
foreach ($code in $LanguageCodes.Values) {
$LanguageCodeLookup[$code['639-2']] = $code['639-1']

if ($code.ContainsKey('639-2/B')) {
$LanguageCodeLookup[$code['639-2/B']] = $code['639-1']
}
}

$SrtFiles = Get-ChildItem -LiteralPath $Source -Recurse -Filter '*.srt'

foreach ($srt in $SrtFiles) {
Expand All @@ -256,13 +246,10 @@ function Start-MKVSubtitleStrip {
# Check if the language code exists in the lookup table
if ($LanguageCodeLookup.ContainsKey($languageCode)) {
$SrtNameNew = $SrtName -replace "\.$languageCode\.srt$", ".$($LanguageCodeLookup[$languageCode]).srt"
if ($ChangedLanguages -notcontains $LanguageCodes.$($LanguageCodeLookup[$languageCode]).Name) {
$ChangedLanguages += $LanguageCodes.$($LanguageCodeLookup[$languageCode]).Name
}
$Destination = Join-Path -Path $SrtDirectory -ChildPath $SrtNameNew
Move-Item -LiteralPath $SrtPath -Destination $Destination -Force
} else {
Write-Host "Language code '$languageCode' not found in the lookup table."
Write-HTMLLog -Column2 "Language code $languageCode not found in the 3 letter lookup table." -ColorBg 'Error'
}
}
if ($TotalSubsToExtract -gt 0) {
Expand All @@ -275,5 +262,4 @@ function Start-MKVSubtitleStrip {
} else {
Write-HTMLLog -Column1 'Result:' -Column2 'No SRT subs found in MKV'
}
}
Start-MKVSubtitleStrip -Source "D:\Temp\Torrent\PROCD\TV\Resident.Alien.S02E16.1080p.WEB.h264-GOSSIP" -MKVMergePath "C:\Program Files\MKVToolNix\mkvmerge.exe" -MKVExtractPath "C:\Program Files\MKVToolNix\mkvextract.exe" -WantedLanguages @("eng", "dut") -SubtitleNamesToRemove @("Forced") -LanguageCodes @{"en"=@{"639-1"="en";"639-2"="eng";"name"="English"}; "nl"=@{"639-1"="nl";"639-2"="nld";"639-2/B"="dut";"name"="Dutch"}}
}
Loading

0 comments on commit 99e3e33

Please sign in to comment.