zoukankan      html  css  js  c++  java
  • 把视频文件拆成图片保存在沙盒中

    /**

     *  把视频文件拆成图片保存在沙盒中

     *

     *  @param fileUrl        本地视频文件URL

     *  @param fps            拆分时按此帧率进行拆分

     *  @param completedBlock 所有帧被拆完成后回调

     */

    - (void)splitVideo:(NSURL *)fileUrl fps:(float)fps completedBlock:(void(^)())completedBlock {

        if (!fileUrl) {

            return;

        }

        NSDictionary *optDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

        AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:fileUrl options:optDict];

        

        CMTime cmtime = avasset.duration; //视频时间信息结构体

        Float64 durationSeconds = CMTimeGetSeconds(cmtime); //视频总秒数

        

        NSMutableArray *times = [NSMutableArray array];

        Float64 totalFrames = durationSeconds * fps; //获得视频总帧数

        CMTime timeFrame;

        for (int i = 1; i <= totalFrames; i++) {

            timeFrame = CMTimeMake(i, fps); //i  帧率

            NSValue *timeValue = [NSValue valueWithCMTime:timeFrame];

            [times addObject:timeValue];

        }

        

        AVAssetImageGenerator *imgGenerator = [[AVAssetImageGenerator alloc] initWithAsset:avasset];

        //防止时间出现偏差

        imgGenerator.requestedTimeToleranceBefore = kCMTimeZero;

        imgGenerator.requestedTimeToleranceAfter = kCMTimeZero;

        NSInteger timesCount = [times count];

        [imgGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {

            printf("current-----: %lld ", requestedTime.value);

            switch (result) {

                case AVAssetImageGeneratorCancelled:

                    NSLog(@"Cancelled");

                    break;

                case AVAssetImageGeneratorFailed:

                    NSLog(@"Failed");

                    break;

                case AVAssetImageGeneratorSucceeded: {

                    if (requestedTime.value == timesCount) {

                        NSLog(@"completed");

                        if (completedBlock) {                        completedBlock();

                        }

                    }

                }

                    break;

            }

        }];

    }

  • 相关阅读:
    cstring string 比较之二(学习笔记)
    转 大话设计模式学习笔记(思维导图) 更新中
    转 十三种设计模式的思维导图
    (转)关于栈、堆、静态存储区最大可分配大小的探讨 海量之一
    如何学习网络协议(学习笔记)
    境界篇:linux 驱动开发的境界(学习笔记)
    b.关于vc编程境界的讨论
    关于编程境界的小结
    Java异常(一) Java异常简介及其架构
    多线程简单阐述
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/6879086.html
Copyright © 2011-2022 走看看