如何為文件中的所有「hh:mm」字串新增固定的時間量?

如何為文件中的所有「hh:mm」字串新增固定的時間量?

我有一份文件,是視訊訪談的轉錄。影片本身嵌入了 SMPTE 時間碼以及預燒時間碼 (BITC)。 SMPTE 時間碼和 BITC 對應一天中採訪拍攝的時間,採用 24 小時格式。

在轉錄文件中,方括號中每兩分鐘有一個[hh:mm]格式的時間戳記。但是,這些轉錄時間戳從「00:00」開始。

有沒有辦法使用一種「搜尋和取代」功能來瀏覽文檔,尋找所有實例並向hh:mm這些時間戳記添加固定時間,從而反映一天中的時間,與 SMPTE 時間碼相符?

完美的解決方案是一個具有以下功能的工具:

  1. 尋找所有[hh:mm]格式的時間戳
  2. 將 24 小時格式的訪談開始時間「yy:yy」加入所有這些原始時間戳記「xx:xx」。
  3. 將時間戳記替換為新的、更正的時間戳記“zz:zz”,該時間戳等於 xx:xx+yy:yy。

例如,如果我的訪談在早上 9:30 開始,則每個時間戳將按以下方式替換:

  1. 00:00 + 9:30 = 9:30
  2. 00:02 + 9:30 = 9:32
  3. 00:04 + 9:30 = 9:34
  4. ETC。

答案1

我懷疑這是最好的方法,但這是一個解決方案。它顯然不是很完美,因為我不確定您將如何投入時間增量,或者資料是否只是一個單元格中的一段,但它應該可以幫助您完成大部分工作。

它將活動單元格值拆分為一個數組,查找“[”,然後按小時步長調整前 2 個字符,按分鐘步長調整字符 4 和 5,然後將它們全部放回一起。

這也適用於 activecell,這不是很好的編碼實踐。

Sub update_times()

    Dim sParts() As String
    Dim input_text As String
    Dim output_text As String
    Dim split_on As String
    Dim hours_add As Integer
    Dim minutes_add As Integer
    Dim hours_final As Integer
    Dim minutes_final As Integer

    split_on = "["
    hours_add = 9
    minutes_add = 30
    input_text = ActiveCell.Value

    sParts = Split(input_text, split_on)
    output_text = sParts(0)

    For i = 1 To UBound(sParts)
        hours_final = Left(sParts(i), 2) + hours_add
        minutes_final = Mid(sParts(i), 4, 2) + minutes_add
        'checks to wrap data
        If minutes_final > 59 Then
            hours_final = hours_final + 1
            minutes_final = minutes_final - 60
        End If
        If hours_final > 23 Then
            hours_final = hours_final - 24
        End If
        'put the part back together with the new hour and minutes
        sParts(i) = "[" & Format(hours_final, "00") & ":" & Format(minutes_final, "00") & Right(sParts(i), Len(sParts(i)) - 5)
        'add the part to the final string
        output_text = output_text & sParts(i)
    Next
    ActiveCell.Value = output_text

End Sub

相關內容