Files
MP42Foundation/MP42/Sup2Vob/MP42SupConverter.swift
T
Scott GarrettandClaude Code e02f60b5f6 Import BLU-RAY PGS .sup files via vendored sup2vob
MP42VobSubImporter now claims "sup" alongside "idx": the file is converted up-front to a temp .idx/.sub pair, so the existing VobSub machinery (muxable subp passthrough, tx3g conversion, frame size from the idx size: line) carries everything downstream. The pair lives in a private temp directory until the importer's dealloc; tracks retain the importer through export, so demux can always reopen it.

The converter is sup2vob (https://git.technomancer.com/Scott-Files/sup2vob) vendored under MP42/Sup2Vob, kept identical to upstream apart from provenance headers; the CLI-only globals and main.swift are adapted in Convert.swift, and MP42SupConverter is the new @objc facade the importer calls over the MP42-Shared-Swift.h shim. Vendored output was verified byte-for-byte identical to the upstream CLI, and saved MP4s play back as dvd_subtitle tracks.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-28 18:18:16 -05:00

63 lines
2.8 KiB
Swift

//
// MP42SupConverter.swift
// MP42Foundation
//
// Objective-C entry point for the vendored sup2vob converter
// (https://git.technomancer.com/Scott-Files/sup2vob): BLU-RAY PGS .sup →
// VobSub .sub/.idx. MP42VobSubImporter drives it so a dragged-in .sup lands
// on the framework's existing VobSub import/mux path unchanged.
//
import Foundation
@objc(MP42SupConverter) public class SupConverter: NSObject {
@objc public static let errorDomain = "org.galad.MP42Foundation.SubConverter"
/// Converts the BLU-RAY PGS `inputURL` (.sup) to a VobSub pair: the .sub is
/// written to `subURL` and the matching .idx beside it (same stem, `.idx`
/// extension). `language`/`languageName` only decorate the .idx `id:`/`alt:`
/// lines — the importer overwrites the track language from the source
/// filename, the way the .srt importer does.
///
/// Mirrors sup2vob's CLI orchestration: PAL/NTSC picked automatically from
/// the PGS frame-rate code, BT.709 palette interpretation, frames
/// downscaled to the DVD frame the muxer writes.
@objc(convertSupAtURL:toSubAtURL:language:languageName:error:)
public static func convertSup(at inputURL: URL,
toSubAt subURL: URL,
language: String,
languageName: String) throws {
let fileBytes = [UInt8](try Data(contentsOf: inputURL))
let compositions: [Composition]
do {
compositions = try readPGS(fileBytes, space: .bt709)
} catch let supError as PGSError {
throw conversionError(100, "\(inputURL.lastPathComponent): \(supError)")
}
guard !compositions.isEmpty else {
throw conversionError(101, "\(inputURL.lastPathComponent): no compositions found")
}
let resolution = DVDResolution.from(frameRateCode: compositions[0].frameRateCode)
let result = convertToDVD(compositions, resolution: resolution)
guard !result.frames.isEmpty else {
throw conversionError(102, "\(inputURL.lastPathComponent): no displayable subtitle frames")
}
let subFile = writeSub(result.frames)
let idxText = writeIdx(resolution: resolution, fourColorPalette: result.fourColorPalette,
frames: result.frames, filePositions: subFile.filePositions,
lang: language, langName: languageName)
let idxURL = subURL.deletingPathExtension().appendingPathExtension("idx")
try Data(subFile.bytes).write(to: subURL)
try idxText.write(to: idxURL, atomically: true, encoding: .ascii)
}
private static func conversionError(_ code: Int, _ message: String) -> NSError {
NSError(domain: errorDomain, code: code, userInfo: [NSLocalizedDescriptionKey: message])
}
}