I need trim many videos, and then show preview of their compilation. User can edit start time and end time for every video, and I show preview of current video (while user trim) with code:
AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:video.fileURL]];
int32_t timeScale = player.currentItem.asset.duration.timescale;
float startTime = video.startTime;
float endTime = video.endTime > 0 ? video.endTime : video.duration;
player.currentItem.forwardPlaybackEndTime = CMTimeMakeWithSeconds(endTime, timeScale);
[player seekToTime:CMTimeMakeWithSeconds(startTime, timeScale)
toleranceBefore:kCMTimeZero
toleranceAfter:kCMTimeZero
completionHandler:^(BOOL finished) {
if (finished) {
[self.player play];
}
}];
I use AVMutableComposition for compilation preview, because I need single AVPlayerItem for GPUImage. My code:
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
for (Video *video in videos) {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:video.fileURL] options:nil];
int32_t timeScale = asset.duration.timescale;
CMTime startTime = CMTimeMakeWithSeconds(video.startTime, timeScale);
CMTime duration = CMTimeMakeWithSeconds(
(video.endTime > 0 ? video.endTime : video.duration) - video.startTime,
timeScale);
[composition insertTimeRange:CMTimeRangeMake(startTime, duration)
ofAsset:asset
atTime:composition.duration error:nil];
}
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:[composition copy]];
I see different ranges of videos on a screen. Where am I wrong?