AVAsset随时间切割不正确

时间:2016-11-16 17:50:19

标签: objective-c avfoundation avasset

我有这段代码:

float seconds = CMTimeGetSeconds(editableAsset.duration);

        AVMutableComposition *thirtMutableComposition = [AVMutableComposition composition];

        if (assetVideoTrack != nil) {
            AVMutableCompositionTrack *compositionVideoTrack = [thirtMutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
            AVAssetTrack *videoAssetTrack= [[editableAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
            compositionVideoTrack.preferredTransform = videoAssetTrack.preferredTransform;
            [compositionVideoTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(1, NSEC_PER_SEC), CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC)) ofTrack:assetVideoTrack atTime:kCMTimeZero error:nil];

        }

        if (assetAudioTrack != nil) {
            AVMutableCompositionTrack *compositionAudioTrack = [thirtMutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
            [compositionAudioTrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(1, NSEC_PER_SEC), CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC)) ofTrack:assetAudioTrack atTime:kCMTimeZero error:nil];

        }

它不起作用。关于它的奇怪之处:如果我使用:

float seconds = 10;

而不是第一行它工作正常。但是第一部分中的浮点值恰好是10秒。

你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您对CMTimeRangeMake()的来电在两个方面看起来很奇怪:

首先,你要从资产中删除1秒,所以你应该将持续时间减少1秒:

CMTimeRangeMake(CMTimeMakeWithSeconds(1, NSEC_PER_SEC), CMTimeMakeWithSeconds(seconds-1, NSEC_PER_SEC))

其次,对NSEC_PER_SEC的{​​{1}}次调用的preferredTimescale使用CMTimeMakeWithSeconds看起来很可疑。 10 9 似乎有点大。为什么不尝试600这样的东西? e.g。

CMTimeMakeWithSeconds(1, 600)