【发布时间】:2016-04-20 12:29:16
【问题描述】:
Illustration of what I'm trying to do
我正在尝试执行以下操作:
- 播放音乐
- 录制方形视频(我在视图中有一个容器,显示您正在录制的内容)
- 在顶部添加标签,在方形视频的左下方添加应用图标和名称。
到目前为止,我设法播放音乐,在不同视图的方形容器中显示 AVCaptureVideoPreviewLayer,并将视频保存到相机胶卷。
问题是我几乎找不到一些关于使用 AVFoundation 的模糊教程,而且这是我的第一个应用程序,这让事情变得相当困难。
我设法做了这些事情,但我仍然不明白 AVFoundation 是如何工作的。该文档对于初学者来说是模糊的,我还没有找到我特别想要的教程,并且将多个教程(并用 Obj C 编写)放在一起使得这变得不可能。我的问题如下:
- 视频未保存为方形。 (提到该应用不支持横向)
- 视频没有音频。 (我认为我应该添加一些除视频之外的音频输入)
- 如何给视频添加水印?
- 我有一个错误:我创建了一个带有文本和图像的视图(messageView;参见代码),让用户知道视频已保存到相机胶卷。但是,如果我第二次开始录制,则视图会在视频录制时出现,而不是在录制后。我怀疑这与为每个视频命名相同有关。
所以我做了准备:
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 有关。但显然我不能这样做,因为我什至不知道如何制作视频正方形并添加音频。
【问题讨论】:
-
您是否要在录制后添加水印 AKA 录制视频,然后编辑并添加水印或在录制过程中添加?
-
我想添加水印后录制。所以用音频录制视频(在 UI 上显示为正方形) -> 完成录制 -> 制作视频正方形 -> 添加水印
-
通过后处理,您需要使用 AVMutableComposition 和 avassetexportsession。有一个 WWDC 会议,但不记得是哪一年,它在视频编辑上
标签: ios swift video avfoundation