IOS 視頻分解圖片、圖片合成視頻

來源:互聯網
上載者:User

標籤:elb   效果   settings   cap   rate   2.3   generate   注意   scale   

在IOS視頻處理中,視頻分解圖片和圖片合成視頻是IOS視頻處理中常常遇到的問題。這篇部落格就這兩個部分對IOS視頻映像的相互轉換做一下分析。

(1)視頻分解圖片

這裡視頻分解圖片使用的是AVAssetImageGenerator。利用這個class能夠非常方便的實現不同一時候間戳下,視訊框架的抓取。注意一般這樣的視頻分解圖片幀的方法都是放在子線程中的,而UI更新操作都是放在主線程中的。

以下來看看核心代碼:

    _imageGenerator = [[AVAssetImageGeneratoralloc] initWithAsset:_asset];

    images = [[NSMutableArrayalloc]initWithCapacity:10];

    _imageGenerator.maximumSize =THUMBNAIL_SIZE;

   CMTime duration = _asset.duration;

   CMTimeValue intervalSeconds = duration.value /3;

    CMTime time = kCMTimeZero;

    NSMutableArray *times = [NSMutableArrayarray];

   for (NSUInteger i =0; i < 3; i++) {

        [timesaddObject:[NSValuevalueWithCMTime:time]];

        time =CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));

    }

    [_imageGeneratorgenerateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,                                                                                      CGImageRef cgImage,

    CMTime actualTime,   

    AVAssetImageGeneratorResult result,

   NSError *error) {

       if (cgImage) {

           UIImage *image = [UIImageimageWithCGImage:cgImage];

            [imagesaddObject:image];

        }

       if (images.count ==3) {

            dispatch_async(dispatch_get_main_queue(), ^{

               self.imageview1.image = [imagesobjectAtIndex:0];

               self.imageview2.image = [imagesobjectAtIndex:1];

               self.imageview3.image = [imagesobjectAtIndex:2];

            });

        }

    }];

分解之後的幀效果例如以下:                               圖片合成視頻效果例如以下:

    

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="311" height="574" >

    (2)圖片合成視頻

    圖片合成視頻的方法相對來說更加複雜一點。我們主要用到的class是這個:

AVAssetWriterInputPixelBufferAdaptor。

不同之處在於這裡我們還要設定圖片合成視頻的各種參數,比方幀率,編碼方式等等。

    2.1 設定檔案封裝類型

AVFileTypeQuickTimeMovie

   2.2 設定圖片格式

kCVPixelFormatType_32ARGB

    2.3 設定編碼方式、圖片尺寸

     NSDictionary *videoSettings =@{AVVideoCodecKey :AVVideoCodecH264,

                           AVVideoWidthKey : [NSNumbernumberWithInt:(int)width],

                           AVVideoHeightKey : [NSNumbernumberWithInt:(int)height]};

2.4 圖片合成開始

         CMTime lastTime =CMTimeMake(i, self.frameTime.timescale);

         CMTime presentTime =CMTimeAdd(lastTime, self.frameTime);

         [self.bufferAdapterappendPixelBuffer:sampleBuffer withPresentationTime:presentTime];





IOS 視頻分解圖片、圖片合成視頻

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.