GPUImage解析(十一) —— 一個簡單的實例之視頻的疊加混合(六)

版本記錄

版本號 時間
V1.0 2017.09.06

前言

GPUImage是直接利用顯卡實現(xiàn)視頻或者圖像處理的技術卫枝。感興趣可以看上面幾篇文章。
1. GPUImage解析(一) —— 基本概覽(一)
2. GPUImage解析(二) —— 基本概覽(二)
3. GPUImage解析(三) —— 基本概覽(三)
4. GPUImage解析(四) —— 安裝方法及框架介紹
5. GPUImage解析(五) —— 框架中的幾個基類
6. GPUImage解析(六) —— 一個簡單的實例(一)
7. GPUImage解析(七) —— 一個簡單的實例結(jié)合GPUImageVideoCamera(二)
8. GPUImage解析(八) —— 一個簡單的實例之多濾鏡視頻采集存儲(三)
9. GPUImage解析(九) —— 一個簡單的實例之GPUImageTiltShiftFilter濾鏡處理(四)
10. GPUImage解析(十) —— 一個簡單的實例之實時更改視頻的濾鏡值(五)

功能要求

實現(xiàn)給定的視頻和攝像頭采集視頻的混合疊加末早。


功能實現(xiàn)

下面我們還是老規(guī)矩看代碼。

1. JJGPUImageMPVC.h
#import <UIKit/UIKit.h>

@interface JJGPUImageMPVC : UIViewController

@end
2. JJGPUImageMPVC.m
#import "JJGPUImageMPVC.h"
#import "GPUImage.h"
#import "Masonry.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface JJGPUImageMPVC ()

@property (nonatomic, strong) GPUImageView *imageView;
@property (nonatomic, strong) GPUImageDissolveBlendFilter *blendFilter;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) GPUImageMovie *imageMovie;
@property (nonatomic, strong) GPUImageMovieWriter *movieWriter;
@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
@property (nonatomic, strong) GPUImageOutput *imageOut;
@property (nonatomic, copy) NSString *moviePath;
@property (nonatomic, strong) NSURL *movieURL;

@end

@implementation JJGPUImageMPVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self setupConfiguration];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - Object Private Function

- (void)setupConfiguration
{
    //實例化GPUImageDissolveBlendFilter
    self.blendFilter = [[GPUImageDissolveBlendFilter alloc] init];
    [self.blendFilter setMix:0.5];
    
    //播放視頻
    NSURL *demoURL = [[NSBundle mainBundle] URLForResource:@"lili" withExtension:@"mp4"];
    self.imageMovie = [[GPUImageMovie alloc] initWithURL:demoURL];
    self.imageMovie.runBenchmark = YES;
    self.imageMovie.playAtActualSpeed = YES;
    
    //實例化GPUImageVideoCamera
    self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
    //路徑
    NSString *moviePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
    unlink([moviePath UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
    self.moviePath = moviePath;
    self.movieURL = movieURL;
    
    //實例化GPUImageMovieWriter
    GPUImageMovieWriter *movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
    self.movieWriter = movieWriter;

    [self.videoCamera addTarget:self.blendFilter];
    [self.imageMovie addTarget:self.blendFilter];
    self.movieWriter.shouldPassthroughAudio = NO;
    self.imageMovie.audioEncodingTarget = self.movieWriter;
    self.movieWriter.encodingLiveVideo = NO;
    
    //開始并顯示
    [self.blendFilter addTarget:self.imageView];
    [self.blendFilter addTarget:self.movieWriter];
    
    [self.videoCamera startCameraCapture];
    [self.movieWriter startRecording];
    [self.imageMovie startProcessing];
    
    //定時
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkDidWorkUpdateProgress)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    link.paused = NO;
    
    //存儲
    __weak typeof(self) weakSelf = self;
    [movieWriter setCompletionBlock:^{
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf.blendFilter removeTarget:strongSelf.movieWriter];
        [strongSelf.movieWriter finishRecording];
        
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(strongSelf.moviePath))
        {
            [library writeVideoAtPathToSavedPhotosAlbum:strongSelf.movieURL completionBlock:^(NSURL *assetURL, NSError *error)
             {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     
                     if (error) {
                         NSLog(@"保存失敗");
                     }
                     else {
                         NSLog(@"保存成功");
                     }
                 });
             }];
        }
    }];
}

- (void)setupUI
{
    //實例化GPUImageView
    self.imageView = [[GPUImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.imageView];
    
    //實例化UILabel
    self.progressLabel = [[UILabel alloc] init];
    self.progressLabel.textColor = [UIColor redColor];
    self.progressLabel.font = [UIFont systemFontOfSize:20.0];
    self.progressLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.progressLabel];
    
    [self.progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.imageView);
        make.bottom.equalTo(self.imageView).offset(-20.0);
        make.height.equalTo(@25);
        make.width.equalTo(self.imageView);
    }];
}

#pragma mark - Action && Notification

- (void)linkDidWorkUpdateProgress
{
    self.progressLabel.text = [NSString stringWithFormat:@"Progress = %.1f", self.imageMovie.progress * 100];
}

@end

功能效果

下面我們就看一下實現(xiàn)效果。

給定視頻
本地視頻和攝像頭采集混合

可見竞滓,本地視頻和攝像頭采集后的視頻混合了沦偎。

后記

未完疫向,待續(xù)~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市豪嚎,隨后出現(xiàn)的幾起案子搔驼,更是在濱河造成了極大的恐慌,老刑警劉巖侈询,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舌涨,死亡現(xiàn)場離奇詭異,居然都是意外死亡扔字,警方通過查閱死者的電腦和手機囊嘉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進店門温技,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人扭粱,你說我怎么就攤上這事舵鳞。” “怎么了焊刹?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵系任,是天一觀的道長。 經(jīng)常有香客問我虐块,道長俩滥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任贺奠,我火速辦了婚禮霜旧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘儡率。我一直安慰自己挂据,他們只是感情好,可當我...
    茶點故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布儿普。 她就那樣靜靜地躺著崎逃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪眉孩。 梳的紋絲不亂的頭發(fā)上个绍,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機與錄音浪汪,去河邊找鬼巴柿。 笑死,一個胖子當著我的面吹牛死遭,可吹牛的內(nèi)容都是我干的广恢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼呀潭,長吁一口氣:“原來是場噩夢啊……” “哼钉迷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蜗侈,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤篷牌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后踏幻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡戳杀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年该面,在試婚紗的時候發(fā)現(xiàn)自己被綠了夭苗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,727評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡隔缀,死狀恐怖题造,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情猾瘸,我是刑警寧澤界赔,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站牵触,受9級特大地震影響淮悼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜揽思,卻給世界環(huán)境...
    茶點故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一袜腥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钉汗,春花似錦羹令、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至卢未,卻和暖如春肪凛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尝丐。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工显拜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人爹袁。 一個月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓远荠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親失息。 傳聞我的和親對象是個殘疾皇子譬淳,可洞房花燭夜當晚...
    茶點故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容