所以我正在处理这个视频编辑器项目,我遇到了获取trackID的问题 我搜索了我所知道的每一个地方,我所能找到的只是一些客观的代码,我无法将其转换为快速的
我在这行中收到错误:
layer?.trackID = videoAsset?.tracksWithMediaType(AVMediaTypeVideo)
错误是
无法分配此表达式的结果
这是我的代码:
import UIKit
import MobileCoreServices
import AVFoundation
import AVKit
class VideoMakerViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIVideoEditorControllerDelegate {
var videoAsset: AVAsset? = nil
var videoEditor: AVMutableVideoComposition? = nil
var videoEditorInstroctions: AVVideoCompositionInstruction? = nil
var layer: AVVideoCompositionLayerInstruction? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func chooseVideo(sender: AnyObject) {
var videoPicker = UIImagePickerController()
videoPicker.delegate = self
videoPicker.sourceType = .PhotoLibrary
videoPicker.mediaTypes = [kUTTypeMovie]
videoPicker.allowsEditing = false
presentViewController(videoPicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
let videoName = url.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let localPath = documentDirectory.stringByAppendingPathComponent(videoName)
println(videoName)
videoAsset = AVURLAsset(URL: url, options: nil)
videoEditorInstroctions?.backgroundColor = UIColor.blueColor().CGColor
layer?.trackID = videoAsset?.tracksWithMediaType(AVMediaTypeVideo)
videoEditor = AVMutableVideoComposition(propertiesOfAsset: videoAsset)
videoEditor?.frameDuration = CMTimeMake(60, 30)
videoEditor?.renderSize = CGSize(width: 600, height: 1000)
videoEditor?.renderScale = Float(1)
println(videoAsset?.duration)
}
}
答案 0 :(得分:0)
首先:trackID
是只读的。为了能够为其分配任何内容,您必须使用AVMutableVideoCompositionLayerInstruction
而不是AVVideoCompositionLayerInstruction
。
第二:tracksWithMediaType
返回
显示mediaType媒体的资产
AVAssetTrack
对象数组。
trackID
的类型为CMPersistentTrackID
。因此,您无法将其中一个分配给另一个。
但是,您可以/必须从返回数组中的任何对象中检索您要查找的ID:
layer?.trackID = (videoAsset?.tracksWithMediaType(AVMediaTypeVideo)[0].trackID)!
或
layer?.trackID = videoAsset!.tracksWithMediaType(AVMediaTypeVideo)[0].trackID