ios无尽的录像 [英] ios endless video recording

查看:202
本文介绍了ios无尽的录像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个iPhone应用程序,使用相机只记录最后几分钟/秒。
例如,您录制一段电影5分钟,单击保存,只有最后30秒将被保存。我不想实际记录五分钟,然后剁最后30秒(这不会为我工作)。这个想法名为循环录制



此结果在一个无尽的录像,但你记得只有最后一部分。
Precorder应用程序做我想做的。 (我想在其他上下文中使用此功能)
我认为这应该很容易模拟与循环缓冲区
我开始一个项目与AVFoundation。这将是真棒,如果我能以某种方式将视频数据重定向到一个循环缓冲区(我将实现)。



我知道我可以将视频分割成若干段并保存,但保存并重新启动相机以记录另一部分将需要时间,可能会失去电影中的一些重要时刻。



任何线索如何从相机重定向数据。

$ b $从iOS 8开始,您可以使用VTCompressionSession,并且可以直接访问NAL单元。






幸运的是,你可以做到这一点,我会告诉你如何,但是,你将不得不让你的手脏MP4或MOV容器。一个有用的资源(虽然,更多MOV特定)是苹果的Quicktime文件格式简介手册
http://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFPreface/qtffPreface .html#// apple_ref / doc / uid / TP40000939-CH202-TPXREF101



首先,你不能启动从录制结束前30秒的任意点保存电影,您必须在大约30秒钟使用一些I-Frame。根据您的关键帧间隔,它可能在该30秒标记之前或之后几秒钟。你可以使用所有的I帧,从任意点开始,但之后你可能想重新编码视频,因为它将是相当大。



我们知道,让我们继续。



第一步是设置AVAssetWriter时,您需要将其AVAssetWriterInput的expectsMediaDataInRealTime属性设置为YES。



在captureOutput回调中,您可以从写入的文件中执行fread。第一个fread会给你一点点的MP4 / MOV(无论你使用什么格式)头(即'ftyp'原子,'宽'原子和'mdat'原子的开始)。你想要'mdat'部分里面的内容。因此,您开始保存数据的偏移将为36左右。



每次读取都会得到0个或更多的AVC NAL单位。您可以在ISO / IEC 14496-10表7-1中找到NAL单元类型的列表。它们的格式与附录B中的格式略有不同,但是很好。另外,在MP4 / MOV文件中将只有IDR切片和非IDR切片。



MP4 / MOV容器中的NAL单元格式如下:

  4字节 - 大小
[Size]字节 - NALU数据
data [0] 0x1F - NALU类型

现在你有你要找的数据。当你去保存这个文件,你必须更新MPV / MOV容器与正确的长度,样本计数,你必须更新的stsz原子与每个样本的正确的大小和事情,如更新媒体标题和具有正确的电影持续时间的轨道标题等等。我可能建议做的是在第一次运行时创建一个样例容器,你可以或多或少地使用该特定电影的适当数据覆盖/增加。你会想这样做,因为各种iDevices上的编码器不都有相同的设置,'avcC'原子包含编码器信息。



在这种情况下,真的需要了解很多关于AVC流,所以你可能想集中你的实验更新你正确选择的容器格式。祝你好运。


I'm trying to develop an iPhone app that will use the camera to record only the last few minutes/seconds. For example, you record some movie for 5 minutes click "save", and only the last 30s will be saved. I don't want to actually record five minutes and then chop last 30s (this wont work for me). This idea is called "Loop recording".

This results in an endless video recording, but you remember only last part. Precorder app do what I want to do. (I want use this feature in other context) I think this should be easily simulated with a Circular buffer. I started a project with AVFoundation. It would be awesome if I could somehow redirect video data to a circular buffer (which I will implement). I found information only on how to write it to a file.

I know I can chop video into intervals and save them, but saving it and restarting camera to record another part will take time and it is possible to lose some important moments in the movie.

Any clues how to redirect data from camera would be appreciated.

解决方案

Important! As of iOS 8 you can use VTCompressionSession and have direct access to the NAL units instead of having to dig through the container.


Well luckily you can do this and I'll tell you how, but you're going to have to get your hands dirty with either the MP4 or MOV container. A helpful resource for this (though, more MOV-specific) is Apple's Quicktime File Format Introduction manual http://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFPreface/qtffPreface.html#//apple_ref/doc/uid/TP40000939-CH202-TPXREF101

First thing's first, you're not going to be able to start your saved movie from an arbitrary point 30 seconds before the end of the recording, you'll have to use some I-Frame at approximately 30 seconds. Depending on what your Keyframe Interval is, it may be several seconds before or after that 30 second mark. You could use all I-frames and start from an arbitrary point, but then you'll probably want to re-encode the video afterward because it will be quite large.

SO knowing that, let's move on.

First step is when you set up your AVAssetWriter, you will want to set its AVAssetWriterInput's expectsMediaDataInRealTime property to YES.

In the captureOutput callback you'll be able to do an fread from the file you are writing to. The first fread will get you a little bit of MP4/MOV (whatever format you're using) header (i.e. 'ftyp' atom, 'wide' atom, and the beginning of the 'mdat' atom). You want what's inside the 'mdat' section. So the offset you'll start saving data from will be 36 or so.

Each read will get you 0 or more AVC NAL Units. You can find a listing of NAL unit types from ISO/IEC 14496-10 Table 7-1. They will be in a slightly different format than specified in Annex B, but it's fine. Additionally, there will only be IDR slices and non-IDR slices in the MP4/MOV file. IDR will be the I-Frame you're looking to hang onto.

The NAL unit format in the MP4/MOV container is as follows:

4 bytes - Size
[Size] bytes - NALU Data
data[0] & 0x1F - NALU Type

So now you have the data you're looking for. When you go to save this file, you'll have to update the MPV/MOV container with the correct length, sample count, you'll have to update the 'stsz' atom with the correct sizes for each sample and things like updating the media headers and track headers with the correct duration of the movie and so on. What I would probably recommend doing is creating a sample container on first run that you can more or less just overwrite/augment with the appropriate data for that particular movie. You'll want to do this because the encoders on the various iDevices don't all have the same settings and the 'avcC' atom contains encoder information.

You don't really need to know much about the AVC stream in this case, so you'll probably want to concentrate your experimenting around updating the container format you choose correctly. Good luck.

这篇关于ios无尽的录像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆