gpt4 book ai didi

ios - 视频swift中的图像/文本叠加

转载 作者:IT王子 更新时间:2023-10-29 05:51:04 25 4
gpt4 key购买 nike

我正在使用 swift 处理视频中的图像叠加以实现水印效果。我正在为此使用 AVFoundation 但不知何故我没有成功。

以下是我的叠加图像/文本代码

    let path = NSBundle.mainBundle().pathForResource("sample_movie", ofType:"mp4")
let fileURL = NSURL(fileURLWithPath: path!)

let composition = AVMutableComposition()
var vidAsset = AVURLAsset(URL: fileURL, options: nil)

// get video track
let vtrack = vidAsset.tracksWithMediaType(AVMediaTypeVideo)
let videoTrack:AVAssetTrack = vtrack[0] as! AVAssetTrack
let vid_duration = videoTrack.timeRange.duration
let vid_timerange = CMTimeRangeMake(kCMTimeZero, vidAsset.duration)

var error: NSError?
let compositionvideoTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
compositionvideoTrack.insertTimeRange(vid_timerange, ofTrack: videoTrack, atTime: kCMTimeZero, error: &error)

compositionvideoTrack.preferredTransform = videoTrack.preferredTransform

// Watermark Effect
let size = videoTrack.naturalSize

let imglogo = UIImage(named: "image.png")
let imglayer = CALayer()
imglayer.contents = imglogo?.CGImage
imglayer.frame = CGRectMake(5, 5, 100, 100)
imglayer.opacity = 0.6

// create text Layer
let titleLayer = CATextLayer()
titleLayer.backgroundColor = UIColor.whiteColor().CGColor
titleLayer.string = "Dummy text"
titleLayer.font = UIFont(name: "Helvetica", size: 28)
titleLayer.shadowOpacity = 0.5
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.frame = CGRectMake(0, 50, size.width, size.height / 6)

let videolayer = CALayer()
videolayer.frame = CGRectMake(0, 0, size.width, size.height)

let parentlayer = CALayer()
parentlayer.frame = CGRectMake(0, 0, size.width, size.height)
parentlayer.addSublayer(videolayer)
parentlayer.addSublayer(imglayer)
parentlayer.addSublayer(titleLayer)

let layercomposition = AVMutableVideoComposition()
layercomposition.frameDuration = CMTimeMake(1, 30)
layercomposition.renderSize = size
layercomposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videolayer, inLayer: parentlayer)

// instruction for watermark
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
let videotrack = composition.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack
let layerinstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videotrack)
instruction.layerInstructions = NSArray(object: layerinstruction) as [AnyObject]
layercomposition.instructions = NSArray(object: instruction) as [AnyObject]

// create new file to receive data
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir: AnyObject = dirPaths[0]
let movieFilePath = docsDir.stringByAppendingPathComponent("result.mov")
let movieDestinationUrl = NSURL(fileURLWithPath: movieFilePath)

// use AVAssetExportSession to export video
let assetExport = AVAssetExportSession(asset: composition, presetName:AVAssetExportPresetHighestQuality)
assetExport.outputFileType = AVFileTypeQuickTimeMovie
assetExport.outputURL = movieDestinationUrl
assetExport.exportAsynchronouslyWithCompletionHandler({
switch assetExport.status{
case AVAssetExportSessionStatus.Failed:
println("failed \(assetExport.error)")
case AVAssetExportSessionStatus.Cancelled:
println("cancelled \(assetExport.error)")
default:
println("Movie complete")


// play video
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.playVideo(movieDestinationUrl!)
})
}
})

通过这段代码,我没有实现覆盖....我不知道我做错了什么...

问题:

  • 此代码中是否缺少任何内容?或者这段代码有什么问题吗?
  • 此代码仅适用于录制的视频还是所有视频,包括图库中的视频?

最佳答案

@El Captain 提供的代码可以工作。它只是缺少:

    assetExport.videoComposition = layercomposition

您可以在AVAssetExportSession

实例化后立即添加它

注意:最初提供的代码只能导出视频轨道,不能导出音频轨道。如果您需要音轨,您可以在配置 compositionvideoTrack 后添加类似这样的内容:

let compositionAudioTrack:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
for audioTrack in audioTracks {
try! compositionAudioTrack.insertTimeRange(audioTrack.timeRange, ofTrack: audioTrack, atTime: kCMTimeZero)
}

关于ios - 视频swift中的图像/文本叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32450356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com