Initial checkin
This commit is contained in:
commit
5c40c7d7e7
5 files changed
+1031
No files matched your search
+127
@@ -0,0 +1,127 @@
|
||||
import Foundation
|
||||
|
||||
let usage = """
|
||||
usage: sup2vob [options] <input.sup>
|
||||
|
||||
Converts a Blu-ray PGS .sup file to VobSub (.sub + .idx), the same output
|
||||
shape BDSup2Sub produces — no Java required.
|
||||
|
||||
options:
|
||||
-o <file> output .sub path (a matching .idx is written beside it);
|
||||
default: <input> with .sup replaced by .sub
|
||||
--ntsc force a 720x480 DVD frame (default: auto from the PGS rate)
|
||||
--pal force a 720x576 DVD frame (default: auto from the PGS rate)
|
||||
--full pass through the source frame size (e.g. 1920x1080) with
|
||||
no scaling — what BDSup2Sub does in HD passthrough mode
|
||||
--lang <id> two-letter language id for the .idx (default: en)
|
||||
--langname <n> alternative language name for the commented alt: line
|
||||
(default: English)
|
||||
--bt601 interpret the PGS palette as BT.601 (default: BT.709)
|
||||
--debug trace per-frame conversion decisions on stderr
|
||||
-h, --help show this help
|
||||
"""
|
||||
|
||||
var outPath: String? = nil
|
||||
var input: String? = nil
|
||||
var forceRes: DVDResolution? = nil
|
||||
var fullFrame = false
|
||||
var lang = "en"
|
||||
var langName = "English"
|
||||
var space: ColorSpace = .bt709
|
||||
|
||||
var args = Array(CommandLine.arguments.dropFirst())
|
||||
while !args.isEmpty {
|
||||
let a = args.removeFirst()
|
||||
switch a {
|
||||
case "-h", "--help":
|
||||
print(usage)
|
||||
exit(0)
|
||||
case "-o":
|
||||
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for -o\n".data(using: .utf8)!); exit(1) }
|
||||
outPath = args.removeFirst()
|
||||
case "--ntsc": forceRes = .ntsc
|
||||
case "--pal": forceRes = .pal
|
||||
case "--full": fullFrame = true
|
||||
case "--lang":
|
||||
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for --lang\n".data(using: .utf8)!); exit(1) }
|
||||
lang = args.removeFirst()
|
||||
case "--langname":
|
||||
guard !args.isEmpty else { FileHandle.standardError.write("missing argument for --langname\n".data(using: .utf8)!); exit(1) }
|
||||
langName = args.removeFirst()
|
||||
case "--bt601": space = .bt601
|
||||
case "-bt601", "--bt709": space = .bt709
|
||||
case "--debug": debugFrames = true
|
||||
default:
|
||||
if a.hasPrefix("-") {
|
||||
FileHandle.standardError.write("unknown option: \(a)\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
if input == nil { input = a }
|
||||
}
|
||||
}
|
||||
|
||||
guard let input else {
|
||||
FileHandle.standardError.write(usage.data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
guard let data = FileManager.default.contents(atPath: input) else {
|
||||
FileHandle.standardError.write("cannot read \(input)\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
let buf = [UInt8](data)
|
||||
|
||||
let comps: [Composition]
|
||||
do {
|
||||
comps = try readPGS(buf, space: space)
|
||||
} catch let e as PGSError {
|
||||
FileHandle.standardError.write("\(input): \(e)\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
} catch {
|
||||
FileHandle.standardError.write("\(input): \(error)\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
guard !comps.isEmpty else {
|
||||
FileHandle.standardError.write("\(input): no compositions found\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
let resolution = forceRes ?? (fullFrame
|
||||
? DVDResolution(width: comps[0].canvasW, height: comps[0].canvasH)
|
||||
: DVDResolution.from(frameRateCode: comps[0].frameRateCode))
|
||||
let result = convertToDVD(comps, resolution: resolution)
|
||||
if debugFrames, let f = result.subs.first {
|
||||
let lut: [UInt8] = [0, 85, 170, 255]
|
||||
var pgm = "P5\n\(f.w) \(f.h)\n255\n".data(using: .ascii)!
|
||||
pgm.append(contentsOf: f.idx.map { lut[Int($0)] })
|
||||
try? pgm.write(to: URL(fileURLWithPath: "/tmp/suptest/dbg_frame0.pgm"))
|
||||
FileHandle.standardError.write("debug: frame0 idx → /tmp/suptest/dbg_frame0.pgm (\(f.w)x\(f.h) x=\(f.x) y=\(f.y))\n".data(using: .utf8)!)
|
||||
}
|
||||
guard !result.subs.isEmpty else {
|
||||
FileHandle.standardError.write("\(input): no displayable subtitle frames\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
let subPath = outPath ?? (input.hasSuffix(".sup") ? String(input.dropLast(4)) + ".sub" : input + ".sub")
|
||||
let idxPath = (subPath as NSString).deletingPathExtension + ".idx"
|
||||
|
||||
let subBytes = writeSub(result.subs)
|
||||
let idxText = writeIdx(resolution: resolution, palette4: result.palette4,
|
||||
subs: result.subs, filepos: subBytes.filepos,
|
||||
lang: lang, langName: langName)
|
||||
|
||||
do {
|
||||
try Data(subBytes.bytes).write(to: URL(fileURLWithPath: subPath))
|
||||
try idxText.write(toFile: idxPath, atomically: true, encoding: .ascii)
|
||||
} catch {
|
||||
FileHandle.standardError.write("write failed: \(error)\n".data(using: .utf8)!)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
let pal = result.palette4.dropFirst().filter { $0.isVisible }
|
||||
.map { String(format: "#%02x%02x%02x", $0.r, $0.g, $0.b) }.joined(separator: " ")
|
||||
FileHandle.standardError.write("""
|
||||
sup2vob: \(result.subs.count) frames → \(subPath) (\(subBytes.bytes.count) bytes) + \(idxPath)
|
||||
frame size \(resolution.width)x\(resolution.height), palette: \(pal)
|
||||
|
||||
""".data(using: .utf8)!)
|
||||
Reference in new issue
Block a user