拼接后视频中的绿色背景

时间:2019-04-22 12:50:31

标签: ios swift video video-editing

我正在开发一个应用程序,该应用程序应该将摄像机录制的视频和资源中的视频录制并粘贴在一起。拼接效果很好,最终视频可以在设备上正常播放。但是,当最终视频上传到Facebook或YouTube时,在那里播放视频时会出现绿色和灰色背景。此背景出现在录制的视频结束且资源中的视频开始的位置。

This is what it looks like:

我使用AVFoundation进行拼接。这是示例代码:

let finalMixComposition : AVMutableComposition = AVMutableComposition()

    var videoCompositionTrack : [AVMutableCompositionTrack] = []
    var audioCompositionTrack : [AVMutableCompositionTrack] = []

    let finalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()

    let renderSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

    var startTime = CMTime.zero

    videoCompositionTrack.append(finalMixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)!)
    audioCompositionTrack.append(finalMixComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)!)

    let arrayVideos : [URL] = [firstVideo, secondVideo, thirdVideo, fourthVideo…]

    for url in arrayVideos {
        let videoAsset : AVAsset = AVAsset(url: url)

        let aVideoAssetTrack = videoAsset.tracks(withMediaType: AVMediaType.video)[0]

        var aAudioAssetTrack = videoAsset.tracks(withMediaType: AVMediaType.audio)[0]

        do {
            try videoCompositionTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration),
                                                         of: aVideoAssetTrack,
                                                         at: startTime)

            try audioCompositionTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration),
                                                            of: aAudioAssetTrack,
                                                            at: startTime)
        }
        catch {
            print("error")
        }

        startTime = CMTime(seconds: startTime.seconds + aVideoAssetTrack.timeRange.duration.seconds)
    }

    finalVideoCompositionInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: startTime)

    let mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
    mutableVideoComposition.frameDuration = CMTimeMake(value: 0, timescale: 60)

    mutableVideoComposition.renderSize = renderSize

    let savePathUrl = tempURL()

    let assetExport: AVAssetExportSession = AVAssetExportSession(asset: finalMixComposition,
                                                                 presetName: AVAssetExportPresetHighestQuality)!
    assetExport.outputFileType = AVFileType.mp4
    assetExport.outputURL = savePathUrl
    assetExport.shouldOptimizeForNetworkUse = true

    assetExport.exportAsynchronously {}

有人怀疑这是由于视频参数不同所致。有谁知道如何解决这个问题?感谢您的帮助;)

1 个答案:

答案 0 :(得分:0)

解决了视频拼接问题。 必须在视频设置中指定BitRate,Codec,ProfileLevel。 对于该解决方案,我找到了一个做到这一点的库:SDAVAssetExportSession

这是示例代码:

let encoder = SDAVAssetExportSession(asset: finalMixComposition)
    encoder!.outputFileType = AVFileType.mp4.rawValue
    encoder!.outputURL = savePathUrl
    encoder!.videoSettings = [AVVideoCodecKey: AVVideoCodecType.h264,
                              AVVideoWidthKey: 1024,
                              AVVideoHeightKey: 768,
                              AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 6000000,
                                                                AVVideoProfileLevelKey: AVVideoProfileLevelH264High40]]

    encoder!.audioSettings = [AVFormatIDKey: kAudioFormatMPEG4AAC,
                              AVNumberOfChannelsKey: 2,
                              AVSampleRateKey: 44100,
                              AVEncoderBitRateKey: 128000]

    encoder!.exportAsynchronously {}