拼接后视频中的绿色背景
Green background in video after stitching
我正在开发一个应用程序,该应用程序应该记录并将来自相机的录制视频和来自资源的视频粘贴在一起。拼接效果很好,最终视频在设备上播放效果很好。但是当最终视频上传到 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 {}
怀疑这是由于视频参数不同造成的。有谁知道如何解决这个问题?感谢您的帮助 ;)
解决了视频拼接问题。
有必要在视频设置中指定 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 {}
我正在开发一个应用程序,该应用程序应该记录并将来自相机的录制视频和来自资源的视频粘贴在一起。拼接效果很好,最终视频在设备上播放效果很好。但是当最终视频上传到 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 {}
怀疑这是由于视频参数不同造成的。有谁知道如何解决这个问题?感谢您的帮助 ;)
解决了视频拼接问题。 有必要在视频设置中指定 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 {}