AVAudioFoundation(3):音視頻編輯

本文轉(zhuǎn)自:AVAudioFoundation(3):音視頻編輯 | www.samirchen.com

本文主要內(nèi)容來自 AVFoundation Programming Guide。

音視頻編輯

上面簡(jiǎn)單了解了下 AVFoundation 框架后,我們來看看跟音視頻編輯相關(guān)的接口。

一個(gè) composition 可以簡(jiǎn)單的認(rèn)為是一組軌道(tracks)的集合,這些軌道可以是來自不同媒體資源(asset)。AVMutableComposition 提供了接口來插入或者刪除軌道,也可以調(diào)整這些軌道的順序。

下面這張圖反映了一個(gè)新的 composition 是怎么從已有的 asset 中獲取對(duì)應(yīng)的 track 并進(jìn)行拼接形成新的 asset。

image

在處理音頻時(shí),你可以在使用 AVMutableAudioMix 類的接口來做一些自定義的操作,如下圖所示?,F(xiàn)在,你可以做到指定一個(gè)最大音量或設(shè)置一個(gè)音頻軌道的音量漸變。

image

如下圖所示,我們還可以使用 AVMutableVideoComposition 來直接處理 composition 中的視頻軌道。處理一個(gè)單獨(dú)的 video composition 時(shí),你可以指定它的渲染尺寸、縮放比例、幀率等參數(shù)并輸出最終的視頻文件。通過一些針對(duì) video composition 的指令(AVMutableVideoCompositionInstruction 等),我們可以修改視頻的背景顏色、應(yīng)用 layer instructions。這些 layer instructions(AVMutableVideoCompositionLayerInstruction 等)可以用來對(duì) composition 中的視頻軌道實(shí)施圖形變換、添加圖形漸變、透明度變換、增加透明度漸變。此外,你還能通過設(shè)置 video composition 的 animationTool 屬性來應(yīng)用 Core Animation Framework 框架中的動(dòng)畫效果。

image

如下圖所示,你可以使用 AVAssetExportSession 相關(guān)的接口來合并你的 composition 中的 audio mix 和 video composition。你只需要初始化一個(gè) AVAssetExportSession 對(duì)象,然后將其 audioMixvideoComposition 屬性分別設(shè)置為你的 audio mix 和 video composition 即可。

image

創(chuàng)建 Composition

上面簡(jiǎn)單介紹了集中音視頻編輯的場(chǎng)景,現(xiàn)在我們來詳細(xì)介紹具體的接口。從 AVMutableComposition 開始。

當(dāng)使用 AVMutableComposition 創(chuàng)建自己的 composition 時(shí),最典型的,我們可以使用 AVMutableCompositionTrack 來向 composition 中添加一個(gè)或多個(gè) composition tracks,比如下面這個(gè)簡(jiǎn)單的例子便是向一個(gè) composition 中添加一個(gè)音頻軌道和一個(gè)視頻軌道:

AVMutableComposition *mutableComposition = [AVMutableComposition composition];
// Create the video composition track.
AVMutableCompositionTrack *mutableCompositionVideoTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
// Create the audio composition track.
AVMutableCompositionTrack *mutableCompositionAudioTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

當(dāng)為 composition 添加一個(gè)新的 track 的時(shí)候,需要設(shè)置其媒體類型(media type)和 track ID,主要的媒體類型包括:音頻、視頻、字幕、文本等等。

這里需要注意的是,每個(gè) track 都需要一個(gè)唯一的 track ID,比較方便的做法是:設(shè)置 track ID 為 kCMPersistentTrackID_Invalid 來為對(duì)應(yīng)的 track 獲得一個(gè)自動(dòng)生成的唯一 ID。

向 Composition 添加視聽數(shù)據(jù)

要將媒體數(shù)據(jù)添加到一個(gè) composition track 中需要訪問媒體數(shù)據(jù)所在的 AVAsset,可以使用 AVMutableCompositionTrack 的接口將具有相同媒體類型的多個(gè) track 添加到同一個(gè) composition track 中。下面的例子便是從兩個(gè) AVAsset 中各取出一份 video asset track,再添加到一個(gè)新的 composition track 中去:

// You can retrieve AVAssets from a number of places, like the camera roll for example.
AVAsset *videoAsset = <#AVAsset with at least one video track#>;
AVAsset *anotherVideoAsset = <#another AVAsset with at least one video track#>;
// Get the first video track from each asset.
AVAssetTrack *videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *anotherVideoAssetTrack = [[anotherVideoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// Add them both to the composition.
[mutableCompositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAssetTrack.timeRange.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:nil];
[mutableCompositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,anotherVideoAssetTrack.timeRange.duration) ofTrack:anotherVideoAssetTrack atTime:videoAssetTrack.timeRange.duration error:nil];

檢索兼容的 Composition Tracks

如果可能,每一種媒體類型最好只使用一個(gè) composition track,這樣能夠優(yōu)化資源的使用。當(dāng)你連續(xù)播放媒體數(shù)據(jù)時(shí),應(yīng)該將相同類型的媒體數(shù)據(jù)放到同一個(gè) composition track 中,你可以通過類似下面的代碼來從 composition 中查找是否有與當(dāng)前的 asset track 兼容的 composition track,然后拿來使用:

AVMutableCompositionTrack *compatibleCompositionTrack = [mutableComposition mutableTrackCompatibleWithTrack:<#the AVAssetTrack you want to insert#>];
if (compatibleCompositionTrack) {
    // Implementation continues.
}

需要注意的是,在同一個(gè) composition track 中添加多個(gè)視頻段時(shí),當(dāng)視頻段之間切換時(shí)可能會(huì)丟幀,尤其在嵌入式設(shè)備上。基于這個(gè)問題,應(yīng)該合理選擇一個(gè) composition track 里的視頻段數(shù)量。

設(shè)置音量漸變

只使用一個(gè) AVMutableAudioMix 對(duì)象就能夠?yàn)?composition 中的每一個(gè) audio track 單獨(dú)做音頻處理。

下面代碼展示了如果使用 AVMutableAudioMix 給一個(gè) audio track 設(shè)置音量漸變給聲音增加一個(gè)淡出效果。使用 audioMix 類方法獲取 AVMutableAudioMix 實(shí)例;然后使用 AVMutableAudioMixInputParameters 類的 audioMixInputParametersWithTrack: 接口將 AVMutableAudioMix 實(shí)例與 composition 中的某一個(gè) audio track 關(guān)聯(lián)起來;之后便可以通過 AVMutableAudioMix 實(shí)例來處理音量了。

AVMutableAudioMix *mutableAudioMix = [AVMutableAudioMix audioMix];
// Create the audio mix input parameters object.
AVMutableAudioMixInputParameters *mixParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:mutableCompositionAudioTrack];
// Set the volume ramp to slowly fade the audio out over the duration of the composition.
[mixParameters setVolumeRampFromStartVolume:1.f toEndVolume:0.f timeRange:CMTimeRangeMake(kCMTimeZero, mutableComposition.duration)];
// Attach the input parameters to the audio mix.
mutableAudioMix.inputParameters = @[mixParameters];

自定義視頻處理

處理音頻是我們使用 AVMutableAudioMix,那么處理視頻時(shí),我們就使用 AVMutableVideoComposition,只需要一個(gè) AVMutableVideoComposition 實(shí)例就可以為 composition 中所有的 video track 做處理,比如設(shè)置渲染尺寸、縮放、播放幀率等等。

下面我們依次來看一些場(chǎng)景。

設(shè)置視頻背景色

所有的 video composition 也必然對(duì)應(yīng)一組 AVVideoCompositionInstruction 實(shí)例,每個(gè) AVVideoCompositionInstruction 中至少包含一條 video composition instruction。我們可以使用 AVMutableVideoCompositionInstruction 來創(chuàng)建我們自己的 video composition instruction,通過這些指令,我們可以修改 composition 的背景顏色、后處理、layer instruction 等等。

下面的實(shí)例代碼展示了如何創(chuàng)建 video composition instruction 并將一個(gè) composition 的整個(gè)時(shí)長(zhǎng)都設(shè)置為紅色背景色:

AVMutableVideoCompositionInstruction *mutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mutableVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, mutableComposition.duration);
mutableVideoCompositionInstruction.backgroundColor = [[UIColor redColor] CGColor];

設(shè)置透明度漸變

我們也可以用 video composition instructions 來應(yīng)用 video composition layer instructions。AVMutableVideoCompositionLayerInstruction 可以用來設(shè)置 video track 的圖形變換、圖形漸變、透明度、透明度漸變等等。一個(gè) video composition instruction 的 layerInstructions 屬性中所存儲(chǔ)的 layer instructions 的順序決定了 tracks 中的視頻幀是如何被放置和組合的。

下面的示例代碼展示了如何在從一個(gè)視頻切換到第二個(gè)視頻時(shí)添加一個(gè)透明度漸變的效果:

AVAsset *firstVideoAssetTrack = <#AVAssetTrack representing the first video segment played in the composition#>;
AVAsset *secondVideoAssetTrack = <#AVAssetTrack representing the second video segment played in the composition#>;
// Create the first video composition instruction.
AVMutableVideoCompositionInstruction *firstVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
// Set its time range to span the duration of the first video track.
firstVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration);
// Create the layer instruction and associate it with the composition video track.
AVMutableVideoCompositionLayerInstruction *firstVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableCompositionVideoTrack];
// Create the opacity ramp to fade out the first video track over its entire duration.
[firstVideoLayerInstruction setOpacityRampFromStartOpacity:1.f toEndOpacity:0.f timeRange:CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration)];
// Create the second video composition instruction so that the second video track isn't transparent.
AVMutableVideoCompositionInstruction *secondVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
// Set its time range to span the duration of the second video track.
secondVideoCompositionInstruction.timeRange = CMTimeRangeMake(firstVideoAssetTrack.timeRange.duration, CMTimeAdd(firstVideoAssetTrack.timeRange.duration, secondVideoAssetTrack.timeRange.duration));
// Create the second layer instruction and associate it with the composition video track.
AVMutableVideoCompositionLayerInstruction *secondVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mutableCompositionVideoTrack];
// Attach the first layer instruction to the first video composition instruction.
firstVideoCompositionInstruction.layerInstructions = @[firstVideoLayerInstruction];
// Attach the second layer instruction to the second video composition instruction.
secondVideoCompositionInstruction.layerInstructions = @[secondVideoLayerInstruction];
// Attach both of the video composition instructions to the video composition.
AVMutableVideoComposition *mutableVideoComposition = [AVMutableVideoComposition videoComposition];
mutableVideoComposition.instructions = @[firstVideoCompositionInstruction, secondVideoCompositionInstruction];

動(dòng)畫效果

我們還能通過設(shè)置 video composition 的 animationTool 屬性來使用 Core Animation Framework 框架的強(qiáng)大能力。比如:設(shè)置視頻水印、視頻標(biāo)題、動(dòng)畫浮層等。

在 video composition 中使用 Core Animation 有兩種不同的方式:

  • 添加一個(gè) Core Animation Layer 作為獨(dú)立的 composition track
  • 直接使用 Core Animation Layer 在視頻幀中渲染動(dòng)畫效果

下面的代碼展示了后面一種使用方式,在視頻區(qū)域的中心添加水?。?/p>

CALayer *watermarkLayer = <#CALayer representing your desired watermark image#>;
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, mutableVideoComposition.renderSize.width, mutableVideoComposition.renderSize.height);
videoLayer.frame = CGRectMake(0, 0, mutableVideoComposition.renderSize.width, mutableVideoComposition.renderSize.height);
[parentLayer addSublayer:videoLayer];
watermarkLayer.position = CGPointMake(mutableVideoComposition.renderSize.width/2, mutableVideoComposition.renderSize.height/4);
[parentLayer addSublayer:watermarkLayer];
mutableVideoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

一個(gè)完整示例

這里的示例將展示如何合并兩個(gè) video asset tracks 和一個(gè) audio asset track 到一個(gè)視頻文件,其中大體步驟如下:

  • 創(chuàng)建一個(gè) AVMutableComposition 對(duì)象,添加多個(gè) AVMutableCompositionTrack 對(duì)象
  • 在各個(gè) composition tracks 中添加 AVAssetTrack 對(duì)應(yīng)的時(shí)間范圍
  • 檢查 video asset track 的 preferredTransform 屬性決定視頻方向
  • 使用 AVMutableVideoCompositionLayerInstruction 對(duì)象對(duì)視頻進(jìn)行圖形變換
  • 設(shè)置 video composition 的 renderSizeframeDuration 屬性
  • 導(dǎo)出視頻文件
  • 保存視頻文件到相冊(cè)

下面的示例代碼省略了一些內(nèi)存管理和通知移除相關(guān)的代碼。

// 1、創(chuàng)建 composition。創(chuàng)建一個(gè) composition,并添加一個(gè) audio track 和一個(gè) video track。
AVMutableComposition *mutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

// 2、添加 asset。從源 assets 中取得兩個(gè) video track 和一個(gè) audio track,在上面的 video composition track 中依次添加兩個(gè) video track,在 audio composition track 中添加一個(gè) video track。
AVAsset *firstVideoAsset = <#First AVAsset with at least one video track#>;
AVAsset *secondVideoAsset = <#Second AVAsset with at least one video track#>;
AVAsset *audioAsset = <#AVAsset with at least one audio track#>;
AVAssetTrack *firstVideoAssetTrack = [[firstVideoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *secondVideoAssetTrack = [[secondVideoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration) ofTrack:firstVideoAssetTrack atTime:kCMTimeZero error:nil];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondVideoAssetTrack.timeRange.duration) ofTrack:secondVideoAssetTrack atTime:firstVideoAssetTrack.timeRange.duration error:nil];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstVideoAssetTrack.timeRange.duration, secondVideoAssetTrack.timeRange.duration)) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];

// 3、檢查 composition 方向。在 composition 中添加了 audio track 和 video track 后,還必須確保其中所有的 video track 的視頻方向都是一致的。在默認(rèn)情況下 video track 默認(rèn)為橫屏模式,如果這時(shí)添加進(jìn)來的 video track 是在豎屏模式下采集的,那么導(dǎo)出的視頻會(huì)出現(xiàn)方向錯(cuò)誤。同理,將一個(gè)橫向的視頻和一個(gè)縱向的視頻進(jìn)行合并導(dǎo)出,export session 會(huì)報(bào)錯(cuò)。
BOOL isFirstVideoPortrait = NO;
CGAffineTransform firstTransform = firstVideoAssetTrack.preferredTransform;
// Check the first video track's preferred transform to determine if it was recorded in portrait mode.
if (firstTransform.a == 0 && firstTransform.d == 0 && (firstTransform.b == 1.0 || firstTransform.b == -1.0) && (firstTransform.c == 1.0 || firstTransform.c == -1.0)) {
    isFirstVideoPortrait = YES;
}
BOOL isSecondVideoPortrait = NO;
CGAffineTransform secondTransform = secondVideoAssetTrack.preferredTransform;
// Check the second video track's preferred transform to determine if it was recorded in portrait mode.
if (secondTransform.a == 0 && secondTransform.d == 0 && (secondTransform.b == 1.0 || secondTransform.b == -1.0) && (secondTransform.c == 1.0 || secondTransform.c == -1.0)) {
    isSecondVideoPortrait = YES;
}
if ((isFirstVideoAssetPortrait && !isSecondVideoAssetPortrait) || (!isFirstVideoAssetPortrait && isSecondVideoAssetPortrait)) {
    UIAlertView *incompatibleVideoOrientationAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Cannot combine a video shot in portrait mode with a video shot in landscape mode." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [incompatibleVideoOrientationAlert show];
    return;
}

// 4、應(yīng)用 Video Composition Layer Instructions。一旦你知道你要合并的視頻片段的方向是兼容的,那么你接下來就可以為每個(gè)片段應(yīng)用必要的 layer instructions,并將這些 layer instructions 添加到 video composition 中。
// 所有的 `AVAssetTrack` 對(duì)象都有一個(gè) `preferredTransform` 屬性,包含了 asset track 的方向信息。這個(gè) transform 會(huì)在 asset track 在屏幕上展示時(shí)被應(yīng)用。在下面的代碼中,layer instruction 的 transform 被設(shè)置為 asset track 的 transform,便于在你修改了視頻尺寸時(shí),新的 composition 中的視頻也能正確的進(jìn)行展示。
AVMutableVideoCompositionInstruction *firstVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
// Set the time range of the first instruction to span the duration of the first video track.
firstVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, firstVideoAssetTrack.timeRange.duration);
AVMutableVideoCompositionInstruction *secondVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
// Set the time range of the second instruction to span the duration of the second video track.
secondVideoCompositionInstruction.timeRange = CMTimeRangeMake(firstVideoAssetTrack.timeRange.duration, CMTimeAdd(firstVideoAssetTrack.timeRange.duration, secondVideoAssetTrack.timeRange.duration));

// 創(chuàng)建兩個(gè) video layer instruction,關(guān)聯(lián)對(duì)應(yīng)的 video composition track,并設(shè)置 transform 為 preferredTransform。
AVMutableVideoCompositionLayerInstruction *firstVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack];
// Set the transform of the first layer instruction to the preferred transform of the first video track.
[firstVideoLayerInstruction setTransform:firstTransform atTime:kCMTimeZero];
AVMutableVideoCompositionLayerInstruction *secondVideoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack];
// Set the transform of the second layer instruction to the preferred transform of the second video track.
[secondVideoLayerInstruction setTransform:secondTransform atTime:firstVideoAssetTrack.timeRange.duration];

firstVideoCompositionInstruction.layerInstructions = @[firstVideoLayerInstruction];
secondVideoCompositionInstruction.layerInstructions = @[secondVideoLayerInstruction];
AVMutableVideoComposition *mutableVideoComposition = [AVMutableVideoComposition videoComposition];
mutableVideoComposition.instructions = @[firstVideoCompositionInstruction, secondVideoCompositionInstruction];

// 5、設(shè)置渲染尺寸和幀率。要完全解決視頻方向問題,你還需要調(diào)整 video composition 的 `renderSize` 屬性,同時(shí)也需要設(shè)置一個(gè)合適的 `frameDuration`,比如 1/30 表示 30 幀每秒。此外,`renderScale` 默認(rèn)值為 1.0。
CGSize naturalSizeFirst, naturalSizeSecond;
// If the first video asset was shot in portrait mode, then so was the second one if we made it here.
if (isFirstVideoAssetPortrait) {
    // Invert the width and height for the video tracks to ensure that they display properly.
    naturalSizeFirst = CGSizeMake(firstVideoAssetTrack.naturalSize.height, firstVideoAssetTrack.naturalSize.width);
    naturalSizeSecond = CGSizeMake(secondVideoAssetTrack.naturalSize.height, secondVideoAssetTrack.naturalSize.width);
} else {
    // If the videos weren't shot in portrait mode, we can just use their natural sizes.
    naturalSizeFirst = firstVideoAssetTrack.naturalSize;
    naturalSizeSecond = secondVideoAssetTrack.naturalSize;
}
float renderWidth, renderHeight;
// Set the renderWidth and renderHeight to the max of the two videos widths and heights.
if (naturalSizeFirst.width > naturalSizeSecond.width) {
    renderWidth = naturalSizeFirst.width;
} else {
    renderWidth = naturalSizeSecond.width;
}
if (naturalSizeFirst.height > naturalSizeSecond.height) {
    renderHeight = naturalSizeFirst.height;
} else {
    renderHeight = naturalSizeSecond.height;
}
mutableVideoComposition.renderSize = CGSizeMake(renderWidth, renderHeight);
// Set the frame duration to an appropriate value (i.e. 30 frames per second for video).
mutableVideoComposition.frameDuration = CMTimeMake(1,30);


// 6、導(dǎo)出 composition 并保持到相冊(cè)。創(chuàng)建一個(gè) `AVAssetExportSession` 對(duì)象,設(shè)置對(duì)應(yīng)的 `outputURL` 來將視頻導(dǎo)出到指定的文件。同時(shí),我們還可以用 `ALAssetsLibrary` 接口來將導(dǎo)出的視頻文件存儲(chǔ)到相冊(cè)中去。
// Create a static date formatter so we only have to initialize it once.
static NSDateFormatter *kDateFormatter;
if (!kDateFormatter) {
    kDateFormatter = [[NSDateFormatter alloc] init];
    kDateFormatter.dateStyle = NSDateFormatterMediumStyle;
    kDateFormatter.timeStyle = NSDateFormatterShortStyle;
}
// Create the export session with the composition and set the preset to the highest quality.
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality];
// Set the desired output URL for the file created by the export process.
exporter.outputURL = [[[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:@YES error:nil] URLByAppendingPathComponent:[kDateFormatter stringFromDate:[NSDate date]]] URLByAppendingPathExtension:CFBridgingRelease(UTTypeCopyPreferredTagWithClass((CFStringRef)AVFileTypeQuickTimeMovie, kUTTagClassFilenameExtension))];
// Set the output file type to be a QuickTime movie.
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = mutableVideoComposition;
// Asynchronously export the composition to a video file and save this file to the camera roll once export completes.
[exporter exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (exporter.status == AVAssetExportSessionStatusCompleted) {
            ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
            if ([assetsLibrary videoAtPathIsCompatibleWithSavedPhotosAlbum:exporter.outputURL]) {
                [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:exporter.outputURL completionBlock:NULL];
            }
        }
    });
}];

其他參考

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

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

  • 原文:AVFoundation Programming Guide 寫在前面 簡(jiǎn)單翻譯一下AVFoundation...
    朦朧1919閱讀 1,609評(píng)論 0 1
  • 近期一直搞視頻模塊的學(xué)習(xí)。蘋果也提供了AVFoundation這個(gè)強(qiáng)大的庫來支持開發(fā)。先通過文檔來了解下AVFou...
    HHHHHHHHHHD閱讀 901評(píng)論 0 1
  • About AVFoundation - AVFoundation概述 AVFoundation is one o...
    泥孩兒0107閱讀 400評(píng)論 0 0
  • 我想每個(gè)作者都應(yīng)該多了解角色的概念,這樣才能抓住寫作的真諦,創(chuàng)造出鮮活的生命。愿你筆下的人都能在另一個(gè)世界平平淡淡...
    if媿閱讀 801評(píng)論 0 3
  • 這周是假期的第一周,每天都是美好的完美的有規(guī)律做每一件事,我做的事情都是這些和這樣的。 我每天早上我都是先從奶奶家...
    山西地之錦閱讀 491評(píng)論 0 0