VideoFlint / Cabbage

A video composition framework build on top of AVFoundation. It's simple to use and easy to extend.
MIT License
1.52k stars 221 forks source link

通过 Images 创建 AVPlayerItem,视频后半段变黑屏了 #1

Closed NeverAgain11 closed 5 years ago

NeverAgain11 commented 5 years ago

DEBUG 后发现 VideoCompositionInstruction 的方法 open func apply(request: AVAsynchronousVideoCompositionRequest) -> CIImage? 在 16 秒后 requestsourceTrackIDsnil,调用 sourceFrame(byTrackID trackID: CMPersistentTrackID) 也返回 nil。

求助问题原因所在。

下面是我通过 images 创建 AVPlayerItem 的代码:

func makePlayerItemFromImages(_ images: [UIImage]) {
        let ciImages = images.compactMap { $0.cgImage }.map { CIImage.init(cgImage: $0) }

        let resources = ciImages.map { ImageResource(image: $0) }

        for item in resources {
            let timeRange = CMTimeRange(start: kCMTimeZero, duration: CMTimeMake(100, 100))
            item.duration = timeRange.duration
            item.selectedTimeRange = timeRange
        }

        let items = resources.map { TrackItem(resource: $0) }

        var timeLineDuration = kCMTimeZero
        items.forEach {
            $0.configuration.videoConfiguration.baseContentMode = .aspectFit

            let timeRange = CMTimeRange(start: timeLineDuration, duration: $0.resource.duration)
            $0.configuration.timelineTimeRange = timeRange
            timeLineDuration = CMTimeAdd(timeLineDuration, timeRange.duration)

        }

        let timeline = Timeline()
        timeline.videoChannel = items

        let compositionGenerator = CompositionGenerator(timeline: timeline)
        compositionGenerator.renderSize = CGSize(width: 480, height: 480)
        let playerItem = compositionGenerator.buildPlayerItem()

        let controller = AVPlayerViewController.init()
        controller.player = AVPlayer.init(playerItem: playerItem)
        controller.view.backgroundColor = UIColor.white
        present(controller, animated: true, completion: nil)
    }
vitoziv commented 5 years ago

Hum.... 似乎是大于 16 个 track,从第 17 个开始,就没有显示了。奇怪的问题,我看看哪里是不是有未知的坑

vitoziv commented 5 years ago

已修复,可用最新 commit 试试

NeverAgain11 commented 5 years ago

嗯,问题解决了

NeverAgain11 commented 5 years ago

我看了代码,只对 timeLinevideoChannelaudioChannel 修复了这个问题。 timeLineoverlays 应该用于表情之类的内容,如果添加的数量过多的话,track 的数量应该也有这个问题吧。

vitoziv commented 5 years ago

overlays 的确有数量过多的问题,

添加表情、贴纸或者文字之类的可以实现 Timeline 里的 passingThroughVideoCompositionProvider 属性,根据播放时间点,显示对应的内容。

overlays 主要用于支持原生的多个视频显示,不过目前看来 AVFoundation 的限制,无法添加超过 16 个。