【问题标题】:Green background in video after stitching拼接后视频中的绿色背景
【发布时间】:2019-09-11 15:58:15
【问题描述】:

我正在开发一个应用程序,该应用程序应该录制并粘贴来自相机的录制视频和来自资源的视频。拼接效果很好,最终视频在设备上播放良好。但是,当最终视频上传到 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 {}

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

【问题讨论】:

    标签: ios swift video video-editing


    【解决方案1】:

    解决了视频拼接的问题。 需要在视频设置中指定 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 {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多