Private/HL_RENAME_SUBTITLE.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Function HL_RENAME_SUBTITLE  
{
 [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [string]$VideoDirectory,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Farsi/Persian','English','Arabic','French')]
        [string]$Language,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Movie','TV-Series')]
        [string]$Type
    )

    Switch ($Language) 
    {
        "Farsi/Persian" {$EXTENSION=".fa.srt"}
        "English" {$EXTENSION=".en.srt"}
        "Arabic" {$EXTENSION=".ar.srt"}
        "French" {$EXTENSION=".fr.srt"}
    }
    Write-Progress -Activity "Renaming Subtitles to vide file name..." 

    $SUB_NAME=""
    
    $VIDEO_FILES= Get-childItem -Path ($VideoDirectory + "\*") -Include *.mkv, *.mp4
    
    $SUB_FILES= Get-childItem -Path $VideoDirectory -Filter *.srt
    If ($Type -eq "TV-Series")
    {
        $RE_EX_EPISODES="((s\d+e\d+|x\d+)|(\d+x\d+))"
        foreach ($VID in $VIDEO_FILES)
        {
            if ($VID.BaseName -Match $RE_EX_EPISODES)
            {
                $match1=$Matches[0]
                foreach ($sub in $SUB_FILES)
                {
                    if ($sub.BaseName -Match $match1)
                    {
                        $SUB_NAME=$VID.BaseName + $EXTENSION
                        Write-Host -ForegroundColor Cyan $sub.name "==>" $SUB_NAME
                        Rename-Item -LiteralPath $sub.fullname -NewName $SUB_NAME -ErrorAction Ignore
                       
                    }
                }
            }
        }
    }
    else
    {
        if ($VIDEO_FILES -and $SUB_FILES)
        {
            Rename-Item -LiteralPath $SUB_FILES[0].FullName -NewName ($VIDEO_FILES[0].BaseName + $EXTENSION) -ErrorAction Ignore
        }
    }
    Write-Progress -Activity "Renaming Subtitles to vide file name..."  -Completed
}