VBA基础
Access字符串函数处理srt字幕时间
2009-04-10 10:57:09

    Dim fso As New FileSystemObject    Dim txt As TextStream    Dim txt2 As TextStream    Dim strT1 As String    Dim strT2 As String    Dim secondsAdd As Long    Dim msecondsAdd As Long    Dim sourceTitleFile As String    Dim targetTitleFile As String            secondsAdd = (1 * 3600 + 14 * 60 + 38) * -1    msecondsAdd = -350    sourceTitleFile = "E:\dvd\11.02.07.Transformers.2007.DiVX6.DTS.2ADUDiO.HD.DVDRiP-CHD\chd-transformers-dts-2audio-hd-dvdrip-disc2.chs.srtold"    targetTitleFile = "E:\dvd\11.02.07.Transformers.2007.DiVX6.DTS.2ADUDiO.HD.DVDRiP-CHD\chd-transformers-dts-2audio-hd-dvdrip-disc2.chs.srt"                Set txt = fso.OpenTextFile(sourceTitleFile, ForReading, False)    Set txt2 = fso.OpenTextFile(targetTitleFile, ForWriting, True)    Dim strLine    Do Until txt.AtEndOfStream = True        strLine = txt.ReadLine        If IsDate(Mid(strLine, 1, 8)) = True And IsDate(Mid(strLine, 18, 8)) = True Then            strT1 = Mid(strLine, 1, 12)            strT2 = Mid(strLine, 18, 12)            strLine = dateAdd_Srt(strT1, secondsAdd, msecondsAdd) & " --> " & dateAdd_Srt(strT2, secondsAdd, msecondsAdd)        End If        txt2.WriteLine strLine    Loop        txt.Close    txt2.Close    Set fso = Nothing        End Function

'只用于调整 *.srt 字幕文件中的时间Function dateAdd_Srt(ByVal timeString As String, ByVal seconds As Long, ByVal milliseconds As Long) As String    Dim dteTime As Date    Dim lngMS As Long    dteTime = CDate(Mid(timeString, 1, 8))    lngMS = CLng(Mid(timeString, 10, 3))        dteTime = DateAdd("s", seconds, dteTime)        If milliseconds > 0 Then        If (lngMS + milliseconds) > 999 Then            lngMS = (lngMS + milliseconds) Mod 1000            dteTime = DateAdd("s", 1, dteTime)        Else            lngMS = lngMS + milliseconds        End If    Else        If (lngMS + milliseconds) < 0 Then            lngMS = (lngMS + milliseconds) Mod 1000            dteTime = DateAdd("s", -1, dteTime)        Else            lngMS = lngMS + milliseconds        End If        lngMS = Abs(lngMS)            End If        dateAdd_Srt = Format(dteTime, "hh:nn:ss") & "," & Format(lngMS, "000")    End Function

字幕文件下载