gpt4 book ai didi

ios - 将文本字幕添加到视频轨道(在 Swift 中)失败,错误代码为 -11841

转载 作者:可可西里 更新时间:2023-11-01 02:20:03 25 4
gpt4 key购买 nike

一段时间以来,我一直在努力为视频添加文本字幕。我添加了一些我详细提到的链接,但它们没有帮助。

在下面的代码中,我试图为视频添加字幕。输出文件路径如下:

file:///var/mobile/Applications/03E49B29-1070-4541-B7CB-B1366732C179/Documents/output_movie.mov

此外,输入文件是通过在同一应用程序中调用 UIPickerView 记录的,位于以下临时路径中:

file:///private/var/mobile/Applications/03E49B29-1070-4541-B7CB-B1366732C179/tmp/capture/capturedvideo.MOV

我得到的错误如下,

Error:-
Domain=AVFoundationErrorDomain Code=-11841 "Operation Stopped" UserInfo=0x15ebcfb0 {NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The video could not be composed.}

Description:-
<AVAssetExportSession: 0x15d97c80, asset = <AVMutableComposition: 0x15d788d0 tracks = ("<AVMutableCompositionTrack: 0x15d86910 trackID = 1, mediaType = vide, editCount = 1>")>, presetName = AVAssetExportPresetHighestQuality, outputFileType = com.apple.quicktime-movie

Completed merging the video with status code 4

我使用的代码如下。我在 iPhone 4s 的 iOS 7.1.2 上运行它。

class func mergeVideoWithTheme(outputUrl: NSURL, inputVideoUrl videoUrl: NSURL!, onComplete completionHandler: ((Int) -> ())!) -> Void {

// 1. mergeComposition adds all the AVAssets

var mergeComposition : AVMutableComposition = AVMutableComposition()
var trackVideo : AVMutableCompositionTrack = mergeComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
//var trackAudio : AVMutableCompositionTrack = mergeComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

// 2. Add a bank for theme insertion later

//trackVideo.insertTimeRange(range, ofTrack: VideoHelper.Static.blankTrack, atTime: kCMTimeZero, error: nil)

// 3. Source tracks

let sourceAsset = AVURLAsset(URL: videoUrl, options: nil)
let sourceDuration = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration)
let vtrack: AVAssetTrack? = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as? AVAssetTrack
let atrack: AVAssetTrack? = sourceAsset.tracksWithMediaType(AVMediaTypeAudio)[0] as? AVAssetTrack

if (vtrack == nil) {
return
}

let renderWidth = vtrack?.naturalSize.width
let renderHeight = vtrack?.naturalSize.height
let insertTime = kCMTimeZero
let endTime = sourceAsset.duration
let range = sourceDuration

// append tracks

trackVideo.insertTimeRange(sourceDuration, ofTrack: vtrack, atTime: insertTime, error: nil)
//if(atrack > 0){
// trackAudio.insertTimeRange(sourceDuration, ofTrack: atracks[0] as AVAssetTrack, atTime: insertTime, error: nil)
//}

// 4. Add subtitles (we call it theme)

var themeVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition(propertiesOfAsset: sourceAsset)

// 4.1 - Create AVMutableVideoCompositionInstruction

let mainInstruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = range

// 4.2 - Create an AVMutableVideoCompositionLayerInstruction for the video track and fix the orientation.

let videolayerInstruction : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction()
videolayerInstruction.setTransform(trackVideo.preferredTransform, atTime: insertTime)
videolayerInstruction.setOpacity(0.0, atTime: endTime)

// 4.3 - Add instructions

mainInstruction.layerInstructions = NSArray(array: [videolayerInstruction])

themeVideoComposition.renderScale = 1.0
themeVideoComposition.renderSize = CGSizeMake(renderWidth!, renderHeight!)
themeVideoComposition.frameDuration = CMTimeMake(1, 30)
themeVideoComposition.instructions = NSArray(array: [mainInstruction])

// add the theme

// setup variables

// add text

let title = String("Testing this subtitle")

var titleLayer = CATextLayer()
titleLayer.string = title
titleLayer.frame = CGRect(x: 0, y: 0, width: renderWidth!, height: renderHeight!)
let fontName: CFStringRef = "Helvetica-Bold"
let fontSize = CGFloat(36)
titleLayer.font = CTFontCreateWithName(fontName, fontSize, nil)
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.foregroundColor = UIColor.whiteColor().CGColor

var backgroundLayer = CALayer()
backgroundLayer.frame = CGRect(x: 0, y: 0, width: renderWidth!, height: renderHeight!)
backgroundLayer.masksToBounds = true
backgroundLayer.addSublayer(titleLayer)

// 2. set parent layer and video layer

var parentLayer = CALayer()
var videoLayer = CALayer()
parentLayer.frame = CGRect(x: 0, y: 0, width: renderWidth!, height: renderHeight!)
videoLayer.frame = CGRect(x: 0, y: 0, width: renderWidth!, height: renderHeight!)

parentLayer.addSublayer(backgroundLayer)
parentLayer.addSublayer(videoLayer)

// 3. make animation

themeVideoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)

// Remove the file if it already exists (merger does not overwrite)

let fileManager = NSFileManager.defaultManager()
fileManager.removeItemAtURL(outputUrl, error: nil)

// export to output url

var exporter = AVAssetExportSession(asset: mergeComposition, presetName: AVAssetExportPresetHighestQuality)
exporter.outputURL = outputUrl
exporter.videoComposition = themeVideoComposition
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.shouldOptimizeForNetworkUse = true
exporter.exportAsynchronouslyWithCompletionHandler({
if (exporter.error != nil) {
println("Error")
println(exporter.error)
println("Description")
println(exporter.description)
}
completionHandler(exporter.status.rawValue)
})
}

非常感谢您的帮助。我没有找到任何向视频添加动画的快速示例。想知道它是否适用于 swift 上的任何人。

引用文献:(1) https://gist.github.com/SheffieldKevin/c01789ccff2b2a87f5ea (2) http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

最佳答案

创建图层指令时插入 trackID 失败。尝试:

let videolayerInstruction : AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(vtrack)

关于ios - 将文本字幕添加到视频轨道(在 Swift 中)失败,错误代码为 -11841,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340247/

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