我正在尝试将CIFilter应用于AVAsset,然后在应用过滤器的情况下保存它.我这样做的方法是使用AVAssetExportSessionwith videoCompositionset设置为AVMutableVideoComposition具有自定义AVVideoCompositing类的对象.
我还将instructions我的AVMutableVideoComposition对象设置为自定义组合指令类(符合AVMutableVideoCompositionInstruction).这个类传递了一个跟踪ID,以及一些其他不重要的变量.
不幸的是,我遇到了一个问题 - startVideoCompositionRequest:我的自定义视频合成器类(符合AVVideoCompositing)中的函数没有被正确调用.
当我将passthroughTrackID自定义指令类的变量设置为轨道ID时,startVideoCompositionRequest(request)我的函数AVVideoCompositing不会被调用.
然而,当我没有设置passthroughTrackID我的自定义指令类的变量,将startVideoCompositionRequest(request) 被调用,但没有正确-打印request.sourceTrackIDs结果空数组,并request.sourceFrameByTrackID(trackID)导致零值.
我发现有趣的是,cancelAllPendingVideoCompositionRequests:在尝试使用过滤器导出视频时,该函数总是被调用两次.startVideoCompositionRequest:在startVideoCompositionRequest:未调用的情况下,它要么被调用一次,要么被调用一次,或者连续两次被调用.
我创建了三个用于导出带过滤器的视频的类.这是实用程序类,它基本上只包含一个export函数并调用所有必需的代码
class VideoFilterExport{
let asset: AVAsset
init(asset: AVAsset){
self.asset = asset
}
func export(toURL url: NSURL, callback: (url: NSURL?) -> Void){
guard let track: AVAssetTrack = self.asset.tracksWithMediaType(AVMediaTypeVideo).first else{callback(url: nil); return}
let composition …Run Code Online (Sandbox Code Playgroud) ios avcomposition avvideocomposition avassetexportsession swift
如果您想自己运行,这是一个完整的项目:https://www.dropbox.com/s/5p384mogjzflvqk/AVPlayerLayerSoundOnlyBug_iOS10.zip?dl=0
这是iOS 10上的一个新问题,从iOS 10.2开始修复.在导出期间使用AVAssetExportSession和AVVideoCompositionCoreAnimationTool导出视频以在视频顶部合成图层后,AVPlayerLayer中播放的视频无法播放.这似乎不是由于达到AV编码/解码流水线限制造成的,因为它经常在单次导出后发生,据我所知只会旋转2个流水线:1为AVAssetExportSession而另一个为AVPlayer.我也正确设置了图层的框架,正如您可以通过运行下面的代码看到的那样,该图层为图层提供了一个您可以清楚看到的蓝色背景.
在导出之后,在播放视频之前等待一段时间似乎使其更加可靠,但这并不是一个可以接受的解决方法来告诉用户.
有关导致此问题或我如何解决或解决问题的任何想法?我搞砸了什么或者错过了一个重要的步骤或细节?任何帮助或文档指针都非常感谢.
import UIKit
import AVFoundation
/* After exporting an AVAsset using AVAssetExportSession with AVVideoCompositionCoreAnimationTool, we
* will attempt to play a video using an AVPlayerLayer with a blue background.
*
* If you see the blue background and hear audio you're experiencing the missing-video bug. Otherwise
* try hitting the button again.
*/
class ViewController: UIViewController {
private var playerLayer: AVPlayerLayer?
private let button = UIButton()
private let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AVFoundation. 这有效。我还尝试向视频添加叠加层,例如包含日期的文本标签。
例如:保存的视频不仅是相机看到的,还有时间戳。
这是我保存视频的方式:
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
saveVideo(toURL: movieURL!)
}
private func saveVideo(toURL url: URL) {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
}) { (success, error) in
if(success) {
print("Video saved to Camera Roll.")
} else {
print("Video failed to save.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个movieOuput是一个AVCaptureMovieFileOutput。我的预览图层不包含任何子图层。我尝试将时间戳标签的图层添加到 previewLayer,但这没有成功。
我已经尝试过Ray Wenderlich 的例子以及这个堆栈溢出问题。最后,我也尝试了这个教程,所有这些都无济于事。
如何为相机胶卷中保存的视频中的视频添加叠加层?
xcode avfoundation video-recording avcapturemoviefileoutput swift