Skip to content

Commit

Permalink
Refractor and cleanup code
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <mrtops03@gmail.com>
  • Loading branch information
ecdye committed Sep 27, 2024
1 parent 72b251d commit fa72454
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 133 deletions.
9 changes: 5 additions & 4 deletions Sources/macSubtitleOCR/Extensions/DataExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ extension Data {
return bytes.reduce(0) { T($0) << 8 + T($1) }
}
}

func hexEncodedString() -> String {
map { String(format: "%02hhx", $0) }.joined()
}
/* Useful for debugging purposes
func hexEncodedString() -> String {
map { String(format: "%02hhx", $0) }.joined()
}
*/
}
12 changes: 12 additions & 0 deletions Sources/macSubtitleOCR/VobSub/MPEG2PacketType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// MPEG2PacketType.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/26/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

enum MPEG2PacketType {
static let psPacket: UInt32 = 0x0000_01BA
static let pesPacket: UInt32 = 0x0000_01BD
}
12 changes: 12 additions & 0 deletions Sources/macSubtitleOCR/VobSub/RLEFragment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// RLEFragment.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/26/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

struct RLEFragment {
let offset: Int
let size: Int
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SUBFileHandler.swift
// VobSub.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/21/24.
Expand All @@ -9,44 +9,43 @@
import CoreGraphics
import Foundation
import ImageIO
import os

class VobSubDecoder {
struct Subtitle {
class VobSub {
struct IdxSubtitleReference {
let timestamp: String
let offset: Int
}

var idxPalette = [UInt8]()
var decodedSubtitles = [CGImage]()
var logger = Logger(subsystem: "github.ecdye.macSubtitleOCR", category: "VobSub")
private var stderr = StandardErrorOutputStream()

func readVobSubFiles(subFileURL: URL, idxFilePath: String) -> (FileHandle, String)? {
do {
let fileHandle = try FileHandle(forReadingFrom: subFileURL)
let idxData = try String(contentsOf: URL(fileURLWithPath: idxFilePath), encoding: .utf8)
return (fileHandle, idxData)
} catch {
print("Error reading files: \(error)")
return nil
}
}

func parseIdxFile(idxData: String) -> [Subtitle] {
print("Parsing idx file")
var subtitles = [Subtitle]()
func parseIdxFile(idxData: String) -> [IdxSubtitleReference] {
var subtitles = [IdxSubtitleReference]()
let lines = idxData.split(separator: "\n")
print("Lines: \(lines)")
let timestampRegex: NSRegularExpression
let offsetRegex: NSRegularExpression
do {
timestampRegex = try NSRegularExpression(pattern: "timestamp: (\\d{2}:\\d{2}:\\d{2}:\\d{3})")
offsetRegex = try NSRegularExpression(pattern: "filepos: (\\w+)")
} catch {
print("Error creating regular expressions: \(error)")
print("Error: Failed to create regular expressions: \(error)", to: &stderr)
return []
}

for line in lines {
print("Parsing line: \(line)")
if line.starts(with: "palette:") {
let entries = line.split(separator: ", ").map { String($0) }
for entry in entries {
Expand All @@ -62,18 +61,17 @@ class VobSubDecoder {
let timestamp = (line as NSString).substring(with: timestampMatch.range(at: 1))
let offsetString = (line as NSString).substring(with: offsetMatch.range(at: 1))
if let offset = Int(offsetString, radix: 16) {
subtitles.append(Subtitle(timestamp: timestamp, offset: offset))
subtitles.append(IdxSubtitleReference(timestamp: timestamp, offset: offset))
}
}
}
}
return subtitles
}

func extractSubtitleImages(subFile: FileHandle, subtitles: [Subtitle]) throws {
func extractSubtitleImages(subFile: FileHandle, subtitles: [IdxSubtitleReference]) throws {
var idx = 0
for subtitle in subtitles {
print("Parsing subtitle at offset \(subtitle.offset) with timestamp \(subtitle.timestamp)")
let offset = UInt64(subtitle.offset)
var nextOffset: UInt64
if idx + 1 < subtitles.count {
Expand All @@ -86,8 +84,8 @@ class VobSubDecoder {
var pic = VobSubSubtitle()
try readSubFrame(pic: &pic, subFile: subFile, offset: offset, nextOffset: nextOffset)
subFile.seek(toFileOffset: UInt64(pic.rleFragments[0].offset))
print("Found image at offset \(subtitle.offset) with timestamp \(subtitle.timestamp)")
print("Image size: \(pic.imageWidth) x \(pic.imageHeight)")
logger.debug("Found image at offset \(subtitle.offset) with timestamp \(subtitle.timestamp)")
logger.debug("Image size: \(pic.imageWidth) x \(pic.imageHeight)")
let cgImage = try decodeRLEToCGImage(subPicture: pic, fileData: subFile, palette: idxPalette)
decodedSubtitles.append(cgImage!)
idx += 1
Expand All @@ -96,10 +94,9 @@ class VobSubDecoder {

func decodeVobSub(subFilePath: String, idxFilePath: String) throws {
guard let (subFile, idxData) = readVobSubFiles(subFileURL: URL(filePath: subFilePath), idxFilePath: idxFilePath) else {
print("f")
print("Error: Failed to read files", to: &stderr)
return
}
print("Here")
let subtitles = parseIdxFile(idxData: idxData)
try extractSubtitleImages(subFile: subFile, subtitles: subtitles)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/macSubtitleOCR/VobSub/VobSubError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// VobSubError.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/26/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

enum VobSubError: Error {
case invalidRLEBufferOffset
}
Loading

0 comments on commit fa72454

Please sign in to comment.