assaddIntervalcaption.ps1


<#PSScriptInfo
 
.VERSION 0.3
 
.GUID 25a2e39f-df6a-4591-9e13-1ba9acdbf35f
 
.AUTHOR he852100
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS windows,ass,sub
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>



<#
 
.DESCRIPTION
 ass 在字幕间隔中添加添加自定义字幕
 
#>
 

<#
.SYNOPSIS
    ass 在字幕间隔中添加添加自定义字幕
.DESCRIPTION
    一粒尘:前辈们,有没有办法,让字幕出现以外的时间里,都显示我的一句口号?
.EXAMPLE
    PS C:\> add-Intervalcaption -path xxx.ass -text 'aaa'
    add-Intervalcaption -path [文件路径] -text [字幕]
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    General notes
#>

# Author: he852100
# Origianl date: 2021
#Requires -Version 3
function add-Intervalcaption {
    [CmdletBinding()]
    param (
        # Specifies a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is
        # used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters,
        # enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any
        # characters as escape sequences.
        [Parameter(Mandatory = $true,
            Position = 0,
            ParameterSetName = "LiteralPath",
            ValueFromPipelineByPropertyName = $false,
            HelpMessage = "ass文件路径")]
        [ValidateNotNullOrEmpty()]
        [string]
        $Path,
        [Parameter(Mandatory = $true,
            Position = 1,
            ParameterSetName = "LiteralPath",
            ValueFromPipelineByPropertyName = $false,
            HelpMessage = "需要插入的文本")]
        [string]
        $text,
        [Parameter(Mandatory = $false,
            Position = 2,
            ParameterSetName = "LiteralPath",
            ValueFromPipelineByPropertyName = $false,
            HelpMessage = "ass文件路径")]
        [int]
        $mintime = 5,
        [Parameter(Mandatory = $false,
            Position = 3,
            ParameterSetName = "LiteralPath",
            ValueFromPipelineByPropertyName = $false,
            HelpMessage = "ass文件路径")]
        [int]
        $interval = 1000,
        [Parameter(Mandatory = $false,
        Position = 4,
        ParameterSetName = "LiteralPath",
        ValueFromPipelineByPropertyName = $false,
        HelpMessage = "需要插入的文本")]
    [string]
    $font
    )
    #间隔激活秒数

    if (Test-Path -Path $path) {
        if ($path -match '\.ass$') {
            $output = $path -replace '\.ass$'
        }
        else {
            Write-Error '路径错误'
            break
        }

    }
    if($font){
        $f=$font
    }else{
        $f='Default'
    }
    $gc = Get-Content -path "$path"
    $list = $gc -match '(?<start>\d:\d{2}:\d{2}\.\d{2}),(?<end>\d:\d{2}:\d{2}\.\d{2})'
    $all = $gc | select-string -pattern '(?<start>\d:\d{2}:\d{2}\.\d{2}),(?<end>\d:\d{2}:\d{2}\.\d{2})' -AllMatches
    $alls = $all.Matches

    $arraylist = @()
    $start = ($alls.Groups | Where-Object { $_.name -like 'start' }).Value
    $end = ($alls.Groups | Where-Object { $_.name -like 'end' }).Value
    $arraylist += for (($i = 0), ($j = 1); $i -lt $start.Count; ($i++), ($j++)) {
        if($j -lt $start.Count){
        $end1 = [datetime]$end[$i]
        $start1 = [datetime]$start[$j]
        }
        $time = New-TimeSpan -Start $end1 -End $start1

        $ling = [datetime]'0:00:00.00'
        if ($i -eq 0 -and (($start1 - $ling) -gt $mintime)) {
            $a = $end1.AddMilliseconds(-$interval).ToString( 'H:mm:ss.ff') 
            "Dialogue: 0,${ling},${a},$f,NTP,0,0,0,,$text"

        }
        $list[$i]

        $chongfu = $start[$j..$start.Count] | ForEach-Object { [datetime]$_ -lt [datetime]$end[$i] } 
        if ($time.TotalSeconds -gt $mintime -and ($chongfu -contains $false)) {

            $jiastart = $end1.AddMilliseconds($interval).ToString( 'H:mm:ss.ff') 
            $jiaend = $start1.AddMilliseconds(-$interval).ToString( 'H:mm:ss.ff') 

            "Dialogue: 0,${jiastart},${jiaend},$f,NTP,0,0,0,,$text"
        }
    }

    $zhong = ([datetime]$end[-1]).AddMilliseconds(+$interval).ToString( 'H:mm:ss.ff') 
    $arraylist += "Dialogue: 0,${zhong},5:26:45.86,$f,NTP,0,0,0,,$text"
    ($gc -join "`r`n").replace(($list -join "`r`n"), ($arraylist -join "`r`n")) | set-content -path "$output.out.ass"  
}