From bd7910c40442fd01107677d2c20938b64693ddc3 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 12 Oct 2016 15:29:20 +0200 Subject: [PATCH] Improve duration calculation in MP42AVFoundationImporter. --- MP42/MP42AVFImporter.m | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 5223517..d52d1df 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -86,6 +86,7 @@ _localAsset = [[AVAsset assetWithURL:self.fileURL] retain]; NSArray *tracks = [_localAsset tracks]; + CMTime globaDuration = _localAsset.duration; NSArray *availableChapter = [_localAsset availableChapterLocales]; MP42ChapterTrack *chapters = nil; @@ -236,8 +237,22 @@ newTrack.format = [self formatForTrack:track]; newTrack.trackId = track.trackID; newTrack.URL = self.fileURL; - newTrack.dataLength = track.totalSampleDataLength; + + // Use the global duration if track duration is not available. + CMTimeRange timeRange = track.timeRange; + if (timeRange.duration.timescale > 0) { + newTrack.duration = timeRange.duration.value / timeRange.duration.timescale * 1000; + } + else { + newTrack.duration = globaDuration.value / globaDuration.timescale * 1000; + } newTrack.bitrate = track.estimatedDataRate; + if (track.totalSampleDataLength > 0) { + newTrack.dataLength = track.totalSampleDataLength; + } + else { + newTrack.dataLength = newTrack.duration * newTrack.bitrate / 1000; + } NSArray *trackMetadata = [track metadataForFormat:AVMetadataFormatQuickTimeUserData]; @@ -281,11 +296,6 @@ } } - CMTimeRange timeRange = track.timeRange; - if (timeRange.duration.timescale > 0) { - newTrack.duration = timeRange.duration.value / timeRange.duration.timescale * 1000; - } - [self addTrack:newTrack]; [newTrack release]; }