【发布时间】:2019-09-11 15:58:15
【问题描述】:
我正在开发一个应用程序,该应用程序应该录制并粘贴来自相机的录制视频和来自资源的视频。拼接效果很好,最终视频在设备上播放良好。但是,当最终视频上传到 Facebook 或 YouTube 时,在那里播放视频时会出现绿色和灰色背景。此背景出现在录制的视频结束和资源中的视频开始的地方。
我使用 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