ios - 如何在 IOS 中制作慢动作视频

标签 ios objective-c video ffmpeg avfoundation

我必须在视频文件中与音频一起在某些帧之间执行“慢动作”,并且需要将渐变视频存储作为新视频。

引用: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 Foundation 使用组合从现有媒体片段(通常是一个或多个视频和音频轨道)创建新 Assets 。您使用可变组合来添加和删除轨道,并调整它们的时间顺序。您还可以设置音轨的相对音量和斜坡;并设置视频轨道的不透明度和不透明度渐变。合成是存储在内存中的媒体片段的集合。当您使用导出 session 导出合成时,它会折叠成一个文件。 在 iOS 4.1 及更高版本上,您还可以使用 Assets 编写器从样本缓冲区或静止图像等媒体创建 Assets 。

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

更新::AV 导出 session 代码:

 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 中)

关于ios - 如何在 IOS 中制作慢动作视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17296999/

相关文章:

iphone - 为什么SBJson JSON解析只得到最后一个感兴趣的key?

ios - 使用 View Controller Containment 的缺点

iOS - UITableViewCell 的隐藏 UIImageView 是否应该在可见之前没有分配的图像?

ios - 如何使 uiscrollview 仅适用于 ios 的垂直滚动?

python - 如何将python中的async = false添加到Gstreamer splitmuxsink元素音频/视频/H264组合

ios - 通过 iOS App 在 Twitter 上分享视频

iOS 如何在 UITextView 中内联插入 UITextField

ios - 创建带轨道的 GPX 文件的简单方法

ios - 保存视频到图库,获取视频存放到图库的路径

ios - 如何使用 NSRegularExpression 提取字符串的特定部分?