ios - AVMutableComposition插入时间范围: uses wrong range

标签 ios avmutablecomposition avplayeritem

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

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

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

最佳答案

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

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

关于ios - AVMutableComposition插入时间范围: uses wrong range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114725/

相关文章:

ios - FMDB更新版本号不起作用

ios - UISplitScreenController 具有非常多(超过 20 个)详细 View

ios - 在 RestKit 中从 json 中的兄弟节点映射对象

ios - AVPlayer/AVPlayerItem 返回播放错误太慢

ios - UIDatePicker 在 "done"按钮按下时获取 .date 属性

ios - 如何在 AVQueuePlayer 或 AVMutableComposition 中播放多个视频而没有任何间隙或卡住?

ios - 仅部分视频的 ScaleTimeRange

ios - 在 AVMutableComposition 中渲染不同的分辨率

ios - 使用 automaticallyLoadedAssetKeys 和 "duration"初始化 AVPlayerItem

ios - 如何在 iOS swift 中使用 AVPlayer 访问 iOS 文件应用程序中存在的视频文件?