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;

            }

        }];

    }

  • 相关阅读:
    Finding Lines
    2020-3-3 牛客试题复盘
    2020-3-2 牛客试题复盘
    2020-02-29(观看视频笔记)
    2020-02-29(观看视频笔记)
    2020-02-29(观看视频笔记)
    2020-02-28(观看视频笔记)
    2020-02-27(观看视频笔记)
    2020-2-27 牛客试题复盘
    2020-02-26(观看视频笔记)
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/6879086.html
Copyright © 2011-2022 走看看