0

我需要修剪许多视频,然后显示其编辑的预览。用户可以编辑每个视频的开始时间和结束时间,我使用代码显示当前视频的预览(在用户修剪时):

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]];

我在屏幕上看到不同范围的视频。我哪里错了?

4

2 回答 2

1

这应该解决在屏幕上看到错误时间的问题,如果您想在更早的时间停止,可以更改结束时间。希望这可以帮助。

CMTime startTime = CMTimeMake((int)(floor([@(video.startTime) floatValue] * 100)), 100);
CMTime stopTime = CMTimeMake((int)(ceil([@(video.duration) floatValue] * 100)), 100);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

    // grab the portion of interest from the master asset WHOLE MASTER TRACK
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, masterAsset.duration)
                        ofTrack:[[masterAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                         atTime:kCMTimeZero
                          error:&error];
于 2015-09-10T11:02:41.397 回答
0

斯威夫特 5.1

您必须timeRange从 AVMutableComposition 获取最新的并在范围末尾插入一个新的 AVAsset:

self.mutableComposition.tracks.last?.timeRange.end

这将返回您的 AVMutableComposition 持续时间,如果您想添加新用途:

try mutableComposition.insertTimeRange(CMTimeRange(start: .zero, duration: media.duration), of: media, at: duration ?? .zero)
于 2020-10-28T14:41:57.420 回答