我需要修剪许多视频,然后显示其编辑的预览。用户可以编辑每个视频的开始时间和结束时间,我使用代码显示当前视频的预览(在用户修剪时):
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];
}
}];
我使用 AVMutableComposition 进行编译预览,因为我需要单个 AVPlayerItem 用于 GPUImage。我的代码:
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]];
我在屏幕上看到不同范围的视频。我哪里错了?