Skip to content

Commit

Permalink
Fix formatting and remove redundant code
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Sep 22, 2024
1 parent 86d94db commit a1ff503
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 48 deletions.
1 change: 0 additions & 1 deletion Sources/macSubtitleOCR/MKV/EBML/EBML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Foundation

enum EBML {
static let ebml: UInt32 = 0x1A45_DFA3
static let block: UInt32 = 0xA1
static let blockGroup: UInt32 = 0xA0
static let chapters: UInt32 = 0x1043_A770
Expand Down
14 changes: 11 additions & 3 deletions Sources/macSubtitleOCR/MKV/MKVFileHandler.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//
// MKVFileHandler.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/20/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

import Foundation
import os

class MKVFileHandler: MKVFileHandling {
class MKVFileHandler {
var fileHandle: FileHandle
var eof: UInt64
var timestampScale: Double = 1000000.0 // Default value if not specified in a given MKV file
Expand All @@ -11,8 +19,8 @@ class MKVFileHandler: MKVFileHandling {
guard FileManager.default.fileExists(atPath: filePath) else {
throw macSubtitleOCRError.fileReadError
}
self.fileHandle = try FileHandle(forReadingFrom: URL(fileURLWithPath: filePath))
self.eof = fileHandle.seekToEndOfFile()
fileHandle = try FileHandle(forReadingFrom: URL(fileURLWithPath: filePath))
eof = fileHandle.seekToEndOfFile()
fileHandle.seek(toFileOffset: 0)
}

Expand Down
28 changes: 0 additions & 28 deletions Sources/macSubtitleOCR/MKV/MKVProtocol.swift

This file was deleted.

10 changes: 9 additions & 1 deletion Sources/macSubtitleOCR/MKV/MKVSubtitleExtractor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//
// MKVSubtitleExtractor.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/20/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

import Foundation
import os

class MKVSubtitleExtractor: MKVTrackParser, MKVSubtitleExtracting {
class MKVSubtitleExtractor: MKVTrackParser {
func getSubtitleTrackData(trackNumber: Int, outPath: String) throws -> String? {
let tmpSup = URL(fileURLWithPath: outPath).deletingPathExtension().appendingPathExtension("sup").lastPathComponent
let manager = FileManager.default
Expand Down
12 changes: 11 additions & 1 deletion Sources/macSubtitleOCR/MKV/MKVTrackParser.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
//
// MKVTrackParser.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/20/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

import Foundation
import os

class MKVTrackParser: MKVFileHandler, MKVTrackParsing {
class MKVTrackParser: MKVFileHandler {
var tracks: [MKVTrack] = []
private var stderr = StandardErrorOutputStream()

Expand Down Expand Up @@ -79,6 +87,7 @@ class MKVTrackParser: MKVFileHandler, MKVTrackParsing {
// Step 1: Locate the Segment element
guard let segmentSize = locateSegment() else { return nil }
let segmentEndOffset = fileHandle.offsetInFile + segmentSize
// swiftformat:disable:next redundantSelf
logger.debug("Found Segment, Size: \(segmentSize), End Offset: \(segmentEndOffset), EOF: \(self.eof)")

var trackData = [Data](repeating: Data(), count: trackNumber.count)
Expand Down Expand Up @@ -110,6 +119,7 @@ class MKVTrackParser: MKVFileHandler, MKVTrackParsing {

private func parseBlocks(within clusterEndOffset: UInt64, trackNumber: [Int], clusterTimestamp: Int64, trackData: inout [Data]) {
while fileHandle.offsetInFile < clusterEndOffset {
// swiftformat:disable:next redundantSelf
logger.debug("Looking for Block at Offset: \(self.fileHandle.offsetInFile)/\(clusterEndOffset)")
guard case (var blockSize?, let blockType?) = findElement(withID: EBML.simpleBlock, EBML.blockGroup) else { break }

Expand Down
2 changes: 1 addition & 1 deletion Sources/macSubtitleOCR/PGS/PGSSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// PGSSubtitle.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/16/24.
// Created by Ethan Dye on 9/19/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

Expand Down
2 changes: 1 addition & 1 deletion Sources/macSubtitleOCR/PGS/Parsers/ODS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ODS.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/12/24.
// Created by Ethan Dye on 9/19/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

Expand Down
2 changes: 1 addition & 1 deletion Sources/macSubtitleOCR/SRT/SRT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SRT.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/2/24.
// Created by Ethan Dye on 9/19/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

Expand Down
2 changes: 1 addition & 1 deletion Sources/macSubtitleOCR/SRT/SRTSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SRTSubtitle.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/16/24.
// Created by Ethan Dye on 9/19/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

Expand Down
18 changes: 9 additions & 9 deletions Sources/macSubtitleOCR/macSubtitleOCR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct macSubtitleOCR: ParsableCommand {
// MARK: - Properties

@Argument(help: "Input file containing the subtitle stream (.sup or .mkv)")
var sup: String
var input: String

@Argument(help: "Directory to output the completed .srt files to")
var srtDirectory: String
Expand Down Expand Up @@ -49,7 +49,7 @@ struct macSubtitleOCR: ParsableCommand {
let manager = FileManager.default

// Setup options
let inFile = sup
let inFile = input
let revision = setOCRRevision()
let recognitionLevel = setOCRMode()
let languages = language.split(separator: ",").map { String($0) }
Expand All @@ -60,13 +60,13 @@ struct macSubtitleOCR: ParsableCommand {
var srtStreams: [Int: SRT] = [:]
var intermediateFiles: [Int: String] = [:]

if sup.hasSuffix(".mkv") {
let mkvStream = try MKVSubtitleExtractor(filePath: sup)
if input.hasSuffix(".mkv") {
let mkvStream = try MKVSubtitleExtractor(filePath: input)
try mkvStream.parseTracks(codec: "S_HDMV/PGS")
for track in mkvStream.tracks {
subIndex = 1 // reset counter for each track
logger.debug("Found subtitle track: \(track.trackNumber), Codec: \(track.codecId)")
intermediateFiles[track.trackNumber] = try mkvStream.getSubtitleTrackData(trackNumber: track.trackNumber, outPath: sup)!
intermediateFiles[track.trackNumber] = try mkvStream.getSubtitleTrackData(trackNumber: track.trackNumber, outPath: input)!

// Open the PGS data stream
let PGS = try PGS(URL(fileURLWithPath: intermediateFiles[track.trackNumber]!))
Expand All @@ -78,7 +78,7 @@ struct macSubtitleOCR: ParsableCommand {
}
} else {
// Open the PGS data stream
let PGS = try PGS(URL(fileURLWithPath: sup))
let PGS = try PGS(URL(fileURLWithPath: input))
let srtStream = SRT()
srtStreams[0] = srtStream

Expand Down Expand Up @@ -108,8 +108,8 @@ struct macSubtitleOCR: ParsableCommand {
if saveSup, inFile.hasSuffix(".mkv") {
let fileName = "\(trackNumber)_" + URL(fileURLWithPath: inFile).deletingPathExtension().appendingPathExtension("sup").lastPathComponent
try manager.moveItem(
at: URL(fileURLWithPath: intermediateFiles[trackNumber]!),
to: URL(fileURLWithPath: inFile).deletingLastPathComponent().appendingPathComponent(fileName))
at: URL(fileURLWithPath: intermediateFiles[trackNumber]!),
to: URL(fileURLWithPath: inFile).deletingLastPathComponent().appendingPathComponent(fileName))
} else {
try manager.removeItem(at: URL(fileURLWithPath: intermediateFiles[trackNumber]!))
}
Expand Down Expand Up @@ -146,7 +146,7 @@ struct macSubtitleOCR: ParsableCommand {
}
}

private func processSubtitles(PGS: PGS, srtStream: SRT, trackNumber: Int, subIndex: Int, jsonStream: inout [Any], logger: Logger, manager: FileManager, recognitionLevel: VNRequestTextRecognitionLevel, languages: [String], revision: Int) throws {
private func processSubtitles(PGS: PGS, srtStream: SRT, trackNumber _: Int, subIndex: Int, jsonStream: inout [Any], logger: Logger, manager: FileManager, recognitionLevel: VNRequestTextRecognitionLevel, languages: [String], revision: Int) throws {
var subIndex = subIndex
var inJson = jsonStream
for subtitle in PGS.getSubtitles() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/macSubtitleOCRTests/macSubtitleOCRTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// macSubtitleOCRTests.swift
// macSubtitleOCR
//
// Created by Ethan Dye on 9/18/24.
// Created by Ethan Dye on 9/19/24.
// Copyright © 2024 Ethan Dye. All rights reserved.
//

Expand Down

0 comments on commit a1ff503

Please sign in to comment.