gpt4 book ai didi

ios - 给视频文字添加水印不显示

转载 作者:行者123 更新时间:2023-11-29 00:27:23 25 4
gpt4 key购买 nike

我是 Swift 的新手。我正在制作一个应用程序,可以向视频添加自定义动画和水印等效果。我编写了这段代码来添加水印。问题是显示了水印的矩形,也显示了图像,但没有显示文本。这是代码

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)
})

请帮我看看我做错了什么。我会很感激你的。

最佳答案

我在做自己的申请时注意到了这个问题

我的应用程序的一部分功能是为我的视频添加字幕 我曾尝试使用 CATextLayer 将我的字幕渲染到我的 AVMutableComposition obj。但我也失败了然后我尝试使用容器作为一种桥梁。所以我将我的 CATextLayer 放入 CALayer ,然后将此 CALayer 添加到 animationTool 的子层。

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)

在我的应用程序中,这可以解决问题,但是不渲染 CATextLayer 的原因,我们仍然不知道为什么。

希望能帮上忙


21/3/2017 更新

这是我项目中的代码片段,我使用的是 OC,不是 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];

如上所述,我使用的是 subtitle_container,也就是我提到的 CALayer,并将容器添加到 renderingLayer,也就是 parentLayer.

这是否可能是因为您的水印框架不正确,所以您只渲染容器?我将我的容器的宽度和高度设置为我的输出视频的宽度和高度,所以我有一个全屏容器,之后,调整我的 CATextLayer 变得更加容易。

有什么对你有用的吗?

关于ios - 给视频文字添加水印不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42680225/

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