Skip to content

Commit

Permalink
Hack timestamps to make them accurate
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Oct 3, 2024
1 parent 757e00a commit 6f0db74
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 27 deletions.
2 changes: 0 additions & 2 deletions Sources/macSubtitleOCR/Subtitles/Subtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]?
Expand Down
18 changes: 4 additions & 14 deletions Sources/macSubtitleOCR/Subtitles/VobSub/VobSub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
11 changes: 0 additions & 11 deletions Sources/macSubtitleOCR/Subtitles/VobSub/VobSubError.swift

This file was deleted.

7 changes: 7 additions & 0 deletions Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6f0db74

Please sign in to comment.