要在主层和叠加层上使用相同的视频源,可以使用AVMutableComposition
来合并两个视频轨道。
以下是一个使用Swift的示例代码:
import AVFoundation
// 创建主层视频轨道
let mainComposition = AVMutableComposition()
guard let mainVideoTrack = mainComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) else {
fatalError("Failed to create main video track")
}
// 创建叠加层视频轨道
let overlayComposition = AVMutableComposition()
guard let overlayVideoTrack = overlayComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid) else {
fatalError("Failed to create overlay video track")
}
// 获取视频资源
guard let videoURL = Bundle.main.url(forResource: "main_video", withExtension: "mp4") else {
fatalError("Failed to load main video")
}
let videoAsset = AVURLAsset(url: videoURL)
// 将视频资源添加到主层视频轨道
do {
try mainVideoTrack.insertTimeRange(CMTimeRange(start: .zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .video)[0], at: .zero)
} catch {
fatalError("Failed to insert main video track")
}
// 将视频资源添加到叠加层视频轨道
do {
try overlayVideoTrack.insertTimeRange(CMTimeRange(start: .zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .video)[0], at: .zero)
} catch {
fatalError("Failed to insert overlay video track")
}
// 创建视频合成指令
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRange(start: .zero, duration: videoAsset.duration)
// 创建主层视频合成层
let mainLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: mainVideoTrack)
mainInstruction.layerInstructions = [mainLayerInstruction]
// 创建叠加层视频合成层
let overlayLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: overlayVideoTrack)
overlayLayerInstruction.setTransform(CGAffineTransform(scaleX: 0.5, y: 0.5), at: .zero)
overlayLayerInstruction.setOpacity(0.5, at: .zero)
mainInstruction.layerInstructions?.append(overlayLayerInstruction)
// 创建视频合成
let videoComposition = AVMutableVideoComposition()
videoComposition.instructions = [mainInstruction]
videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
videoComposition.renderSize = CGSize(width: mainVideoTrack.naturalSize.width, height: mainVideoTrack.naturalSize.height)
// 导出合成后的视频
guard let exportURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("output.mp4") else {
fatalError("Failed to create export URL")
}
let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)
exporter?.outputURL = exportURL
exporter?.outputFileType = .mp4
exporter?.videoComposition = videoComposition
exporter?.exportAsynchronously(completionHandler: {
switch exporter?.status {
case .completed:
print("Export completed")
case .failed:
print("Export failed: \(exporter?.error)")
case .cancelled:
print("Export cancelled")
default:
break
}
})
这个示例代码假设你有一个名为"main_video.mp4"的视频资源文件。它创建了一个主层和一个叠加层的视频轨道,并将它们添加到AVMutableComposition
中。然后,它创建了一个AVMutableVideoComposition
来控制视频的合成和叠加层效果。最后,它使用AVAssetExportSession
将合成后的视频导出到指定的输出URL。