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>
This commit is contained in:
1 parent
30dd96ff79
commit
e02f60b5f6
6 files changed
+1099
-4
No files matched your search
@@ -18,6 +18,9 @@
|
||||
#import "MP42Track+Private.h"
|
||||
#import "MP42SampleBuffer.h"
|
||||
|
||||
// MP42SupConverter — the vendored sup2vob PGS→VobSub converter (Sup2Vob/).
|
||||
#import "MP42-Shared-Swift.h"
|
||||
|
||||
MP42_OBJC_DIRECT_MEMBERS
|
||||
@interface SBVobSubSample : NSObject
|
||||
{
|
||||
@@ -227,23 +230,86 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
@implementation MP42VobSubImporter {
|
||||
@private
|
||||
NSArray *_VobSubTracks;
|
||||
/// For .sup sources: the temp directory holding the sup2vob-generated
|
||||
/// .idx/.sub pair, and that .idx's URL. The pair lives until dealloc —
|
||||
/// the model tracks retain this importer through export, and demux reads
|
||||
/// the .sub from disk. Both nil for .idx sources, where the .idx sits
|
||||
/// beside its .sub at fileURL.
|
||||
NSURL *_convertedDirectory;
|
||||
NSURL *_indexFileURL;
|
||||
}
|
||||
|
||||
+ (NSArray<NSString *> *)supportedFileFormats {
|
||||
return @[@"idx"];
|
||||
return @[@"idx", @"sup"];
|
||||
}
|
||||
|
||||
+ (NSArray<UTType *> *)supportedContentTypes
|
||||
{
|
||||
return @[[UTType typeWithFilenameExtension:@"idx"]];
|
||||
return @[[UTType typeWithFilenameExtension:@"idx"],
|
||||
[UTType typeWithFilenameExtension:@"sup"]];
|
||||
}
|
||||
|
||||
// Convert a BLU-RAY PGS .sup into a VobSub pair in a private temp directory
|
||||
// and return the generated .idx URL. The tracks retain this importer until the
|
||||
// model drops them, so dealloc's cleanup can't pull the pair out from under a
|
||||
// later demux.
|
||||
- (NSURL *)convertSupAtURL:(NSURL *)supURL error:(NSError * __autoreleasing *)outError
|
||||
{
|
||||
NSURL *tempDirectory = [[NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]
|
||||
URLByAppendingPathComponent:[NSString stringWithFormat:@"sup2vob-%@", [NSUUID UUID].UUIDString]];
|
||||
NSURL *subURL = [tempDirectory URLByAppendingPathComponent:@"subtitles.sub"];
|
||||
|
||||
if (![NSFileManager.defaultManager createDirectoryAtURL:tempDirectory withIntermediateDirectories:YES attributes:nil error:outError]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSError *conversionError = nil;
|
||||
if (![MP42SupConverter convertSupAtURL:supURL
|
||||
toSubAtURL:subURL
|
||||
language:@"en"
|
||||
languageName:@"English"
|
||||
error:&conversionError]) {
|
||||
// Nothing usable was written; don't leave the empty directory behind
|
||||
// (on success the pair lives here until dealloc instead).
|
||||
[[NSFileManager defaultManager] removeItemAtURL:tempDirectory error:NULL];
|
||||
if (outError) {
|
||||
*outError = conversionError ?:
|
||||
MP42Error(MP42LocalizedString(@"The file could not be opened.", @"vobsub error message"),
|
||||
MP42LocalizedString(@"The file is not a sup file, or it does not contain any subtitles.", @"vobsub error message"), 101);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
_convertedDirectory = tempDirectory;
|
||||
return [tempDirectory URLByAppendingPathComponent:@"subtitles.idx"];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
if (_convertedDirectory) {
|
||||
[[NSFileManager defaultManager] removeItemAtURL:_convertedDirectory error:NULL];
|
||||
}
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError * __autoreleasing *)outError
|
||||
{
|
||||
if ((self = [super initWithURL:fileURL])) {
|
||||
|
||||
_indexFileURL = fileURL;
|
||||
if ([fileURL.pathExtension caseInsensitiveCompare:@"sup"] == NSOrderedSame) {
|
||||
_indexFileURL = [self convertSupAtURL:fileURL error:outError];
|
||||
if (!_indexFileURL) {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
MP42TrackId count = 0;
|
||||
_VobSubTracks = LoadVobSubSubtitles(self.fileURL.URLByDeletingLastPathComponent, self.fileURL.lastPathComponent);
|
||||
_VobSubTracks = LoadVobSubSubtitles(_indexFileURL.URLByDeletingLastPathComponent, _indexFileURL.lastPathComponent);
|
||||
|
||||
// PGS carries no usable language (the generated idx id: line is the
|
||||
// converter default), so — like the .srt/.ssa importers — take it from
|
||||
// the filename; the 2-letter code path below would otherwise pin "en".
|
||||
NSString *filenameLanguage = _convertedDirectory ? getFilenameLanguage((__bridge CFStringRef)fileURL.path) : nil;
|
||||
|
||||
for (SBVobSubTrack *track in _VobSubTracks) {
|
||||
MP42SubtitleTrack *newTrack = [[MP42SubtitleTrack alloc] init];
|
||||
@@ -253,6 +319,13 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
newTrack.alternateGroup = 2;
|
||||
newTrack.trackId = count++;
|
||||
newTrack.language = [MP42Languages.defaultManager extendedTagForISO_639_1:track->language];
|
||||
if (filenameLanguage && ![filenameLanguage isEqualToString:@"und"]) {
|
||||
newTrack.language = filenameLanguage;
|
||||
}
|
||||
// "...eng.sdh.idx/sup" style type tokens name the track after
|
||||
// the type ("SDH") — MP4 has no SDH/CC flag, the name is the
|
||||
// convention players show; the language rides its own element.
|
||||
newTrack.name = getFilenameSubtitlesTrackName((__bridge CFStringRef)fileURL.path);
|
||||
newTrack.timescale = 1000;
|
||||
newTrack.duration = track->duration;
|
||||
|
||||
@@ -310,7 +383,7 @@ MP42_OBJC_DIRECT_MEMBERS
|
||||
{
|
||||
@autoreleasepool {
|
||||
|
||||
NSURL *subFileURL = [self.fileURL.URLByDeletingPathExtension URLByAppendingPathExtension:@"sub"];
|
||||
NSURL *subFileURL = [_indexFileURL.URLByDeletingPathExtension URLByAppendingPathExtension:@"sub"];
|
||||
|
||||
NSData *subFileData = [NSData dataWithContentsOfURL:subFileURL];
|
||||
|
||||
|
||||
Reference in new issue
Block a user