我需要修剪很多视频,然后显示他们的编辑预览。 用户可以编辑每个视频的开始时间和结束时间,并使用代码显示当前视频的预览(当用户修剪时):
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]];
我在屏幕上看到不同范围的视频。我哪里错了?
答案 0 :(得分:0)
您必须从AVMutableComposition获取最新的timeRange
,并在范围的末尾插入新的AVAset:
self.mutableComposition.tracks.last?.timeRange.end
这将返回您的AVMutableComposition持续时间,以及如果您想添加新用途:
try mutableComposition.insertTimeRange(CMTimeRange(start: .zero, duration: media.duration), of: media, at: duration ?? .zero)