如何在IOS中做慢动作视频

前端之家收集整理的这篇文章主要介绍了如何在IOS中做慢动作视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须在视频文件中进行“慢动作”以及一些帧之间的音频,并且需要将斜视视频存储为新的视频.

参考号:http://www.youtube.com/watch?v=BJ3_xMGzauk(从0到10秒钟)

从我的分析,我发现AVFoundation框架可以是有帮助的.

参考:
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html

从上述链接复制并粘贴:


编辑
AV基金会使用组合从现有的媒体(通常是一个或多个视频和音轨)创建新的资产.您使用可变组合来添加删除曲目,并调整其时间顺序.您还可以设置音轨的相对音量和斜坡;并设置视频轨道的不透明度和不透明度斜坡.一个组合是在记忆体中保存的一组媒体.使用导出会话导出作品时,它会折叠到一个文件.
在iOS 4.1及更高版本上,您还可以使用资产编写器从介质(如样本缓冲区或静态图像)创建资源.

问题:
可以使用AVFoundation框架做“慢动作”视频/音频文件吗?还是有其他包装可用吗?如果我想单独处理音视频,请指导我怎么办?

AV导出会话更新::代码

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *outputURL = paths[0];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil];
    outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"];
    // Remove Existing File
    [manager removeItemAtPath:outputURL error:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:self.inputAsset presetName:AVAssetExportPresetLowQuality];
    exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; // output path;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]];
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:outputURL] completionBlock:^(NSURL *assetURL,NSError *error){
                if (error) {
                    NSLog(@"Video could not be saved");
                }
            }];
        } else {
            NSLog(@"error: %@",[exportSession error]);
        }
    }];

解决方法

您可以使用AVFoundation和CoreMedia框架来缩放视频.
看看AVMutableCompositionTrack方法
- (void)scaleTimeRange:(CMTimeRange)timeRange toDuration:(CMTime)duration;

样品:

AVURLAsset* videoAsset = nil; //self.inputAsset;

//create mutable composition
AVMutableComposition *mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *videoInsertError = nil;
BOOL videoInsertResult = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration)
                                                        ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                                         atTime:kCMTimeZero
                                                          error:&videoInsertError];
if (!videoInsertResult || nil != videoInsertError) {
    //handle error
    return;
}

//slow down whole video by 2.0
double videoScaleFactor = 2.0;
CMTime videoDuration = videoAsset.duration;

[compositionVideoTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero,videoDuration)
                           toDuration:CMTimeMake(videoDuration.value*videoScaleFactor,videoDuration.timescale)];

//export
AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                     presetName:AVAssetExportPresetLowQuality];

(也许也可以将来自videoAsset的音轨添加到mixComposition中)

原文链接:https://www.f2er.com/iOS/337551.html

猜你在找的iOS相关文章