我正在尝试做的插图
我正在尝试执行以下操作:
到目前为止,我设法播放音乐,在另一个视图中的方形容器中显示AVCaptureVideoPreviewLayer并将视频保存到相机胶卷中。
事实是,我几乎找不到关于使用AVFoundation的模糊教程,这是我的第一个应用程序,这使工作变得非常困难。
我设法做到了这些,但是我仍然不了解AVFoundation的工作方式。该文档对于初学者来说含糊不清,而且我还没有找到适合自己特定需求的教程,而将多个教程(用Obj
C编写)组合在一起就使这一切变得不可能。我的问题如下:
所以我做了准备:
override func viewDidLoad() {
super.viewDidLoad()
// Preset For High Quality
captureSession.sessionPreset = AVCaptureSessionPresetHigh
// Get available devices capable of recording video
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]
// Get back camera
for device in devices
{
if device.position == AVCaptureDevicePosition.Back
{
currentDevice = device
}
}
// Set Input
let captureDeviceInput: AVCaptureDeviceInput
do
{
captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice)
}
catch
{
print(error)
return
}
// Set Output
videoFileOutput = AVCaptureMovieFileOutput()
// Configure Session w/ Input & Output Devices
captureSession.addInput(captureDeviceInput)
captureSession.addOutput(videoFileOutput)
// Show Camera Preview
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
let width = view.bounds.width*0.85
cameraPreviewLayer?.frame = CGRectMake(0, 0, width, width)
// Bring Record Button To Front
view.bringSubviewToFront(recordButton)
captureSession.startRunning()
// // Bring Message To Front
// view.bringSubviewToFront(messageView)
// view.bringSubviewToFront(messageText)
// view.bringSubviewToFront(messageImage)
}
然后,当我按下“录制”按钮时:
@IBAction func capture(sender: AnyObject) {
if !isRecording
{
isRecording = true
UIView.animateWithDuration(0.5, delay: 0.0, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations: { () -> Void in
self.recordButton.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion: nil)
let outputPath = NSTemporaryDirectory() + "output.mov"
let outputFileURL = NSURL(fileURLWithPath: outputPath)
videoFileOutput?.startRecordingToOutputFileURL(outputFileURL, recordingDelegate: self)
}
else
{
isRecording = false
UIView.animateWithDuration(0.5, delay: 0, options: [], animations: { () -> Void in
self.recordButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
}, completion: nil)
recordButton.layer.removeAllAnimations()
videoFileOutput?.stopRecording()
}
}
在录制视频之后:
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
let outputPath = NSTemporaryDirectory() + "output.mov"
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(outputPath)
{
UISaveVideoAtPathToSavedPhotosAlbum(outputPath, self, nil, nil)
// Show Success Message
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: {
self.messageView.alpha = 0.8
}, completion: nil)
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: {
self.messageText.alpha = 1.0
}, completion: nil)
UIView.animateWithDuration(0.4, delay: 0, options: [], animations: {
self.messageImage.alpha = 1.0
}, completion: nil)
// Hide Message
UIView.animateWithDuration(0.4, delay: 1, options: [], animations: {
self.messageView.alpha = 0
}, completion: nil)
UIView.animateWithDuration(0.4, delay: 1, options: [], animations: {
self.messageText.alpha = 0
}, completion: nil)
UIView.animateWithDuration(0.4, delay: 1, options: [], animations: {
self.messageImage.alpha = 0
}, completion: nil)
}
}
那我该怎么解决呢?我一直在搜索和查看教程,但我想不通…我读了有关添加水印的信息,我发现这与在视频顶部添加CALayers有关。但是显然我无法做到这一点,因为我什至不知道如何使视频变得方形并添加音频。
一些东西:
就音频而言,您要添加视频(摄像机)输入,但不添加音频输入。这样做以获得声音。
let audioInputDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
do {
let input = try AVCaptureDeviceInput(device: audioInputDevice)
if sourceAVFoundation.captureSession.canAddInput(input) {
sourceAVFoundation.captureSession.addInput(input)
} else {
NSLog("ERROR: Can't add audio input")
}
} catch let error {
NSLog("ERROR: Getting input device: \(error)")
}
为了使视频更加方形,您将不得不使用AVAssetWriter而不是AVCaptureFileOutput。这更加复杂,但是您获得了更多的“力量”。您已经创建了一个AVCaptureSession,它非常棒,要连接AssetWriter,您需要执行以下操作:
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
guard let documentDirectory: NSURL = urls.first else {
print("Video Controller: getAssetWriter: documentDir Error")
return nil
}
let local_video_name = NSUUID().UUIDString + ".mp4"
self.videoOutputURL = documentDirectory.URLByAppendingPathComponent(local_video_name)
guard let url = self.videoOutputURL else {
return nil
}
self.assetWriter = try? AVAssetWriter(URL: url, fileType: AVFileTypeMPEG4)
guard let writer = self.assetWriter else {
return nil
}
//TODO: Set your desired video size here!
let videoSettings: [String : AnyObject] = [
AVVideoCodecKey : AVVideoCodecH264,
AVVideoWidthKey : captureSize.width,
AVVideoHeightKey : captureSize.height,
AVVideoCompressionPropertiesKey : [
AVVideoAverageBitRateKey : 200000,
AVVideoProfileLevelKey : AVVideoProfileLevelH264Baseline41,
AVVideoMaxKeyFrameIntervalKey : 90,
],
]
assetWriterInputCamera = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)
assetWriterInputCamera?.expectsMediaDataInRealTime = true
writer.addInput(assetWriterInputCamera!)
let audioSettings : [String : AnyObject] = [
AVFormatIDKey : NSInteger(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey : 2,
AVSampleRateKey : NSNumber(double: 44100.0)
]
assetWriterInputAudio = AVAssetWriterInput(mediaType: AVMediaTypeAudio, outputSettings: audioSettings)
assetWriterInputAudio?.expectsMediaDataInRealTime = true
writer.addInput(assetWriterInputAudio!)
设置好AssetWriter之后,然后为视频和音频连接一些输出
let bufferAudioQueue = dispatch_queue_create("audio buffer delegate", DISPATCH_QUEUE_SERIAL)
let audioOutput = AVCaptureAudioDataOutput()
audioOutput.setSampleBufferDelegate(self, queue: bufferAudioQueue)
captureSession.addOutput(audioOutput)
// Always add video last...
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: bufferVideoQueue)
captureSession.addOutput(videoOutput)
if let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo) {
if connection.supportsVideoOrientation {
// Force recording to portrait
connection.videoOrientation = AVCaptureVideoOrientation.Portrait
}
self.outputConnection = connection
}
captureSession.startRunning()
最后,您需要捕获缓冲区并处理这些东西…确保您使类成为AVCaptureVideoDataOutputSampleBufferDelegate和AVCaptureAudioDataOutputSampleBufferDelegate的委托
//MARK: Implementation for AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
if !self.isRecordingStarted {
return
}
if let audio = self.assetWriterInputAudio where connection.audioChannels.count > 0 && audio.readyForMoreMediaData {
dispatch_async(audioQueue!) {
audio.appendSampleBuffer(sampleBuffer)
}
return
}
if let camera = self.assetWriterInputCamera where camera.readyForMoreMediaData {
dispatch_async(videoQueue!) {
camera.appendSampleBuffer(sampleBuffer)
}
}
}
缺少一些零碎的部分,但希望这足以让您连同文档一起弄清楚。
最后,如果要添加水印,可以通过多种方式实时完成,但一种可能的方式是修改sampleBuffer并将水印写入图像。您会在StackOverflow上找到其他与此相关的问题。
问题内容: 在AVFoundation中是否有相对简单的循环视频的方法? 我已经如下创建了我的AVPlayer和AVPlayerLayer: 然后播放以下视频: 视频播放正常,但在最后停止。使用MPMoviePlayerController,您所要做的就是将其属性设置为正确的值。在AVPlayer上似乎没有类似的属性。似乎也没有回调可以告诉我电影何时结束,因此我可以寻找开始并再次播放。 我没有使用
可以在您将要上传的视频每一帧打上水印,以便版权保护。 进入视频水印页面,可以进行水印编辑。 注:只对水印开启后上传的视频生效。 编辑水印页面:
我正在尝试开发一个应用程序,允许我在录制视频时绘制视频,然后将录制的视频和视频保存在一个mp4文件中供以后使用。另外,我想使用camera2库,特别是我需要我的应用程序在高于API 21的设备上运行,我总是避免使用不推荐的库。 我尝试了很多方法,包括FFmpeg,其中我放置了TextureView的覆盖层。getBitmap()(来自摄影机)和从画布获取的位图。它工作正常,但由于它的功能很慢,视频
最近在glassdoor-like网站上遇到了一个面试问题,我找不到一个优化的解决方案来解决这个问题: 这和积水问题完全不同。请通读这些例子。 给定一个输入数组,每个元素表示塔的高度,将浇水量,索引号表示浇水位置。每个塔的宽度为1。浇水后打印图表。 备注: > 浇注位置永远不会处于峰值位置。无需考虑分水的情况。 (如果您给出了这种情况的解决方案,您可以假设如果在峰值位置倒入N水,N/2水向左,N/
我正在使用这个示例(https://github.com/google-ar/arcore-android-sdk/tree/master/samples/hello_ar_java),我想提供使用放置的AR对象录制视频的功能。 我尝试了很多事情,但都没有成功,有没有推荐的方法?
DPlayer视频水印 当前用的是DPlayer视频播放器,我们如何对视频增加水印,求解答