- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- pause
- exit /b
- #>
- # 功能:当前目录txt转换成srt的格式,我知道缺少每句话的结束时间,结束时间就用下一句的开始时间就行:
-
- function GenSrt {
- param(
- [string]$Source,
- [string]$Output
- )
- $re = New-Object System.Text.RegularExpressions.Regex -ArgumentList @('^[^*\n]+\*+\s*(?<begin1>\d{2}:\d{2}:\d{2})\.(?<begin2>\d{3}).*$\n(?<talk>[\s\S]*?)(?=^[^*\n]+\*+\s*(?<end1>\d{2}:\d{2}:\d{2})\.(?<end2>\d{3})|\z)', 'Multiline,Compiled')
- $evaluator = [System.Text.RegularExpressions.MatchEvaluator] {
- param([System.Text.RegularExpressions.Match]$m)
- $Script:ctr++
- # Write-Host ($m.Groups['talk'].Value) -ForegroundColor Green
- if ($m.Groups['end1'].Success) {
- $Script:format -f $Script:ctr, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['end1'].Value, $m.Groups['end2'].Value, $m.Groups['talk'].Value
- } else {
- $Script:format -f $Script:ctr, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['talk'].Value
- }
- }
- $Script:ctr = 0
- [System.IO.File]::WriteAllText($Output, ($re.Replace( [System.IO.File]::ReadAllText($Source, $encoding), $evaluator )), $encoding)
- }
- $encoding = [System.Text.Encoding]::UTF8
- $Script:format = "{0}`r`n{1},{2} --> {3},{4}`r`n{5}`r`n"
- Get-ChildItem -Path "*.txt" | ForEach-Object {
- if (-not $_.PSIsContainer) {
- $dstfile = $_.BaseName + ".srt"
- Write-Host "$($_.Name) --> $dstfile"
- GenSrt -Source $_ -Output $dstfile
- }
- }
复制代码
|