From 6f0db744216ae1b5bd42982fa95bd56710307956 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Wed, 2 Oct 2024 20:06:47 -0600 Subject: [PATCH] Hack timestamps to make them accurate Signed-off-by: Ethan Dye --- .../macSubtitleOCR/Subtitles/Subtitle.swift | 2 -- .../Subtitles/VobSub/VobSub.swift | 18 ++++-------------- .../Subtitles/VobSub/VobSubError.swift | 11 ----------- .../Subtitles/VobSub/VobSubParser.swift | 7 +++++++ 4 files changed, 11 insertions(+), 27 deletions(-) delete mode 100644 Sources/macSubtitleOCR/Subtitles/VobSub/VobSubError.swift diff --git a/Sources/macSubtitleOCR/Subtitles/Subtitle.swift b/Sources/macSubtitleOCR/Subtitles/Subtitle.swift index 5da1942..f191cda 100644 --- a/Sources/macSubtitleOCR/Subtitles/Subtitle.swift +++ b/Sources/macSubtitleOCR/Subtitles/Subtitle.swift @@ -17,8 +17,6 @@ class Subtitle { var imageYOffset: Int? var imageWidth: Int? var imageHeight: Int? - var imageEvenOffset: Int? - var imageOddOffset: Int? var imageData: Data? var imagePalette: [UInt8]? var imageAlpha: [UInt8]? diff --git a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSub.swift b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSub.swift index 0e84250..272dd49 100644 --- a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSub.swift +++ b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSub.swift @@ -47,26 +47,16 @@ class VobSub { } } - func parseIdxFile(idxData: String) -> [IdxSubtitleReference] { + func parseIdxFile(idxData: String) throws -> [IdxSubtitleReference] { var subtitles = [IdxSubtitleReference]() let lines = idxData.split(separator: "\n") - let timestampRegex: NSRegularExpression - let offsetRegex: NSRegularExpression - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "HH:mm:ss:SSS" - do { - timestampRegex = try NSRegularExpression(pattern: "timestamp: (\\d{2}:\\d{2}:\\d{2}:\\d{3})") - offsetRegex = try NSRegularExpression(pattern: "filepos: (\\w+)") - } catch { - print("Error: Failed to create regular expressions: \(error)", to: &stderr) - return [] - } + let timestampRegex = try NSRegularExpression(pattern: "timestamp: (\\d{2}:\\d{2}:\\d{2}:\\d{3})") + let offsetRegex = try NSRegularExpression(pattern: "filepos: (\\w+)") for line in lines { if line.starts(with: "palette:") { let entries = line.split(separator: ", ").map { String($0) } for entry in entries { - print(entry.hexToBytes) idxPalette.append(contentsOf: entry.hexToBytes) } } @@ -133,7 +123,7 @@ class VobSub { print("Error: Failed to read files", to: &stderr) return } - let subtitles = parseIdxFile(idxData: idxData) + let subtitles = try parseIdxFile(idxData: idxData) try extractSubtitleImages(subFile: subFile, idxReference: subtitles) } } diff --git a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubError.swift b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubError.swift deleted file mode 100644 index ed66ce1..0000000 --- a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubError.swift +++ /dev/null @@ -1,11 +0,0 @@ -// -// VobSubError.swift -// macSubtitleOCR -// -// Created by Ethan Dye on 9/30/24. -// Copyright © 2024 Ethan Dye. All rights reserved. -// - -enum VobSubError: Error { - case invalidRLEBufferOffset -} diff --git a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift index 422f2b5..601c560 100644 --- a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift +++ b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift @@ -116,6 +116,10 @@ func readSubFrame(pic: inout Subtitle, subFile: FileHandle, offset: UInt64, next index += 2 var alphaSum = 0 + let relativeEndTimestamp = controlHeader.value(ofType: UInt16.self, at: endOfControl - 1)! << 10 + let endTimestamp = pic.startTimestamp! + TimeInterval(relativeEndTimestamp) / 90.0 - 9 // Not sure why we need to subtract 9 but its pretty close to accurate + pic.endTimestamp = endTimestamp + logger.debug("Display delay is \(endTimestamp)") while index < endOfControl { let command = controlHeader[index] index += 1 @@ -125,6 +129,9 @@ func readSubFrame(pic: inout Subtitle, subFile: FileHandle, offset: UInt64, next break // Set subtitle as forced case 1: break // Start display + case 2: + let displayDelay = controlHeader.value(ofType: UInt16.self, at: 0) + logger.debug("Display delay is \(displayDelay!)") case 3: var byte = controlHeader[index] index += 1