近期一直搞视频模块的学习。苹果也提供了AVFoundation这个强大的库来支持开发。先通过文档来了解下AVFoundation究竟是个多么强大的库。英文水平有限,查了谷歌有道各种翻译,参考了众多大神的博客。后续在慢慢改进~~~ 翻译有误多多指教
AVFoundation is one of several frameworks that you can use to play and create time-based audiovisual media. It provides an Objective-C interface you use to work on a detailed level with time-based audiovisual data. For example, you can use it to examine, create, edit, or reencode media files. You can also get input streams from devices and manipulate video during realtime capture and playback. Figure I-1 shows the architecture on iOS
AVFoundation是一个可以用来播放和创建基于时间的视听媒体的框架。 它提供了一个Objective-C界面,用于使用基于时间的视听数据的详细级别。 例如,您可以使用它来检查,创建,编辑或重新编码媒体文件。 还可以在设备中实时捕获和播放期间从设备获取输入流并操纵视频。下面是iOS上面结构图。
Figure I-2 shows the corresponding media architecture on OS X.
You should typically use the highest-level abstraction available that allows you to perform the tasks you want.
If you simply want to play movies, use the AVKit framework.
On iOS, to record video when you need only minimal control over format, use the UIKit framework (UIImagePickerController).
您应该使用可用的最高级别的抽象接口,执行你想要做的任务。
如果你只是想简单的播放,那就使用 AVKit framework。
在iOS上,如果你录制视频时候只需要用到最少格式设置时,那就可以使用UIKit框架录制视频。(UIImagePickerController).
Note, however, that some of the primitive data structures that you use in AV Foundation—including time-related data structures and opaque objects to carry and describe media data—are declared in the Core Media framework.
但是请注意,某些在AV Foundation 中使用的原始数据结构,包括时间相关的数据结构,传递不透明数据对象,媒体数据描述是在Core Media framework声明的。
At a Glance
There are two facets to the AVFoundation framework—APIs related to video and APIs related just to audio. The older audio-related classes provide easy ways to deal with audio. They are described in the Multimedia Programming Guide, not in this document.
AVFoundation框架包含视频相关的APIs和音频相关的APIs。旧的音频相关类提供了简便的方法来处理音频。他们在Multimedia Programming Guide中介绍,不在这个文档中。
To play sound files, you can use AVAudioPlayer.
To record audio, you can use AVAudioRecorder.
You can also configure the audio behavior of your application using AVAudioSession; this is described in Audio Session Programming Guide.
播放音频文件可以使用 AVAudioPlayer.
播放视频文件可以使用AVAudioRecorder.
还可以通过设置AVAudioSession来配置应用程序的音频。在Audio Session Programming Guide文档中有详细的介绍。
Representing and Using Media with AVFoundation
The primary class that the AV Foundation framework uses to represent media is AVAsset. The design of the framework is largely guided by this representation. Understanding its structure will help you to understand how the framework works. An AVAsset instance is an aggregated representation of a collection of one or more pieces of media data (audio and video tracks). It provides information about the collection as a whole, such as its title, duration, natural presentation size, and so on. AVAsset is not tied to particular data format. AVAsset is the superclass of other classes used to create asset instances from media at a URL (see Using Assets) and to create new compositions (see Editing).
AVAsset.是AV Foundation媒体框架中主要的类。框架的设计主要是由这种表示引导。了解它的结构将有助于您了解该框架是如何工作的。一个AVAsset 实例的媒体数据的一个或更多个(音频和视频轨道)的集合的聚集表示。它规定将有关集合的信息作为一个整体,如它的名称,时间,自然呈现大小等的信息。 AVAsset 是不依赖于特定的数据格式。 AVAsset是常常从URL中的媒体创建资产实例的这种类父类(请参阅 Using Assets),并创造新的成分(见 Editing)。
Each of the individual pieces of media data in the asset is of a uniform type and called a track. In a typical simple case, one track represents the audio component, and another represents the video component; in a complex composition, however, there may be multiple overlapping tracks of audio and video. Assets may also have metadata.
Asset中媒体数据的各个部分,每一个都是一个统一的类型,把这个类型称为“轨道”。在一个典型简单的情况下,一个轨道代表这个音频组件,另一个代表视频组件。然而复杂的组合中,有可能是多个重叠的音频和视频轨道。Assets也可能有元数据。
A vital concept in AV Foundation is that initializing an asset or a track does not necessarily mean that it is ready for use. It may require some time to calculate even the duration of an item (an MP3 file, for example, may not contain summary information). Rather than blocking the current thread while a value is being calculated, you ask for values and get an answer back asynchronously through a callback that you define using a block.
在 AV Foundation 中一个非常重要的概念是:初始化一个 asset 或者一个轨道并不一定意味着它已经准备好可以被使用。这可能需要一些时间来计算一个项目的持续时间(例如一个MP3文件,其中可能不包含摘要信息)。而不是当一个值被计算的时候阻塞当前线程,你访问这个值,并且通过调用你定义的一个 block 来得到异步返回。
相关章节:Using Assets, Time and Media Representations
Playback
AVFoundation allows you to manage the playback of asset in sophisticated ways. To support this, it separates the presentation state of an asset from the asset itself. This allows you to, for example, play two different segments of the same asset at the same time rendered at different resolutions. The presentation state for an asset is managed by a player item object; the presentation state for each track within an asset is managed by a player item track object. Using the player item and player item tracks you can, for example, set the size at which the visual portion of the item is presented by the player, set the audio mix parameters and video composition settings to be applied during playback, or disable components of the asset during playback.
AVFoundation允许你用一种复杂的方式来管理asset的播放。为了支持这一点,它将一个asset的呈现状态从asset自身中分离出来。例如允许你在不同的分辨率下同时播放同一个asset中的两个不同的片段。一个asset的呈现状态是由player item对象管理的。Asset中的每个轨道的呈现状态是由player item track对象管理的。例如使用player item和player item tracks,你可以设置被播放器呈现的项目中可视的那一部分,设置音频的混合参数以及被应用于播放期间的视频组合设定,或者播放期间的禁用组件。
You play player items using a player object, and direct the output of a player to the Core Animation layer. You can use a player queue to schedule playback of a collection of player items in sequence.
你可以使用一个 player 对象来播放播放器项目,并且直接输出一个播放器给核心动画层。你可以使用一个 player queue(player对象的队列)去给队列中player items集合中的播放项目安排序列。
相关章节:Playback
Reading, Writing, and Reencoding Assets
读取,写入以及重新编码Assets
AVFoundation allows you to create new representations of an asset in several ways. You can simply reencode an existing asset, or—in iOS 4.1 and later—you can perform operations on the contents of an asset and save the result as a new asset.
AVFoundation 允许你用几种方式创建新的 asset 的表现形式。你可以简单将已经存在的 asset 重新编码,或者在iOS4.1以及之后的版本中,你可以在一个 asset 的目录中执行一些操作并且将结果保存为一个新的 asset 。
You use an export session to reencode an existing asset into a format defined by one of a small number of commonly-used presets. If you need more control over the transformation, in iOS 4.1 and later you can use an asset reader and asset writer object in tandem to convert an asset from one representation to another. Using these objects you can, for example, choose which of the tracks you want to be represented in the output file, specify your own output format, or modify the asset during the conversion process.
你可以使用 export session 将一个现有的asset重新编码为一个小数字,这个小数字是常用的预先设定好的一些小数字中的一个。如果在转换中你需要更多的控制,在iOS4.1已经以后的版本中,你可以使用 asset reader 和 asset writer 对象串联的一个一个的转换。例如你可以使用这些对象选择在输出的文件中想要表示的轨道,指定你自己的输出格式,或者在转换过程中修改这个asset。
To produce a visual representation of the waveform, you use an asset reader to read the audio track of an asset.
为了产生波形的可视化表示,你可以使用asset reader去读取asset中的音频轨道。
相关章节:Using Assets
Thumbnails
缩略图
To create thumbnail images of video presentations, you initialize an instance of AVAssetImageGenerator using the asset from which you want to generate thumbnails. AVAssetImageGenerator uses the default enabled video tracks to generate images.
创建视频演示图像的缩略图,使用想要生成缩略图的asset初始化一个 AVAssetImageGenerator 的实例。AVAssetImageGenerator 使用默认启用视频轨道来生成图像。
相关章节:Using Assets
Editing
修改
AVFoundation uses compositions to create new assets from existing pieces of media (typically, one or more video and audio tracks). You use a mutable composition to add and remove tracks, and adjust their temporal orderings. You can also set the relative volumes and ramping of audio tracks; and set the opacity, and opacity ramps, of video tracks. A composition is an assemblage of pieces of media held in memory. When you export a composition using an export session, it’s collapsed to a file.
AVFoundation 使用 compositions 去从现有的媒体片段(通常是一个或多个视频和音频轨道)创建新的 assets 。你可以使用一个可变成分去添加和删除轨道,并调整它们的时间顺序。你也可以设置相对音量和增加音频轨道;并设置视频轨道的不透明度和混浊的坡道。一种组合是在记忆体中保存的一组媒体。当你使用 export session 导出一个组合的时候,它会崩溃在一个文件中。
You can also create an asset from media such as sample buffers or still images using an asset writer.
你也可以从媒体上创建一个asset,比如使用asset writer.的示例缓冲区或静态图像。
相关章节:Editing
Still and Video Media Capture
静态和视频媒体捕获
Recording input from cameras and microphones is managed by a capture session. A capture session coordinates the flow of data from input devices to outputs such as a movie file. You can configure multiple inputs and outputs for a single session, even when the session is running. You send messages to the session to start and stop data flow.
In addition, you can use an instance of a preview layer to show the user what a camera is recording.
从相机和麦克风记录输入是由一个 capture session 管理的。一个 capture session 协调从输入设备到输出的数据流,比如一个电影文件。你可以为单一的 session 配置多个输入和输出,甚至 session 正在运行的时候也可以的。你将消息发送到 session 去启动和停止数据流。
此外,你可以使用 preview layer 的一个实例来向用户显示摄像机正在录制的内容。
相关章节:Still and Video Media Capture
Concurrent Programming with AVFoundation
AVFoundation的并发编程
Callbacks from AVFoundation—invocations of blocks, key-value observers, and notification handlers—are not guaranteed to be made on any particular thread or queue. Instead, AVFoundation invokes these handlers on threads or queues on which it performs its internal tasks.
AVFoundation的回调有block块的回调,键值监听以及通知回调,但这些都不能保证特定的线程或者队列的进行。相反,AVFoundation可以在执行其内部任务的线程或队列上调用这些处理程序。
There are two general guidelines as far as notifications and threading:
下面是关于通知和线程的准则
UI related notifications occur on the main thread.
在主线上去做UI更新的通知。
Classes or methods that require you create and/or specify a queue will return notifications on that queue.
创建或者制定一个队列或者方法返回这个队列的通知。
Beyond those two guidelines (and there are exceptions, which are noted in the reference documentation) you should not assume that a notification will be returned on any specific thread.
除了这两个准则(在参考文档中也有例外),您不应该认为在任何特定线程上都会返回一个通知。
If you’re writing a multithreaded application, you can use the NSThread method isMainThread or [[NSThread currentThread] isEqual:<#A stored thread reference#>] to test whether the invocation thread is a thread you expect to perform your work on. You can redirect messages to appropriate threads using methods such as performSelectorOnMainThread:withObject:waitUntilDone: and performSelector:onThread:withObject:waitUntilDone:modes:. You could also use dispatch_async to “bounce” to your blocks on an appropriate queue, either the main queue for UI tasks or a queue you have up for concurrent operations. For more about concurrent operations, see Concurrency Programming Guide; for more about blocks, see Blocks Programming Topics. The AVCam-iOS: Using AVFoundation to Capture Images and Movies sample code is considered the primary example for all AVFoundation functionality and can be consulted for examples of thread and queue usage with AVFoundation.
如果您正在编写一个多线程应用程序,您可以使用NSThread方法isMainThread或[[NSThread currentThread] isEqual:<#A stored thread reference#>] 去测试调用线程是否是您期望执行工作的线程。可以把消息重定向到适当的线程使用方法如performSelectorOnMainThread:withObject:waitUntilDone: 以及 performSelector:onThread:withObject:waitUntilDone:modes:.还可以使用dispatchasync在适当的队列上回到到您的块上,无论是在主线程上处理UI的操作还是有别的并发操作的队列。有关并发操作的更多信息,请参阅Concurrency Programming Guide,关于blocks的更多信息请参阅Blocks Programming Topics AVCam-iOS: Using AVFoundation to Capture Images and Movies .。avcam-ios:使用AVFoundation来捕获图像和电影示例代码,被认为是所有AVFoundation功能的主要示例,可以参考AVFoundation的线程和队列使用情况。
Prerequisites
预备知识
AVFoundation is an advanced Cocoa framework. To use it effectively, you must have:
AVFoundation是Cocoa framework里一个高级的框架。为了更好的使用它,你需要有:
A solid understanding of fundamental Cocoa development tools and techniques
A basic grasp of blocks
A basic understanding of key-value coding and key-value observing
For playback, a basic understanding of (see Core Animation Programming Guide or, for basic playback, the AVKit Framework Reference.
熟练使用基本的Cocoa开发工具和技术。
需要掌握block基础的知识理解kvc和kvo。
对于playback,需要了解Core Animation (参考 Core Animation Programming Guide,playback参考AVKit Framework Reference).
See Also
There are several AVFoundation examples including two that are key to understanding and implementation Camera capture functionality:
这里有一些AVFoundation例子,可以帮助你理解和实现Camera capture。
AVCam-iOS: Using AVFoundation to Capture Images and Movies is the canonical sample code for implementing any program that uses the camera functionality. It is a complete sample, well documented, and covers the majority of the functionality showing the best practices.
AVCamManual: Extending AVCam to Use Manual Capture API is the companion application to AVCam. It implements Camera functionality using the manual camera controls. It is also a complete example, well documented, and should be considered the canonical example for creating camera applications that take advantage of manual controls.
RosyWriter is an example that demonstrates real time frame processing and in particular how to apply filters to video content. This is a very common developer requirement and this example covers that functionality.
RosyWriter 是一个实时的视频帧处理。特别是如何在视频内容中应用filters。这是一个常见的开发需求,这个例子包含了这个功能。
AVLocationPlayer: Using AVFoundation Metadata Reading APIs demonstrates using the metadata APIs.