GPUImage解析(十二) —— 一個(gè)簡單的實(shí)例之添加水釉┲瘛(七)

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.09.14

前言

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

功能要求

實(shí)現(xiàn)視頻添加水印的效果。


功能實(shí)現(xiàn)

下面看示例代碼霜定。

#import "JJGPUWatermarkVC.h"
#import "GPUImage.h"
#import <AssetsLibrary/ALAssetsLibrary.h>
#import "Masonry.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface JJGPUWatermarkVC ()

@property (nonatomic, strong) GPUImageMovie *imageMovie;
@property (nonatomic, strong) GPUImageOutput <GPUImageInput> *blendFilter;
@property (nonatomic, strong) GPUImageMovieWriter *movieWriter;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) GPUImageView *imageView;
@property (nonatomic, copy) NSString *pathStr;
@property (nonatomic, strong) NSURL *movieURL;

@end

@implementation JJGPUWatermarkVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupConfig];
}

- (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)setupUI
{
    //實(shí)例化GPUImageView
    self.imageView = [[GPUImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.imageView];
    
    //顯示label
    self.tipLabel = [[UILabel alloc] init];
    self.tipLabel.textColor = [UIColor blueColor];
    self.tipLabel.font = [UIFont systemFontOfSize:20.0];
    [self.view addSubview:self.tipLabel];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(20.0);
    }];
}

- (void)setupConfig
{
    //濾鏡
    self.blendFilter = [[GPUImageDissolveBlendFilter alloc] init];
    [(GPUImageDissolveBlendFilter *)self.blendFilter setMix:0.5];
    
    //播放
    NSURL *resourceURL = [[NSBundle mainBundle] URLForResource:@"lili" withExtension:@"mp4"];
    AVAsset *asset = [AVAsset assetWithURL:resourceURL];
    
    //實(shí)例化GPUImageMovie
    self.imageMovie = [[GPUImageMovie alloc] initWithAsset:asset];
    self.imageMovie.runBenchmark = YES;
    self.imageMovie.playAtActualSpeed = YES;
    
    //水印提示文字
    UILabel *watermarkLabel = [[UILabel alloc] init];
    watermarkLabel.text  = @"水印";
    watermarkLabel.font = [UIFont systemFontOfSize:30.0];
    watermarkLabel.textColor = [UIColor redColor];
    
    //水印圖片
    UIView *backView = [[UIView alloc] initWithFrame:self.view.frame];
    backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:backView];
    
    UIImageView *watermarkImageView = [[UIImageView alloc] init];
    watermarkImageView.image = [UIImage imageNamed:@"light_4"];
    watermarkImageView.center = CGPointMake(kHeight / 2, kWidth / 2);
    
    [backView addSubview:watermarkLabel];
    [backView addSubview:watermarkImageView];
    
    [watermarkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(backView);
        make.height.equalTo(@(50.0));
        make.width.equalTo(@(50.0));
    }];

    [watermarkLabel sizeToFit];
    [watermarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(backView);
    }];

    //實(shí)例化GPUImageUIElement
    GPUImageUIElement *uiElement = [[GPUImageUIElement alloc] initWithView:backView];

    //存儲(chǔ)相關(guān)
    NSString *pathStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
    unlink([pathStr UTF8String]);
    self.pathStr = pathStr;
    NSURL *movieURL = [NSURL fileURLWithPath:pathStr];
    self.movieURL = movieURL;
    
    //實(shí)例化GPUImageMovieWriter
    self.movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
    
    GPUImageFilter *imageFilter = [[GPUImageFilter alloc] init];
    [self.imageMovie addTarget:imageFilter];
    [imageFilter addTarget:self.blendFilter];
    [uiElement addTarget:self.blendFilter];
    
    self.movieWriter.shouldPassthroughAudio = YES;
    [self.imageMovie enableSynchronizedEncodingUsingMovieWriter:self.movieWriter];
    
    //顯示
    [self.blendFilter addTarget:self.imageView];
    [self.blendFilter addTarget:self.movieWriter];
    [self.movieWriter startRecording];
    [self.imageMovie startProcessing];
    
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayProgress)];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    displayLink.paused = NO;
    
    [imageFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time){
        CGRect frame = watermarkImageView.frame;
        frame.origin.x += 0.5;
        frame.origin.y += 0.5;
        watermarkImageView.frame = frame;
        [uiElement updateWithTimestamp:time];
    }];
    
    //存儲(chǔ)
    __weak typeof(self) weakSelf = self;
    [self.movieWriter setCompletionBlock:^{
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf.blendFilter removeTarget:strongSelf.movieWriter];
        [strongSelf.movieWriter finishRecording];
        
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(strongSelf.pathStr))
        {
            [library writeVideoAtPathToSavedPhotosAlbum:strongSelf.movieURL completionBlock:^(NSURL *assetURL, NSError *error)
             {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     
                     if (error) {
                         NSLog(@"保存失敗");
                     }
                     else {
                         NSLog(@"保存成功");
                     }
                 });
             }];
        }
    }];
}

#pragma mark - Action && Notification

- (void)displayProgress
{
    NSLog(@"self.imageMovie.progress = %f", self.imageMovie.progress);
    self.tipLabel.text = [NSString stringWithFormat:@"Progress = %.2f%%",self.imageMovie.progress * 100];
    [self.tipLabel sizeToFit];
}

@end

功能效果

下面我們就給出幾個(gè)效果圖磨德。分別是錄制時(shí)和存儲(chǔ)在相冊回放時(shí)候的效果。

后記

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拙寡,一起剝皮案震驚了整個(gè)濱河市肆糕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌诚啃,老刑警劉巖私沮,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仔燕,死亡現(xiàn)場離奇詭異,居然都是意外死亡涨享,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門奔脐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來髓迎,“玉大人建丧,你說我怎么就攤上這事◆嶂欤” “怎么了尺铣?”我有些...
    開封第一講書人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵凛忿,是天一觀的道長竞川。 經(jīng)常有香客問我,道長床牧,這世上最難降的妖魔是什么遭贸? 我笑而不...
    開封第一講書人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮除秀,結(jié)果婚禮上算利,老公的妹妹穿的比我還像新娘。我一直安慰自己暂吉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開白布慕的。 她就那樣靜靜地躺著肮街,像睡著了一般判导。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上眼刃,一...
    開封第一講書人閱讀 52,184評(píng)論 1 308
  • 那天擂红,我揣著相機(jī)與錄音,去河邊找鬼。 笑死肯适,一個(gè)胖子當(dāng)著我的面吹牛赴恨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼钳垮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了歧焦?” 一聲冷哼從身側(cè)響起肚医,我...
    開封第一講書人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎舰涌,沒想到半個(gè)月后你稚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搁痛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年鸡典,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片彻况。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疗垛,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出贷腕,到底是詐尸還是另有隱情,我是刑警寧澤瞒斩,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布涮总,位于F島的核電站,受9級(jí)特大地震影響瀑梗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谤职,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一亿鲜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧饶套,春花似錦垒探、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽咖耘。三九已至撬码,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呜笑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來泰國打工凰慈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留驼鹅,地道東北人森篷。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓仲智,卻偏偏與公主長得像,于是被迫代替她去往敵國和親钓辆。 傳聞我的和親對象是個(gè)殘疾皇子肴焊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

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