/
Subtitle.psm1
637 lines (549 loc) · 25.3 KB
/
Subtitle.psm1
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
class Subtitle
{
# public Property
[UInt32]$Index
[TimeSpan]$StartTime
[TimeSpan]$EndTime
[String]$Text
[System.IO.FileInfo]$Path
# Constructor
Subtitle([String]$inputText)
{
$lines = $inputText -split "`r`n"
# Fix for more than 3-4 lines.
[UInt32]$number = 0
if ([UInt32]::TryParse($lines[0], [ref] $number))
{
$this.Index = $number;
}
# Replace , with . ?
$times = $lines[1] -split '\s?-->\s?'
if ($times.Length -eq 2)
{
$tempTimespan = [timespan]::new(0, 0, 0)
[timespan]::TryParse($times[0], [ref] $tempTimespan)
$this.StartTime = $tempTimespan
[timespan]::TryParse($times[1], [ref] $tempTimespan)
$this.EndTime = $tempTimespan
}
else
{
Write-Warning -Message "The subtitle index $($this.Index) with line: $($lines[1]) is invalid."
}
if ($lines.Length -gt 5)
{
throw [System.ArgumentException]::new("Possible subtitle collision. Use split for these lines: $lines")
}
$stringBuilder = [System.Text.StringBuilder]::new(200)
2..($lines.Count - 1) | Foreach-Object { $stringBuilder.AppendLine($lines[$_]) | Out-Null }
$this.Text = $stringBuilder.ToString();
}
# Method
[Subtitle] ChangeDuration([TimeSpan]$increment)
{
$this.StartTime = $this.StartTime + $increment
$this.EndTime = $this.EndTime + $increment
return $this
}
[string] ToString()
{
$stringBuilder = [System.Text.StringBuilder]::new(500)
$stringBuilder.AppendLine($this.Index)
$stringBuilder.AppendLine($this.StartTime.ToString("hh\:mm\:ss\,fff") + ' --> ' + $this.EndTime.ToString("hh\:mm\:ss\,fff"))
$stringBuilder.Append($this.Text) # The line end is already in the Text property.
return $stringBuilder.ToString()
}
}
function Read-Subtitle
{
<#
.SYNOPSIS
Reads subtitles from the input text file.
.DESCRIPTION
The Read-Subtitle cmdlet reads subtitles from the input text file or files. This cmdlet can be piped to the Get-ChildItem cmdlet for a batch processing.
.PARAMETER Path
Specifies a path to a subtitle file. The input can be a string or a valid instance of the System.IO.FileInfo class.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and returns them into console.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path and reads them one by one. The all subtitle from all files are delay by 5 second and saved into their original files.
.NOTES
- This cmdlet uses the OOP approach. The cmdlet use custom made class for the subtitle representation and as the output.
- If you want synchronize multiple subtitles in batch, process files from one directory only.
.LINK
https://github.com/KUTlime/PowerShell-Subtitle-Module
.INPUTS
System.String
System.IO.FileInfo
.OUTPUTS
A collection of the Subtitle class instances stored in array. The collection represents the subtitles read from the input file.
#>
[CmdletBinding(
DefaultParameterSetName = 'Basic',
PositionalBinding = $false,
HelpUri = 'https://github.com/KUTlime/PowerShell-Subtitle-Module'
)]
[Alias('rsub', 'readsub', 'rs', 'Get-Subtitle')]
[OutputType([Subtitle[]])]
Param
(
# A path to subtitle file of folder where the subtitles are located.
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 0,
ParameterSetName = 'Basic')]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
if ( -Not ($_ | Test-Path) )
{
throw "File or folder does not exist"
}
return $true
})]
[System.IO.FileInfo]
$Path
)
Begin
{
}
Process
{
$subtitles = New-Object 'System.Collections.Generic.List[Subtitle]'
if ($Path | Test-Path)
{
$rawLines = (Get-Content -Path $Path.FullName -Raw) -split "`r`n`r`n"
$rawLines | ForEach-Object { if ($_ -ne "") { $subtitles.Add([Subtitle]::new($_)) } }
}
Write-Debug -Message "The number of subtitles: $($subtitles.Count)"
foreach ($subtitle in $subtitles)
{
$subtitle.Path = $Path
}
#$subtitles | ForEach-Object { $_.Path = $Path } -OutVariable 'subtitles'
Write-Debug -Message "The number of subtitles: $($subtitles.Count)"
Write-Output $subtitles
}
End
{
}
}
function Sync-Subtitle
{
<#
.SYNOPSIS
Changes the subtitle timing.
.DESCRIPTION
This cmdlet adjust the subtitle timing based on the input time shift. Use FromTime, FromSubtitleIndex, ToTime, ToSubtitleIndex for a partial synchronization.
You can change timing from, until or between some specific subtitle indexes or subtitle time by combination of these parameters.
.PARAMETER Subtitle
Specifies the subtitle collection to adjust. Any collection which can be iterated is supported.
.PARAMETER TimeShift
Specifies the subtitle time shift in milliseconds. Use a positive, negative value to delay, advance subtitles respectively. The zero time shift causes error.
.PARAMETER FromSubtitleIndex
Specifies the subtitle index from which the subtitles should be adjusted. The default value is 0.
.PARAMETER FromTime
Specifies the subtitle time from which the subtitles should be adjusted. The valid formats: 00:00:00,000 or 00:00:00.000. The default value is 00:00:00.000.
.PARAMETER ToSubtitleIndex
Specifies the subtitle index to which the subtitles should be adjusted. The valid formats: 00:00:00,000 or 00:00:00.000. The default value is the maximal UInt64 value.
.PARAMETER ToTime
Specifies the subtitle time to which the subtitles should be adjusted. The default value is 10675199.02:48:05.4775807.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt | Sync-Subtitle -TimeShift 5000 | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to its original file name.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt | Sync-Subtitle -TimeShift -5000 | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and advance all subtitles by 5 seconds. The result is written to its original file name.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt | Sync-Subtitle -TimeShift 5000 -FromSubtitleIndex 120 | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays subtitles with index 120 and all following subtitles by 5 seconds. The result is written to its original file name.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt | Sync-Subtitle -TimeShift 5000 -FromSubtitleIndex 120 -ToSubtitleIndex 320 | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays subtitles with index 120 to subtitle index 320 by 5 seconds. The subtitle with index 119 and 321 will not be shifted. The result is written to its original file name.
.EXAMPLE
Read-Subtitle -Path C:\MyMovies\SomeMovie\SomeMovie.srt | Sync-Subtitle -TimeShift 5000 -FromSubtitleTime '00:12:13.000' -ToSubtitleTime '01:02:03.000' | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles between time starting on 12 min 13 second inclusively to 1 hour 2 minutes and 3 seconds inclusively by 5 seconds. The subtitle before and after this interval will not be shifted. The result is written to its original file name.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path and reads them one by one. The all subtitle from all files are delay by 5 second and saved into their original files. The result is written to their original files.
.NOTES
- This cmdlet uses the OOP approach. The cmdlet use custom made class for the subtitle representation and as the output.
- If you want synchronize multiple subtitles in batch, process files from one directory only.
- A time shift uses millisecond as unit. 1 second = 1000 milliseconds. Use -TimeShift 5000 for 5 second delay or -TimeShift 5000 for 5 second advance of the subtitles.
.LINK
https://github.com/KUTlime/PowerShell-Subtitle-Module
.INPUTS
Subtitle
Subtitle[]
.OUTPUTS
An adjusted collection of the Subtitle class instances stored in array. The collection represents the subtitles read from the input file.
#>
[CmdletBinding(
DefaultParameterSetName = 'Basic',
PositionalBinding = $false,
HelpUri = 'https://github.com/KUTlime/PowerShell-Subtitle-Module'
)]
[Alias('ssub', 'syncsub', 'ss')]
[OutputType([Subtitle[]])]
param
(
# A collection of subtitles to synchronize.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $true,
ValueFromRemainingArguments = $false,
Position = 0)]
[ValidateNotNull()]
[ValidateScript( {
if ( ($_.Count -eq 0) )
{
throw "The subtitle collection is empty"
}
return $true
})]
[Object[]]
$Subtitle,
# A number of millisecond for subtitle time shift.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 1)]
[ValidateNotNull()]
[ValidateScript( {
if ( ($_.Count -eq 0) )
{
throw "The timeshift is zero."
}
return $true
})]
[Int64]
$TimeShift,
# A positive number which identifies the subtitle index from the synchronization should be processed.
[Parameter(
Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 2)]
[ValidateNotNull()]
[UInt64]
$FromSubtitleIndex = 0,
# A positive time from the subtitle should be processed. The valid formats: 00:00:00,000 or 00:00:00.000.
[Parameter(
Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 3)]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
$validTimeSpan = [TimeSpan]::new(0, 0, 0)
if ( ([TimeSpan]::TryParse($_, [ref] $validTimeSpan) -eq $false) )
{
throw "Validation of the FromTime parameter failed. Try use format 00:00:00,000 or 00:00:00.000 with `".`" as delimiter at the end."
}
return $true
})]
[String]
$FromTime = ([TimeSpan]::new(0, 0, 0)).ToString("hh\:mm\:ss\.fff"),
# A positive number which identifies the subtitle index from the synchronization should be processed.
[Parameter(
Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 4)]
[ValidateNotNull()]
[UInt64]
$ToSubtitleIndex = [UInt64]::MaxValue,
# A positive time until to the subtitle should be processed. The valid formats: 00:00:00,000 or 00:00:00.000.
[Parameter(
Mandatory = $false,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
Position = 5)]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
$validTimeSpan = [TimeSpan]::new(0, 0, 0)
if ( ([TimeSpan]::TryParse($_, [ref] $validTimeSpan) -eq $false) )
{
throw "Validation of the FromTime parameter failed. Try use format 00:00:00,000 or 00:00:00.000 with `".`" as delimiter at the end."
}
return $true
})]
[String]
$ToTime = ([TimeSpan]::MaxValue.ToString("hh\:mm\:ss\.fff"))
)
Begin
{
$timeSpan = [TimeSpan]::FromMilliseconds($TimeShift)
$fromTimeSpan = [TimeSpan]::new(0, 0, 0)
$toTimeSpan = [TimeSpan]::new(0, 0, 0)
if ( ([TimeSpan]::TryParse($FromTime, [ref] $fromTimeSpan) -eq $false) )
{
throw "Parsing of the FromTime parameter failed. Try use format 00:00:00,000 or 00:00:00.000 with `".`" as delimiter at the end."
}
if ( ([TimeSpan]::TryParse($ToTime, [ref] $toTimeSpan) -eq $false) )
{
throw "Parsing of the FromTime parameter failed. Try use format 00:00:00,000 or 00:00:00.000 with `".`" as delimiter at the end."
}
}
Process
{
$subtitlesShifted = New-Object 'System.Collections.Generic.List[Subtitle]'
Write-Debug -Message "The temporary object start time: $($_.StartTime)"
Write-Verbose -Message "Processing subtitle: $($_.Index)"
$Subtitle | ForEach-Object `
{
if (($_.Index -ge $FromSubtitleIndex -and $_.Index -le $ToSubtitleIndex) -or ($_.StartTime -ge $fromTimeSpan -and $_.StartTime -le $toTimeSpan))
{
$subtitlesShifted.Add($Subtitle.ChangeDuration($timeSpan))
}
}
Write-Debug -Message "The temporary object start time: $($_.StartTime)"
Write-Output $subtitlesShifted
}
End
{
Write-Verbose -Message "The pipeline processed."
}
}
function Write-Subtitle
{
<#
.SYNOPSIS
Writes a subtitle or subtitles to a file.
.DESCRIPTION
This cmdlet writes a collection or a single subtitle file to the specified or the original file. The original file overwritten if no destination file is specified or if the specified path is same as original. If destination suffix is specified, a new subtitle file in original location and original name with suffix is created.
.PARAMETER Subtitle
Specifies the subtitle collection to adjust. Any collection which can be iterated is supported.
.PARAMETER Path
Specifies a destination path where the subtitle file with the original or a new name will be written.
.PARAMETER NewFile
Specifies a new file name for synchronized subtitles. The '*.srt' file extension is added automatically. Don't use this parameter with batch processing. All subtitles will be written into this, single file.
.PARAMETER Suffix
Specifies a desired destination file suffix name. Use this parameter with batch processing. All subtitles files names will be modified with this suffix.
.PARAMETER DestinationFile
Specifies a desired destination file name. The '*.srt' file extension is added automatically. Don't use this parameter with batch processing. All subtitles will be written into this, single file.
.PARAMETER DestinationFileSuffix
Specifies a desired destination file suffix name. Use this parameter with batch processing. All subtitles files names will be modified with this suffix.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to its original file in the original path.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Path C:\Temp
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to its original file in the new path C:\Temp.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -NewFile 'SomeMovieDVDVersion'
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to the new file 'SomeMovieDVDVersion.srt' in the original path.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Suffix '-resync'
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to the original file name modified by '-resync' suffix in the original path.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Path C:\Temp -DestinationFile 'SomeMovieDVDVersion'
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to the new file 'SomeMovieDVDVersion.srt' in the new path C:\Temp.
.EXAMPLE
Read-Subtitle -Path 'C:\MyMovies\SomeMovie\SomeMovie.srt' | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Path C:\Temp -DestinationFileSuffix '-resync'
Reads subtitles from the file SomeMovie.srt located in C:\MyMovies\SomeMovie\ path and delays all subtitles by 5 seconds. The result is written to the original file name modified by '-resync' suffix in the new path C:\Temp.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path one by one. All subtitles are delayed by 5 seconds. The result is written to its original file in the original path.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Path C:\Temp
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path one by one. All subtitles are delayed by 5 seconds. The result is written to its original file name in the new path C:\Temp.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Suffix '-resync'
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path one by one. All subtitles are delayed by 5 seconds. The result is written to the original file name modified by '-resync' suffix in the original path.
.EXAMPLE
Get-ChildItem -Path C:\MyMovies\SomeTVShow\SeasonOne -Filter '*.srt' | Read-Subtitle | Sync-Subtitle -TimeShift 5000 | Write-Subtitle -Path C:\Temp -DestinationFileSuffix '-resync'
Reads all srt files located in C:\MyMovies\SomeTVShow\SeasonOne path one by one. All subtitles are delayed by 5 seconds. The result is written to the original file name modified by '-resync' suffix in the new path C:\Temp.
.NOTES
- This cmdlet uses the OOP approach. The cmdlet use custom made class for the subtitle representation and as the output.
- If you want synchronize multiple subtitles in batch, process files from one directory only.
- A destination file name or a original file name suffix is supported as the output file name variations.
.LINK
https://github.com/KUTlime/PowerShell-Subtitle-Module
.INPUTS
Subtitle
Subtitle[]
.OUTPUTS
This cmdlet produces no output to the output stream, only creates/change files on the file storage.
#>
[CmdletBinding(
PositionalBinding = $false,
HelpUri = 'https://github.com/KUTlime/PowerShell-Subtitle-Module'
)]
[Alias('wsub', 'writesub', 'ws', 'Set-Subtitle')]
[OutputType([void])]
param
(
# A collection of subtitles to synchronize.
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitleOnly',
Position = 0)]
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePath',
Position = 0)]
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitleNewName',
Position = 0)]
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitleSuffix',
Position = 0)]
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePathDestinationFile',
Position = 0)]
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePathDestinationNameSuffix',
Position = 0)]
[ValidateNotNull()]
[ValidateScript( {
if ( ($_.Count -eq 0) )
{
throw "The subtitle collection is empty"
}
return $true
})]
[Object[]]
$Subtitle,
# A path to subtitle file of folder where the subtitles are located.
[Parameter(Mandatory = $true,
ValueFromPipeline = $false,
ValueFromPipelineByPropertyName = $false,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePath',
Position = 1)]
[Parameter(
ParameterSetName = 'SubtitlePathDestinationFile',
Mandatory = $true
)]
[Parameter(
ParameterSetName = 'SubtitlePathDestinationNameSuffix',
Mandatory = $true
)]
[ValidateNotNullOrEmpty()]
[ValidateScript( {
if ( -Not ($_ | Test-Path) )
{
throw "File or folder does not exist"
}
return $true
})]
[System.IO.FileInfo]
$Path,
# A name of the new file name.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitleNewName',
Position = 2)]
[ValidateNotNullOrEmpty()]
[String]
$NewFile,
# A suffix for the destination file. Use this for batch processing. The original file name will be used with suffix.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitleSuffix',
Position = 3)]
[ValidateNotNullOrEmpty()]
[String]
$Suffix,
# A name of destination file to avoid binding problems when pipeline is used.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePathDestinationFile',
Position = 4)]
[ValidateNotNullOrEmpty()]
[String]
$DestinationFile,
# A name of destination file suffix to avoid binding problems when pipeline is used.
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false,
ParameterSetName = 'SubtitlePathDestinationNameSuffix',
Position = 5)]
[ValidateNotNullOrEmpty()]
[String]
$DestinationFileSuffix
)
Begin
{
}
Process
{
switch ($PsCmdlet.ParameterSetName)
{
"SubtitleOnly"
{
$filePath = $Subtitle[0].Path.Directory.FullName + "\" + $Subtitle[0].Path.Name
}
"SubtitlePath"
{
$filePath = $Path.FullName + "\" + $Subtitle[0].Path.Name
}
"SubtitleNewName"
{
$filePath = $Subtitle[0].Path.Directory.FullName + "\" + $NewFile + '.srt'
}
"SubtitleSuffix"
{
$filePath = $Subtitle[0].Path.Directory.FullName + "\" + [IO.Path]::GetFileNameWithoutExtension($Subtitle[0].Path) + $Suffix + '.srt'
}
"SubtitlePathDestinationFile"
{
$filePath = $Path.FullName + "\" + $DestinationFile + '.srt'
}
"SubtitlePathDestinationNameSuffix"
{
$filePath = $Path.FullName + "\" + [IO.Path]::GetFileNameWithoutExtension($Subtitle[0].Path) + $DestinationFileSuffix + '.srt'
}
}
$stringBuilder = [System.Text.StringBuilder]::new(200000)
$Subtitle | ForEach-Object { $stringBuilder.Append($_.ToString()) | Out-Null }
$stringBuilder.ToString() | Out-File -FilePath ($filePath + '_') -Encoding:unicode -Append -Force
}
End
{
Write-Verbose -Message "The pipeline processed."
Write-Verbose -Message $filePath
Get-ChildItem -Path ([IO.Path]::GetDirectoryName($filePath)) -Filter '*.srt_' |
ForEach-Object `
{
$NewName = $_.Name -replace '.srt_', '.srt'
$Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
Move-Item -Path $_.FullName -Destination $Destination -Force
}
}
}