From 017cd8d8c1327a98b3b8e4e79b6f7ee5b8db2602 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sun, 14 Dec 2014 09:23:18 +0100 Subject: [PATCH] Return NO in updateMP4FileWithAttributes:attributes:error: only if a serious error happened like a permission error or the mp4 not closing properly. Print the other errors to the log. --- MP42/MP42File.m | 100 ++++++++++++++++------------ MP42/MP42MkvImporter.m | 6 +- MP42/MP42Muxer.h | 5 +- MP42/MP42Muxer.m | 141 +++++++++++++++++++++------------------- MP42/MP42SubUtilities.h | 4 ++ MP42/MP42SubUtilities.m | 22 ++++--- 6 files changed, 155 insertions(+), 123 deletions(-) diff --git a/MP42/MP42File.m b/MP42/MP42File.m index 6f4553b..d9511f3 100644 --- a/MP42/MP42File.m +++ b/MP42/MP42File.m @@ -710,100 +710,113 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { } - (BOOL)updateMP4FileWithAttributes:(NSDictionary *)attributes error:(NSError **)outError { - BOOL noErr = YES; - // Organize the alternate groups - if ([[attributes valueForKey:MP42OrganizeAlternateGroups] boolValue]) + if ([[attributes valueForKey:MP42OrganizeAlternateGroups] boolValue]) { [self organizeAlternateGroups]; + } // Open the mp4 file if (![self startWriting]) { - if (outError) + if (outError) { *outError = MP42Error(@"The file could not be saved.", @"You may do not have sufficient permissions for this operation.", 101); + [_logger writeErrorToLog:*outError]; + } return NO; } // Delete tracks - for (MP42Track *track in _tracksToBeDeleted) + for (MP42Track *track in _tracksToBeDeleted) { [self removeMuxedTrack:track]; + } // Init the muxer and prepare the work - NSMutableArray *tracksToMux = [[NSMutableArray alloc] init]; + NSMutableArray *unsupportedTracks = [[NSMutableArray alloc] init]; + self.muxer = [[[MP42Muxer alloc] initWithFileHandle:self.fileHandle delegate:self logger:_logger] autorelease]; for (MP42Track *track in self.itracks) { if (!track.muxed) { // Reopen the file importer is they are not already open - // this happens when the object was unarchived from a file - if (![track isMemberOfClass:[MP42ChapterTrack class]] && !track.muxer_helper->importer && [track sourceURL]) { - MP42FileImporter *fileImporter = [self.importers valueForKey:[[track sourceURL] path]]; + // this happens when the object was unarchived from a file. + if (![track isMemberOfClass:[MP42ChapterTrack class]] && !track.muxer_helper->importer && track.sourceURL) { + MP42FileImporter *fileImporter = [self.importers valueForKey:track.sourceURL.path]; if (!fileImporter) { - fileImporter = [[[MP42FileImporter alloc] initWithURL:[track sourceURL] error:outError] autorelease]; - [self.importers setObject:fileImporter forKey:[[track sourceURL] path]]; + fileImporter = [[[MP42FileImporter alloc] initWithURL:track.sourceURL error:outError] autorelease]; + [self.importers setObject:fileImporter forKey:track.sourceURL.path]; } if (fileImporter) { track.muxer_helper->importer = fileImporter; } else { if (outError) { - *outError = MP42Error(@"Missing sources.", - @"One or more sources files are missing.", - 200); + NSError *error = MP42Error(@"Missing sources.", @"One or more sources files are missing.", 200); + [_logger writeErrorToLog:error]; + if (outError) { *outError = error; } } - noErr = NO; break; } - } // Add the track to the muxer - if (track.muxer_helper->importer) - [tracksToMux addObject:track]; + if (track.muxer_helper->importer) { + if ([self.muxer canAddTrack:track]) { + [self.muxer addTrack:track]; + } else { + // We don't know how to handle this type of track. + NSError *error = MP42Error(@"Unsupported track", + [NSString stringWithFormat:@"%@, %@, has not been muxed.", track.name, track.format], + 201); + + [_logger writeErrorToLog:error]; + if (outError) { *outError = error; } + + [unsupportedTracks addObject:track]; + } + } } } - if (!noErr) { - [self stopWriting]; - return NO; - } + [self.muxer setup:outError]; + [self.muxer work]; + self.muxer = nil; - if ([tracksToMux count]) { - self.muxer = [[[MP42Muxer alloc] initWithDelegate:self andLogger:_logger] autorelease]; + // Remove the unsupported tracks from the array of the tracks + // to update. Unsupported tracks haven't been muxed, so there is no + // to update them. + NSMutableArray *tracksToUpdate = [self.itracks mutableCopy]; + [tracksToUpdate removeObjectsInArray:unsupportedTracks]; + [unsupportedTracks release]; - for (MP42Track *track in tracksToMux) { - [self.muxer addTrack:track]; - } - - // Setup the muxer - noErr = [self.muxer setup:self.fileHandle error:outError]; - [self.muxer work]; - self.muxer = nil; - } - - [tracksToMux release]; [self.importers removeAllObjects]; // Update modified tracks properties updateMoovDuration(self.fileHandle); - for (MP42Track *track in self.itracks) { + for (MP42Track *track in tracksToUpdate) { if (track.isEdited) { - noErr = [track writeToFile:self.fileHandle error:outError]; - if (!noErr) - break; + if (![track writeToFile:self.fileHandle error:outError]) { + if (*outError) { + [_logger writeErrorToLog:*outError]; + } + } } } + [tracksToUpdate release]; + // Update metadata - if (self.metadata.isEdited) + if (self.metadata.isEdited) { [self.metadata writeMetadataWithFileHandle:self.fileHandle]; + } // Close the mp4 file handle if (![self stopWriting]) { - if (outError) + if (*outError == nil) { *outError = MP42Error(@"File excedes 4 GB.", @"The file is bigger than 4 GB, but it was created with 32bit data chunk offset.\nSelect 64bit data chunk offset in the save panel.", 102); + [_logger writeErrorToLog:*outError]; + } return NO; } @@ -814,10 +827,11 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { [self customChaptersPreview]; } - if ([self.delegate respondsToSelector:@selector(saveDidEnd:)]) + if ([self.delegate respondsToSelector:@selector(saveDidEnd:)]) { [self.delegate performSelector:@selector(saveDidEnd:) withObject:self]; + } - return noErr; + return YES; } #pragma mark - Chapters previews diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index b06070f..779d8ce 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -676,8 +676,12 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t if (trackInfo->Type == TT_SUB) { if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { if (strcmp(trackInfo->CodecID, "S_VOBSUB") && strcmp(trackInfo->CodecID, "S_HDMV/PGS")) { - if (!demuxHelper->ss) + if (!demuxHelper->ss) { demuxHelper->ss = [[SBSubSerializer alloc] init]; + if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) { + [demuxHelper->ss setSSA:YES]; + } + } NSString *string = [[[NSString alloc] initWithBytes:frame length:FrameSize encoding:NSUTF8StringEncoding] autorelease]; if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) { diff --git a/MP42/MP42Muxer.h b/MP42/MP42Muxer.h index 5f894e6..e96b33c 100644 --- a/MP42/MP42Muxer.h +++ b/MP42/MP42Muxer.h @@ -27,11 +27,12 @@ int32_t _cancelled; } -- (instancetype)initWithDelegate:(id )del andLogger:(id )logger; +- (instancetype)initWithFileHandle:(MP4FileHandle)fileHandle delegate:(id )del logger:(id )logger; +- (BOOL)canAddTrack:(MP42Track *)track; - (void)addTrack:(MP42Track *)track; -- (BOOL)setup:(MP4FileHandle)fileHandle error:(NSError **)outError; +- (BOOL)setup:(NSError **)outError; - (void)work; - (void)cancel; diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 1eb2724..c9d8154 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -27,10 +27,11 @@ return self; } -- (instancetype)initWithDelegate:(id )del andLogger:(id )logger +- (instancetype)initWithFileHandle:(MP4FileHandle)fileHandle delegate:(id )del logger:(id )logger { - if ((self = [super init])) { - _workingTracks = [[NSMutableArray alloc] init]; + if ((self = [self init])) { + NSParameterAssert(fileHandle); + _fileHandle = fileHandle; _delegate = del; _logger = [logger retain]; } @@ -38,16 +39,29 @@ return self; } -- (void)addTrack:(MP42Track*)track +- (BOOL)canAddTrack:(MP42Track *)track +{ + NSArray *supportedFormats = @[MP42VideoFormatH264, MP42VideoFormatMPEG4Visual, MP42VideoFormatJPEG, + MP42AudioFormatAAC, MP42AudioFormatAC3, MP42AudioFormatALAC, MP42AudioFormatDTS, + MP42SubtitleFormatTx3g, MP42SubtitleFormatVobSub, MP42ClosedCaptionFormatCEA608]; + if ([supportedFormats containsObject:track.format]) { + return YES; + if ([track isMemberOfClass:[MP42AudioTrack class]]) { + // TO-DO Check if we can initialize the audio converter + } + } else { + return NO; + } +} + +- (void)addTrack:(MP42Track *)track { if (![track isMemberOfClass:[MP42ChapterTrack class]]) [_workingTracks addObject:track]; } -- (BOOL)setup:(MP4FileHandle)fileHandle error:(NSError **)outError +- (BOOL)setup:(NSError **)outError { - BOOL noErr = YES; - _fileHandle = fileHandle; NSMutableArray *unsupportedTracks = [[NSMutableArray alloc] init];; for (MP42Track *track in _workingTracks) { @@ -60,16 +74,17 @@ magicCookie = [helper->importer magicCookieForTrack:track]; timeScale = [helper->importer timescaleForTrack:track]; } else { + [unsupportedTracks addObject:track]; continue; } - if([track isMemberOfClass:[MP42AudioTrack class]] && track.needConversion) { + // Setup the converters + if ([track isMemberOfClass:[MP42AudioTrack class]] && track.needConversion) { MP42AudioConverter *audioConverter = [[MP42AudioConverter alloc] initWithTrack:(MP42AudioTrack *)track andMixdownType:[(MP42AudioTrack *)track mixdownType] error:outError]; if (audioConverter == nil) { - noErr = NO; if (outError && *outError) { [_logger writeErrorToLog:*outError]; } @@ -79,12 +94,11 @@ helper->converter = audioConverter; } - if([track isMemberOfClass:[MP42SubtitleTrack class]] && ([track.sourceFormat isEqualToString:MP42SubtitleFormatVobSub] || [track.sourceFormat isEqualToString:MP42SubtitleFormatPGS]) && track.needConversion) { + if ([track isMemberOfClass:[MP42SubtitleTrack class]] && ([track.sourceFormat isEqualToString:MP42SubtitleFormatVobSub] || [track.sourceFormat isEqualToString:MP42SubtitleFormatPGS]) && track.needConversion) { MP42BitmapSubConverter *subConverter = [[MP42BitmapSubConverter alloc] initWithTrack:(MP42SubtitleTrack *)track error:outError]; if (subConverter == nil) { - noErr = NO; if (outError && *outError) { [_logger writeErrorToLog:*outError]; } @@ -93,9 +107,9 @@ } helper->converter = subConverter; - } - else if([track isMemberOfClass:[MP42SubtitleTrack class]] && track.needConversion) + } else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && track.needConversion) { track.format = MP42SubtitleFormatTx3g; + } // H.264 video track if ([track isMemberOfClass:[MP42VideoTrack class]] && [track.format isEqualToString:MP42VideoFormatH264]) { @@ -105,7 +119,7 @@ NSSize size = [helper->importer sizeForTrack:track]; uint8_t *avcCAtom = (uint8_t*)[magicCookie bytes]; - dstTrackId = MP4AddH264VideoTrack(fileHandle, timeScale, + dstTrackId = MP4AddH264VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, size.width, size.height, avcCAtom[1], // AVCProfileIndication @@ -119,7 +133,7 @@ for (i = 0; i < spsCount; i++) { uint16_t spsSize = (avcCAtom[ptrPos++] << 8) & 0xff00; spsSize += avcCAtom[ptrPos++] & 0xff; - MP4AddH264SequenceParameterSet(fileHandle, dstTrackId, + MP4AddH264SequenceParameterSet(_fileHandle, dstTrackId, avcCAtom+ptrPos, spsSize); ptrPos += spsSize; } @@ -128,27 +142,27 @@ for (i = 0; i < ppsCount; i++) { uint16_t ppsSize = (avcCAtom[ptrPos++] << 8) & 0xff00; ppsSize += avcCAtom[ptrPos++] & 0xff; - MP4AddH264PictureParameterSet(fileHandle, dstTrackId, + MP4AddH264PictureParameterSet(_fileHandle, dstTrackId, avcCAtom+ptrPos, ppsSize); ptrPos += ppsSize; } - MP4SetVideoProfileLevel(fileHandle, 0x15); + MP4SetVideoProfileLevel(_fileHandle, 0x15); [helper->importer setActiveTrack:track]; } // MPEG-4 Visual video track else if ([track isMemberOfClass:[MP42VideoTrack class]] && [track.format isEqualToString:MP42VideoFormatMPEG4Visual]) { - MP4SetVideoProfileLevel(fileHandle, MPEG4_SP_L3); + MP4SetVideoProfileLevel(_fileHandle, MPEG4_SP_L3); // Add video track - dstTrackId = MP4AddVideoTrack(fileHandle, timeScale, + dstTrackId = MP4AddVideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, [(MP42VideoTrack*)track width], [(MP42VideoTrack*)track height], MP4_MPEG4_VIDEO_TYPE); if ([magicCookie length]) - MP4SetTrackESConfiguration(fileHandle, dstTrackId, + MP4SetTrackESConfiguration(_fileHandle, dstTrackId, [magicCookie bytes], [magicCookie length]); @@ -158,7 +172,7 @@ // Photo-JPEG video track else if ([track isMemberOfClass:[MP42VideoTrack class]] && [track.format isEqualToString:MP42VideoFormatJPEG]) { // Add video track - dstTrackId = MP4AddJpegVideoTrack(fileHandle, timeScale, + dstTrackId = MP4AddJpegVideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, [(MP42VideoTrack*)track width], [(MP42VideoTrack*)track height]); [helper->importer setActiveTrack:track]; @@ -166,12 +180,12 @@ // AAC audio track else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatAAC]) { - dstTrackId = MP4AddAudioTrack(fileHandle, + dstTrackId = MP4AddAudioTrack(_fileHandle, timeScale, 1024, MP4_MPEG4_AUDIO_TYPE); if (!track.needConversion && [magicCookie length]) { - MP4SetTrackESConfiguration(fileHandle, dstTrackId, + MP4SetTrackESConfiguration(_fileHandle, dstTrackId, [magicCookie bytes], [magicCookie length]); } @@ -186,7 +200,7 @@ const uint64_t * ac3Info = (const uint64_t *)[magicCookie bytes]; - dstTrackId = MP4AddAC3AudioTrack(fileHandle, + dstTrackId = MP4AddAC3AudioTrack(_fileHandle, timeScale, ac3Info[0], ac3Info[1], @@ -200,21 +214,21 @@ // ALAC audio track else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatALAC]) { - dstTrackId = MP4AddALACAudioTrack(fileHandle, + dstTrackId = MP4AddALACAudioTrack(_fileHandle, timeScale); if ([magicCookie length]) - MP4SetTrackBytesProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", [magicCookie bytes], [magicCookie length]); + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", [magicCookie bytes], [magicCookie length]); [helper->importer setActiveTrack:track]; } // DTS audio track else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatDTS]) { - dstTrackId = MP4AddAudioTrack(fileHandle, + dstTrackId = MP4AddAudioTrack(_fileHandle, timeScale, 512, 0xA9); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.*.channels", [(MP42AudioTrack*)track channels]); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.*.channels", [(MP42AudioTrack*)track channels]); [helper->importer setActiveTrack:track]; } @@ -233,10 +247,10 @@ } if (!videoSize.width) { - MP4TrackId videoTrack = findFirstVideoTrack(fileHandle); + MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle); if (videoTrack) { - videoSize.width = getFixedVideoWidth(fileHandle, videoTrack); - videoSize.height = MP4GetTrackVideoHeight(fileHandle, videoTrack); + videoSize.width = getFixedVideoWidth(_fileHandle, videoTrack); + videoSize.height = MP4GetTrackVideoHeight(_fileHandle, videoTrack); } else { videoSize.width = 640; @@ -253,11 +267,11 @@ subSize.height = videoSize.height; const uint8_t textColor[4] = { 255,255,255,255 }; - dstTrackId = MP4AddSubtitleTrack(fileHandle, timeScale, videoSize.width, subSize.height); + dstTrackId = MP4AddSubtitleTrack(_fileHandle, timeScale, videoSize.width, subSize.height); - MP4SetTrackDurationPerChunk(fileHandle, dstTrackId, timeScale / 8); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "tkhd.alternate_group", 2); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "tkhd.layer", -1); + MP4SetTrackDurationPerChunk(_fileHandle, dstTrackId, timeScale / 8); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "tkhd.alternate_group", 2); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "tkhd.layer", -1); int32_t displayFlags = 0; if (vPlacement) @@ -267,25 +281,25 @@ else if ([(MP42SubtitleTrack *)track allSamplesAreForced]) displayFlags += 0xC0000000; - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.displayFlags", displayFlags); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.displayFlags", displayFlags); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.horizontalJustification", 1); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.verticalJustification", -1); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.horizontalJustification", 1); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.verticalJustification", -1); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorRed", 0); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorGreen", 0); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorBlue", 0); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorAlpha", 0); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorRed", 0); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorGreen", 0); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorBlue", 0); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.bgColorAlpha", 0); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.defTextBoxBottom", subSize.height); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.defTextBoxRight", videoSize.width); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.defTextBoxBottom", subSize.height); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.defTextBoxRight", videoSize.width); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontSize", videoSize.height * 0.05); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontSize", videoSize.height * 0.05); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorRed", textColor[0]); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorGreen", textColor[1]); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorBlue", textColor[2]); - MP4SetTrackIntegerProperty(fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorAlpha", textColor[3]); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorRed", textColor[0]); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorGreen", textColor[1]); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorBlue", textColor[2]); + MP4SetTrackIntegerProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.tx3g.fontColorAlpha", textColor[3]); /* translate the track */ if (!vPlacement) { @@ -294,11 +308,11 @@ uint32_t *ptr32 = (uint32_t*) nval; uint32_t size; - MP4GetTrackBytesProperty(fileHandle, dstTrackId, "tkhd.matrix", &val, &size); + MP4GetTrackBytesProperty(_fileHandle, dstTrackId, "tkhd.matrix", &val, &size); memcpy(nval, val, size); ptr32[7] = CFSwapInt32HostToBig( (videoSize.height * 0.85) * 0x10000); - MP4SetTrackBytesProperty(fileHandle, dstTrackId, "tkhd.matrix", nval, size); + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "tkhd.matrix", nval, size); free(val); } @@ -312,7 +326,7 @@ if ([magicCookie length] < sizeof(uint32_t) * 16) continue; - dstTrackId = MP4AddSubpicTrack(fileHandle, timeScale, 640, 480); + dstTrackId = MP4AddSubpicTrack(_fileHandle, timeScale, 640, 480); uint32_t *subPalette = (uint32_t*) [magicCookie bytes]; int ii; @@ -326,7 +340,7 @@ palette[ii][2] = (subPalette[ii] >> 8) & 0xff; palette[ii][3] = (subPalette[ii]) & 0xff; } - MP4SetTrackESConfiguration( fileHandle, dstTrackId, + MP4SetTrackESConfiguration(_fileHandle, dstTrackId, (uint8_t*)palette, 16 * 4 ); [helper->importer setActiveTrack:track]; @@ -344,10 +358,10 @@ } if (!videoSize.width) { - MP4TrackId videoTrack = findFirstVideoTrack(fileHandle); + MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle); if (videoTrack) { - videoSize.width = getFixedVideoWidth(fileHandle, videoTrack); - videoSize.height = MP4GetTrackVideoHeight(fileHandle, videoTrack); + videoSize.width = getFixedVideoWidth(_fileHandle, videoTrack); + videoSize.height = MP4GetTrackVideoHeight(_fileHandle, videoTrack); } else { videoSize.width = 640; @@ -355,25 +369,16 @@ } } - dstTrackId = MP4AddCCTrack(fileHandle, timeScale, videoSize.width, videoSize.height); + dstTrackId = MP4AddCCTrack(_fileHandle, timeScale, videoSize.width, videoSize.height); [helper->importer setActiveTrack:track]; } else { - // We don't know how to handle this type of track. - // Alert the user. - NSError *error = MP42Error(@"Unsupported track", - [NSString stringWithFormat:@"%@, %@, has not been muxed.", track.name, track.format], - 140); - - [_logger writeErrorToLog:error]; - if (outError) { *outError = error; } [unsupportedTracks addObject:track]; - continue; } if (dstTrackId) { - MP4SetTrackDurationPerChunk(fileHandle, dstTrackId, timeScale / 8); + MP4SetTrackDurationPerChunk(_fileHandle, dstTrackId, timeScale / 8); track.Id = dstTrackId; } } @@ -381,7 +386,7 @@ [_workingTracks removeObjectsInArray:unsupportedTracks]; [unsupportedTracks release]; - return noErr; + return YES; } - (void)work diff --git a/MP42/MP42SubUtilities.h b/MP42/MP42SubUtilities.h index e3a0675..2ac8a6b 100644 --- a/MP42/MP42SubUtilities.h +++ b/MP42/MP42SubUtilities.h @@ -33,6 +33,8 @@ BOOL position_information, forced; unsigned last_begin_time, last_end_time; unsigned linesInput; + + BOOL ssa; } -(void)addLine:(SBSubLine *)sline; -(void)setFinished:(BOOL)finished; @@ -42,6 +44,8 @@ -(void)setPositionInformation:(BOOL)info; -(BOOL)forced; -(void)setForced:(BOOL)info; +-(void)setSSA:(BOOL)ssa; + @end NSMutableString *STStandardizeStringNewlines(NSString *str); diff --git a/MP42/MP42SubUtilities.m b/MP42/MP42SubUtilities.m index ba33115..abe0559 100644 --- a/MP42/MP42SubUtilities.m +++ b/MP42/MP42SubUtilities.m @@ -118,8 +118,13 @@ canOutput: if (l->begin_time > begin_time) end_time = MIN(end_time, l->begin_time); - if (l->begin_time <= begin_time) - [str appendString:l->line]; + if (l->begin_time <= begin_time) { + // Try to be a bit smart and avoid duplicated lines + // from ssa. + if (!ssa || ![str containsString:l->line]) { + [str appendString:l->line]; + } + } } for (i = 0; i < nlines; i++) { @@ -186,6 +191,11 @@ canOutput: forced = info; } +-(void)setSSA:(BOOL)isSSA +{ + ssa = isSSA; +} + -(NSString*)description { return [NSString stringWithFormat:@"lines left: %lu finished inputting: %d",(unsigned long)[lines count],finished]; @@ -958,13 +968,7 @@ NSString* removeNewLines(NSString* string) { while ([mutableString length] && [mutableString characterAtIndex:[mutableString length] - 1] == '\n') [mutableString deleteCharactersInRange:NSMakeRange([mutableString length] -1, 1)]; - NSMutableString *mutableString2 = [NSMutableString stringWithString:mutableString]; - - NSUInteger replacedCount = [mutableString2 replaceOccurrencesOfString:@"\n" withString:@" " options:0 range:NSMakeRange(0,[mutableString2 length])]; - if (replacedCount >= 2) - return mutableString2; - else - return mutableString; + return mutableString; } void createForcedAtom(u_int8_t* buffer) {