AVFoundation-學(xué)習(xí)筆記1 文檔翻譯理解

近期一直搞視頻模塊的學(xué)習(xí)。蘋果也提供了AVFoundation這個(gè)強(qiáng)大的庫來支持開發(fā)。先通過文檔來了解下AVFoundation究竟是個(gè)多么強(qiáng)大的庫。英文水平有限,查了谷歌有道各種翻譯,參考了眾多大神的博客。后續(xù)在慢慢改進(jìn)~~~ 翻譯有誤多多指教

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是一個(gè)可以用來播放和創(chuàng)建基于時(shí)間的視聽媒體的框架。 它提供了一個(gè)Objective-C界面,用于使用基于時(shí)間的視聽數(shù)據(jù)的詳細(xì)級別。 例如,您可以使用它來檢查,創(chuàng)建,編輯或重新編碼媒體文件。 還可以在設(shè)備中實(shí)時(shí)捕獲和播放期間從設(shè)備獲取輸入流并操縱視頻。下面是iOS上面結(jié)構(gòu)圖。

AVFoundation stack on iOS.png

Figure I-2 shows the corresponding media architecture on OS X.

AVFoundation stack on OS X.png

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).
您應(yīng)該使用可用的最高級別的抽象接口,執(zhí)行你想要做的任務(wù)。
如果你只是想簡單的播放,那就使用 AVKit framework
在iOS上,如果你錄制視頻時(shí)候只需要用到最少格式設(shè)置時(shí),那就可以使用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 中使用的原始數(shù)據(jù)結(jié)構(gòu),包括時(shí)間相關(guān)的數(shù)據(jù)結(jié)構(gòu),傳遞不透明數(shù)據(jù)對象,媒體數(shù)據(jù)描述是在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框架包含視頻相關(guān)的APIs和音頻相關(guān)的APIs。舊的音頻相關(guān)類提供了簡便的方法來處理音頻。他們在Multimedia Programming Guide中介紹,不在這個(gè)文檔中。

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.
還可以通過設(shè)置AVAudioSession來配置應(yīng)用程序的音頻。在Audio Session Programming Guide文檔中有詳細(xì)的介紹。

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媒體框架中主要的類。框架的設(shè)計(jì)主要是由這種表示引導(dǎo)。了解它的結(jié)構(gòu)將有助于您了解該框架是如何工作的。一個(gè)AVAsset 實(shí)例的媒體數(shù)據(jù)的一個(gè)或更多個(gè)(音頻和視頻軌道)的集合的聚集表示。它規(guī)定將有關(guān)集合的信息作為一個(gè)整體,如它的名稱,時(shí)間,自然呈現(xiàn)大小等的信息。 AVAsset 是不依賴于特定的數(shù)據(jù)格式。 AVAsset是常常從URL中的媒體創(chuàng)建資產(chǎn)實(shí)例的這種類父類(請參閱 Using Assets),并創(chuàng)造新的成分(見 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中媒體數(shù)據(jù)的各個(gè)部分,每一個(gè)都是一個(gè)統(tǒng)一的類型,把這個(gè)類型稱為“軌道”。在一個(gè)典型簡單的情況下,一個(gè)軌道代表這個(gè)音頻組件,另一個(gè)代表視頻組件。然而復(fù)雜的組合中,有可能是多個(gè)重疊的音頻和視頻軌道。Assets也可能有元數(shù)據(jù)。

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 中一個(gè)非常重要的概念是:初始化一個(gè) asset 或者一個(gè)軌道并不一定意味著它已經(jīng)準(zhǔn)備好可以被使用。這可能需要一些時(shí)間來計(jì)算一個(gè)項(xiàng)目的持續(xù)時(shí)間(例如一個(gè)MP3文件,其中可能不包含摘要信息)。而不是當(dāng)一個(gè)值被計(jì)算的時(shí)候阻塞當(dāng)前線程,你訪問這個(gè)值,并且通過調(diào)用你定義的一個(gè) block 來得到異步返回。

相關(guān)章節(jié):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允許你用一種復(fù)雜的方式來管理asset的播放。為了支持這一點(diǎn),它將一個(gè)asset的呈現(xiàn)狀態(tài)從asset自身中分離出來。例如允許你在不同的分辨率下同時(shí)播放同一個(gè)asset中的兩個(gè)不同的片段。一個(gè)asset的呈現(xiàn)狀態(tài)是由player item對象管理的。Asset中的每個(gè)軌道的呈現(xiàn)狀態(tài)是由player item track對象管理的。例如使用player item和player item tracks,你可以設(shè)置被播放器呈現(xiàn)的項(xiàng)目中可視的那一部分,設(shè)置音頻的混合參數(shù)以及被應(yīng)用于播放期間的視頻組合設(shè)定,或者播放期間的禁用組件。

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.
你可以使用一個(gè) player 對象來播放播放器項(xiàng)目,并且直接輸出一個(gè)播放器給核心動畫層。你可以使用一個(gè) player queue(player對象的隊(duì)列)去給隊(duì)列中player items集合中的播放項(xiàng)目安排序列。

相關(guān)章節(jié):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 允許你用幾種方式創(chuàng)建新的 asset 的表現(xiàn)形式。你可以簡單將已經(jīng)存在的 asset 重新編碼,或者在iOS4.1以及之后的版本中,你可以在一個(gè) asset 的目錄中執(zhí)行一些操作并且將結(jié)果保存為一個(gè)新的 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 將一個(gè)現(xiàn)有的asset重新編碼為一個(gè)小數(shù)字,這個(gè)小數(shù)字是常用的預(yù)先設(shè)定好的一些小數(shù)字中的一個(gè)。如果在轉(zhuǎn)換中你需要更多的控制,在iOS4.1已經(jīng)以后的版本中,你可以使用 asset reader 和 asset writer 對象串聯(lián)的一個(gè)一個(gè)的轉(zhuǎn)換。例如你可以使用這些對象選擇在輸出的文件中想要表示的軌道,指定你自己的輸出格式,或者在轉(zhuǎn)換過程中修改這個(gè)asset。

To produce a visual representation of the waveform, you use an asset reader to read the audio track of an asset.

為了產(chǎn)生波形的可視化表示,你可以使用asset reader去讀取asset中的音頻軌道。

相關(guān)章節(jié):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.

創(chuàng)建視頻演示圖像的縮略圖,使用想要生成縮略圖的asset初始化一個(gè) AVAssetImageGenerator 的實(shí)例。AVAssetImageGenerator 使用默認(rèn)啟用視頻軌道來生成圖像。

相關(guān)章節(jié):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 去從現(xiàn)有的媒體片段(通常是一個(gè)或多個(gè)視頻和音頻軌道)創(chuàng)建新的 assets 。你可以使用一個(gè)可變成分去添加和刪除軌道,并調(diào)整它們的時(shí)間順序。你也可以設(shè)置相對音量和增加音頻軌道;并設(shè)置視頻軌道的不透明度和混濁的坡道。一種組合是在記憶體中保存的一組媒體。當(dāng)你使用 export session 導(dǎo)出一個(gè)組合的時(shí)候,它會崩潰在一個(gè)文件中。

You can also create an asset from media such as sample buffers or still images using an asset writer.
你也可以從媒體上創(chuàng)建一個(gè)asset,比如使用asset writer.的示例緩沖區(qū)或靜態(tài)圖像。

相關(guān)章節(jié):Editing

Still and Video Media Capture

靜態(tài)和視頻媒體捕獲

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.
從相機(jī)和麥克風(fēng)記錄輸入是由一個(gè) capture session 管理的。一個(gè) capture session 協(xié)調(diào)從輸入設(shè)備到輸出的數(shù)據(jù)流,比如一個(gè)電影文件。你可以為單一的 session 配置多個(gè)輸入和輸出,甚至 session 正在運(yùn)行的時(shí)候也可以的。你將消息發(fā)送到 session 去啟動和停止數(shù)據(jù)流。
此外,你可以使用 preview layer 的一個(gè)實(shí)例來向用戶顯示攝像機(jī)正在錄制的內(nèi)容。
相關(guān)章節(jié):Still and Video Media Capture

Concurrent Programming with AVFoundation

AVFoundation的并發(fā)編程

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的回調(diào)有block塊的回調(diào),鍵值監(jiān)聽以及通知回調(diào),但這些都不能保證特定的線程或者隊(duì)列的進(jìn)行。相反,AVFoundation可以在執(zhí)行其內(nèi)部任務(wù)的線程或隊(duì)列上調(diào)用這些處理程序。

There are two general guidelines as far as notifications and threading:
下面是關(guān)于通知和線程的準(zhǔn)則

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.
創(chuàng)建或者制定一個(gè)隊(duì)列或者方法返回這個(gè)隊(duì)列的通知。

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.
除了這兩個(gè)準(zhǔn)則(在參考文檔中也有例外),您不應(yīng)該認(rèn)為在任何特定線程上都會返回一個(gè)通知。

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.

如果您正在編寫一個(gè)多線程應(yīng)用程序,您可以使用NSThread方法isMainThread或[[NSThread currentThread] isEqual:<#A stored thread reference#>] 去測試調(diào)用線程是否是您期望執(zhí)行工作的線程。可以把消息重定向到適當(dāng)?shù)木€程使用方法如performSelectorOnMainThread:withObject:waitUntilDone: 以及 performSelector:onThread:withObject:waitUntilDone:modes:.還可以使用dispatchasync在適當(dāng)?shù)年?duì)列上回到到您的塊上,無論是在主線程上處理UI的操作還是有別的并發(fā)操作的隊(duì)列。有關(guān)并發(fā)操作的更多信息,請參閱Concurrency Programming Guide,關(guān)于blocks的更多信息請參閱Blocks Programming Topics AVCam-iOS: Using AVFoundation to Capture Images and Movies .。avcam-ios:使用AVFoundation來捕獲圖像和電影示例代碼,被認(rèn)為是所有AVFoundation功能的主要示例,可以參考AVFoundation的線程和隊(duì)列使用情況。

Prerequisites
預(yù)備知識

AVFoundation is an advanced Cocoa framework. To use it effectively, you must have:
AVFoundation是Cocoa framework里一個(gè)高級的框架。為了更好的使用它,你需要有:

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開發(fā)工具和技術(shù)。
需要掌握block基礎(chǔ)的知識理解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例子,可以幫助你理解和實(shí)現(xiàn)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 是一個(gè)實(shí)時(shí)的視頻幀處理。特別是如何在視頻內(nèi)容中應(yīng)用filters。這是一個(gè)常見的開發(fā)需求,這個(gè)例子包含了這個(gè)功能。

AVLocationPlayer: Using AVFoundation Metadata Reading APIs demonstrates using the metadata APIs.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,835評論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,676評論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,730評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,118評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,873評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,266評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,330評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,482評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,036評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,846評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,025評論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,575評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,279評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,684評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,953評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,751評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,016評論 2 375

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,721評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,511評論 5 6
  • 1.5月02日,早上洗漱 2.5月14日,早上洗漱 3.5月15日,早上洗漱 4.5月17日,早上洗漱 5.5月1...
    家峪閱讀 159評論 0 0
  • 夏日炎炎烈日照,一路原野似火燒。驕陽無處路曬夠,初果盛世背代出。車行一路有驛站,百樹叢中水果餐。天炎地?zé)嵴镶?..
    甘朝武閱讀 231評論 0 0
  • 比翼雙飛 理枝相連 唯你 獨(dú)立于世 孤獨(dú) 孤芳 孤傲 …… 也罷 至少 你存在過 遺立于世的孤兒 請四海為家 以夢...
    嚕啦嚀嚀閱讀 223評論 0 1