Reputation: 103
i am new to Swift. I was making an application that can add custom animations and effects like water mark to the video. I have written this code to add a water mark. The problem is that the rectangle for watermark is displayed and image is also displayed but the text is not shown. Here is the code
let videoAsset = AVURLAsset(url: URL(fileURLWithPath: path!))
let mixComposition: AVMutableComposition = AVMutableComposition()
let mutableTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
let assetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let timeRangeForVideoAsset = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
print("1 ...")
do{
try mutableTrack.insertTimeRange(timeRangeForVideoAsset, of: assetTrack, at: kCMTimeZero)
}catch{
print("Error in adding time range")
}
mutableTrack.preferredTransform = assetTrack.preferredTransform
let imageLayer: CALayer = CALayer()
var im = Bundle.main.path(forResource: "Ground", ofType: "png")
let image = UIImage(contentsOfFile: im!)
print("2 ...")
imageLayer.contents = image?.cgImage
imageLayer.opacity = 0.7
imageLayer.frame = CGRect(x: 0, y: 100, width: self.view.frame.width, height: 50)
let videoSize = assetTrack.naturalSize
let parentLayer = CALayer()
let videoLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: assetTrack.naturalSize.width, height: assetTrack.naturalSize.height)
videoLayer.frame = CGRect(x: 0, y: 0, width: assetTrack.naturalSize.width, height: assetTrack.naturalSize.height)
print("3 ...")
let titleLayer = CATextLayer()
titleLayer.backgroundColor = UIColor.white.cgColor
titleLayer.string = "Dummy text"
titleLayer.foregroundColor = UIColor.black.cgColor
titleLayer.font = UIFont(name: "Helvetica", size: 28)
titleLayer.opacity = 1.0
titleLayer.shadowOpacity = 0.5
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.frame = CGRect(x: 0, y: 50, width: self.view.frame.width, height: 50)
parentLayer.addSublayer(videoLayer)
parentLayer.addSublayer(imageLayer)
parentLayer.addSublayer(titleLayer)
let videoComp = AVMutableVideoComposition()
videoComp.renderSize = videoSize
videoComp.frameDuration = CMTimeMake(1, 30)
videoComp.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer)
print("4 ...")
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration)
var layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: assetTrack)
instruction.layerInstructions = [layerInstruction]
videoComp.instructions = [instruction]
print("5 ...")
let assetExport = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
assetExport?.videoComposition = videoComp
let videoName = "markedVideo.mov"
var exPath = NSTemporaryDirectory().appending(videoName)
if(FileManager.default.fileExists(atPath: exPath)){
do{
try FileManager.default.removeItem(atPath: exPath)
}
catch{
print("File Not Deleted")
}
}
print("6 ...")
assetExport?.outputFileType = AVFileTypeQuickTimeMovie
assetExport?.outputURL = URL(fileURLWithPath: exPath)
assetExport?.shouldOptimizeForNetworkUse = true
assetExport?.exportAsynchronously(completionHandler: {
print("Success ...")
self.playWithMPKit(path: exPath)
})
Please help me what i am doing wrong. I will be thankful to you.
Upvotes: 0
Views: 435
Reputation: 357
i have notice this issue when i was making my own application
there is a part of function of my application which is adding subtitles to my videos i was tried to using CATextLayer to rendering my subtitles to my AVMutableComposition obj.but i was failed too then i was tried to using an container as kind of bridge.so i was putting my CATextLayer in to CALayer , then adding this CALayer to animationTool's sublayer.
let subtitle = CATextLayer()
/*
setup subtitle's properties...
*/
let subtitle_container = CALayer()
/*
setup container's frame/position/rotation/animations<-(such as subtitles fade in / out effects)...
*/
subtitle_container.addSublayer(subtitle)
parentLayer.addSublayer(subtitle_container)
in my application, this will do the trick, but the reason of not rendering CATextLayer,well still have no idea why.
wish could help
This is pieces of code in my project, i was using OC not swift.
RAGE_SubtitleModel * m = subtitleModelArray[i];
// ^this model contained the frame of subtitle, start time , end time.
CGFloat subtitle_startTime = CMTimeGetSeconds(m.timeMapping.target.start);
CGFloat subtitle_duration = CMTimeGetSeconds(m.timeMapping.target.duration);
CALayer * subtitle_container = [CALayer layer];
CATextLayer * subtitle_layer = [CATextLayer layer];
// Font
[subtitle_layer setFont:(__bridge CFTypeRef _Nullable)(m.font.fontName)];
// Font Size
[subtitle_layer setFontSize:m.fontSize * MAX(ratioW, ratioH)];
// Background Color
[subtitle_layer setBackgroundColor:[UIColor clearColor].CGColor];
// Subtitle Color
[subtitle_layer setForegroundColor:m.color.CGColor];
// Subtitle Content
[subtitle_layer setString:m.string];
// Subtitle Alignment Type
[subtitle_layer setAlignmentMode:kCAAlignmentLeft];
[subtitle_layer setContentsScale:SCREEN_SCALE];
// Subtitle Position
[subtitle_container setFrame:CGRectMake(0,
0,
targetSize.width,
targetSize.height)];
[subtitle_layer setFrame:CGRectMake(0, 0, m.frame.size.width * ratioW, m.frame.size.height * ratioH)];
[subtitle_layer setAnchorPoint:CGPointMake(.5,.5)];
[subtitle_layer setPosition:CGPointMake(m.frame.origin.x * ratioW + m.frame.size.width * ratioW * .5f,
targetSize.height - m.frame.origin.y * ratioH - m.frame.size.height * ratioH * .5f)];
[subtitle_container addSublayer:subtitle_layer];
[renderingLayer addSublayer:subtitle_container];
as above , i was using subtitle_container
which is CALayer
i mentioned, and adding container to renderingLayer
which is parentLayer
.
is this possible because your watermark's frame was incorrect, so you are rendering only container? i was setting my container's width & height to my output video's width & height ,so i had an full screen container, after that, adjusting my CATextLayer become much more easier.
Anything useful to you?
Upvotes: 1