媒体合成和编辑
2017-06-29 14:12
489 查看
媒体合成和编辑
组合媒体
AVFoundation有关资源组合的功能源于
AVAsset的子类AVComposition。一个组合就是将其他几种媒体资源组合成一个自定义的临时排列,再将这个临时排列视为一个可以呈现或处理的独立媒体项目。就比如
AVAsset对象,组合相当于包含了一个或多个给定类型的媒体轨道的容器。
AVComposition中轨道都是AVAssetTrack的子类AVCompositionTrack。一个组合轨道本身由一个或多个媒体片段组成,由AVCompositionTrackSegment类定义,代表这个组合中的实际媒体区域。
AVComposition和
AVCompositionTrack都是不可变对象,提供对资源的只读操作。这些对象提供了一个合适的接口让应用程序的一部分可以进行播放或处理。不过,当创建自己的组合时,就需要使用
AVMutableComposition和
AVMutableCompositionTrack所提供的可变子类。这些对象提供的接口需要操作轨道和轨道分段,这样就可以创建所需的临时排列了。
要创建自定义组合,需指定在将要添加到组合的源媒体的时间范围,还要指定要添加片段的每个轨道的位置。
AVMutableComposition和AVMutableCompositionTrack
AVMutableComposition
AVMutableComposition可以添加或删除
tracks,和添加、删除、缩放
time range。
AVMutableComposition把
assets集合在一起,如下所示:
创建Composition
使用
AVMutableComposition类创建
Composition,为把媒体数据添加到
composition中,需添加一个或多个由
AVMutableCompositionTrack类表示的
composition tracks
最简单的例子是使用一个
video track和一个
audio track创建一个可变的
composition,如下:
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];
当把一个
tracks添加到一个
composition中时,需提供一个
media type和一个
track ID
media type最常用的
media type的是
AVMediaTypeVideo和
AVMediaTypeAudio,不过也可使用AVMediaTypeSubtitle和AVMediaTypeText
与视听数据相关联的每个轨道都具有一个称为
track ID的唯一标识符。可使用kCMPersistentTrackID_Invalid来表示AVFoundation应该自动生成一个正确的轨道ID。
addMutableTrackWithMediaType:preferredTrackID:方法添加一个空的
track到
composition
添加数据到Composition
创建好composition后,就需要把媒体数据添加到对应的
track中,需通过
AVAsset对象。如下的例子,把添加2个不同的的
video asset tracks到同一个
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];
insertTimeRange:ofTrack:atTime:error:方法添加一个时间范围的
track
导出组合
使用AVAssetExportSession对象导出组合,设置其输出的URL,并设置其输出文件类型
// 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;
调用其exportAsynchronouslyWithCompletionHandler:方法开始导出过程,如下
首先在在主队列检查导出会话状态,如果导出成功完成,则将导出的视频文件写入
AssetsLibrary
[self.exportSession exportAsynchronouslyWithCompletionHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ AVAssetExportSessionStatus status = self.exportSession.status; if (status == AVAssetExportSessionStatusCompleted) { [self writeExportedVideoToAssetsLibrary]; } else { [UIAlertView showAlertWithTitle:@"Export Failed" message:@"The request export failed."]; } }); }]; ...... - (void)writeExportedVideoToAssetsLibrary { NSURL *exportURL = self.exportSession.outputURL; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:exportURL]) { [library writeVideoAtPathToSavedPhotosAlbum:exportURL completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { NSString *message = @"Unable to write to Photos library."; [UIAlertView showAlertWithTitle:@"Write Failed" message:message]; } [[NSFileManager defaultManager] removeItemAtURL:exportURL error:nil]; }]; } else { NSLog(@"Video could not be exported to assets library."); } self.exportSession = nil; }
例子
合成的例子,可参考Merging Audio and Video in Native iOS,一个简单的音频和视频合成的例子:另外在《AV Foundation开发秘籍》中也有介绍
资源
EditingMerging Audio and Video in Native iOS
相关文章推荐
- 媒体处理软件:MKVToolnix-MKV编辑合成软件
- 与小容探讨:Blog媒体和2.0时代的编辑
- 与小容探讨:Blog媒体和2.0时代的编辑
- android音频编辑(裁剪,合成)(1)
- 网站编辑与传统媒体编辑的区别及特点
- android音频编辑(裁剪,合成)(2)
- android视频的编辑(录制,裁剪,合成)(1)
- 多个应用程序同时编辑一个文件不同部分后能否合成?
- 与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成
- iOS开发 在图片上添加文字,图片合成文字,图片上添加富文本,美图秀秀,美颜相机文字编辑
- QLab for Mac(现场媒体编辑工具) v4.1.6最新破解版
- Android多媒体学习四:实现图像的编辑和合成
- 与小容探讨:Blog媒体和2.0时代的编辑
- 身份证合成编辑
- android视频的编辑(录制,裁剪,合成)(2)
- 实现图像的编辑和合成
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
- 能拯救媒体的,不是编辑记者而是“产品经理”
- android音频编辑(裁剪,合成)(1)
- 如何在30分钟之内编辑排版好文章并发布在30个媒体平台?