給視頻加水印

#import ?<Photos/Photos.h>

#import <AssetsLibrary/AssetsLibrary.h>

#import "GPUImage.h"



@property (nonatomic, strong) GPUImageMovie *movieFile;

@property (nonatomic, strong) GPUImageMovieWriter? ? ? *movieWriter;

@property (nonatomic, strong) GPUImageNormalBlendFilter *filter;



//網(wǎng)絡(luò)視頻保存到本地

-(void)saveVideo{

?? ?dispatch_queue_t queue = dispatch_queue_create("hmj", DISPATCH_QUEUE_CONCURRENT);

? ? dispatch_async(queue, ^{

? ? ? ? NSLog(@"我是第一個(gè)任務(wù), 當(dāng)前線程:%@ ,是否主線程%d",[NSThread currentThread],[NSThread isMainThread]);

? ? ? ? NSString *filePath;

? ? ? ? NSDate *date = [NSDate date];

? ? ? ? NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.videoModel.videoUrl]];

? ? ? ? NSURLResponse *response;

? ? ? ? NSError *error;

? ? ? ? NSData* result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

? ? ? ? NSLog(@"Response expects %lld bytes", [response expectedContentLength]);

? ? ? ? NSLog(@"Response suggested file name: %@", [response suggestedFilename]);

? ? ? ? if ([response suggestedFilename])

? ? ? ? ? ? //? ? ? ? ? ? self.savePath = [DEST_PATH stringByAppendingString:[response suggestedFilename]];


? ? ? ? ? ? if (!result)

? ? ? ? ? ? ? ? NSLog(@"Error downloading data: %@.", [error localizedDescription]);

? ? ? ? ? ? else if (response.expectedContentLength < 0)

? ? ? ? ? ? ? ? NSLog(@"Error with download. Carrier redirect?");

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSLog(@"Download succeeded.");

? ? ? ? ? ? ? ? NSLog(@"Read %ld bytes", result.length);

? ? ? ? ? ? ? ? NSLog(@"Elapsed time: %0.2f seconds.", -1*[date timeIntervalSinceNow]);


? ? ? ? ? ? ? ? NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

? ? ? ? ? ? ? ? filePath = [NSString stringWithFormat:@"%@/video.mp4",docDirPath];


? ? ? ? ? ? ? ? if ([result writeToFile:filePath atomically:YES]) {

? ? ? ? ? ? ? ? ? ? NSLog(@"succeed");

? ? ? ? ? ? ? ? ? ? [self saveVideo:filePath];

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? NSLog(@"faild");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }


? ? });

}


//videoPath為視頻下載到本地之后的本地路徑

- (void)saveVideo:(NSString *)videoPath{

? ? if (videoPath) {

//加水印

? ? ? ? NSURL *videoURL = [NSURL fileURLWithPath:videoPath];

? ? ? ? [self saveVedioPath:videoURL WithWaterImg:[UIImage imageNamed:@"wylb"] WithCoverImage:[UIImage imageNamed:@"wylb"] WithQustion:[NSString stringWithFormat:@"令令號:%@",[LLManagerTool getUserRequireId]] WithFileName:@"waterVideo"];

//不加水印直接保存

//? ? ? ? if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)) {

//? ? ? ? ? ? //保存相冊核心代碼

//? ? ? ? ? ? UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

//? ? ? ? }


? ? }


}

//不加水印

-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo{

? ? if (error) {

? ? ? ? NSLog(@"保存視頻失敗%@", error.localizedDescription);


? ? ? ? if (_isShowBlock) {

? ? ? ? ? ? _isShowBlock(NO,@"保存失敗",@"保存至相冊");

? ? ? ? }

? ? }

? ? else {

? ? ? ? NSLog(@"保存視頻成功");

? ? ? ? if (_isShowBlock) {

? ? ? ? ? ? _isShowBlock(NO,@"保存成功",@"保存至相冊");

? ? ? ? }

? ? }


}


//加水印保存在本地

-(void)saveVedioPath:(NSURL*)vedioPath WithWaterImg:(UIImage*)img WithCoverImage:(UIImage*)coverImg WithQustion:(NSString*)question WithFileName:(NSString*)fileName

{

? ? // 濾鏡

? ? //? ? filter = [[GPUImageDissolveBlendFilter alloc] init];

? ? //? ? [(GPUImageDissolveBlendFilter *)filter setMix:0.0f];

? ? //也可以使用透明濾鏡

? ? //? ? filter = [[GPUImageAlphaBlendFilter alloc] init];

? ? //? ? //mix即為疊加后的透明度,這里就直接寫1.0了

? ? //? ? [(GPUImageDissolveBlendFilter *)filter setMix:1.0f];

?? ?_filter = [[GPUImageNormalBlendFilter alloc] init];

? ? NSURL *sampleURL? = vedioPath;

? ? AVAsset *asset = [AVAsset assetWithURL:sampleURL];

? ? CGSize size = asset.naturalSize;

? ? _movieFile = [[GPUImageMovie alloc] initWithAsset:asset];

? ? _movieFile.playAtActualSpeed = NO;

? ? // 文字水印

? ? UILabel *label = [[UILabel alloc] init];

? ? label.text = question;

? ? label.font = [UIFont systemFontOfSize:15];

? ? label.textColor = [UIColor whiteColor];

? ? [label setTextAlignment:NSTextAlignmentRight];

? ? [label sizeToFit];

//? ? label.layer.masksToBounds = YES;

//? ? label.layer.cornerRadius = 18.0f;

? ? [label setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];

? ? [label setFrame:CGRectMake(SCREEN_WIDTH-label.size.width-20, SCREEN_HEIGHT-50, label.size.width, 20)];

? ? //圖片水印

? ? UIImage *coverImage1 = [img copy];

? ? UIImageView *coverImageView1 = [[UIImageView alloc] initWithImage:coverImage1];

? ? [coverImageView1 setFrame:CGRectMake(SCREEN_WIDTH - 131, SCREEN_HEIGHT-110, 111, 27)];

? ? //第二個(gè)圖片水印

? ? UIImage *coverImage2 = [coverImg copy];

? ? UIImageView *coverImageView2 = [[UIImageView alloc] initWithImage:coverImage2];

? ? [coverImageView2 setFrame:CGRectMake(270, 100, 210, 50)];

? ? UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];

? ? subView.backgroundColor = [UIColor clearColor];

? ? [subView addSubview:coverImageView1];

//? ? [subView addSubview:coverImageView2];

? ? [subView addSubview:label];

? ? [coverImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {

? ? ? ? make.left.top.offset(20);

? ? ? ? make.height.offset(20);

? ? ? ? make.width.offset(50);

? ? }];

? ? [label mas_makeConstraints:^(MASConstraintMaker *make) {

? ? ? ? make.top.equalTo(coverImageView1.mas_bottom).offset(10);

? ? ? ? make.left.offset(20);

? ? }];

? ? GPUImageUIElement *uielement = [[GPUImageUIElement alloc] initWithView:subView];

? ? NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.mp4",fileName]];

? ? unlink([pathToMovie UTF8String]);

? ? NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

? ? _movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(SCREEN_WIDTH , SCREEN_HEIGHT)];

? ? GPUImageFilter* progressFilter = [[GPUImageFilter alloc] init];

? ? [progressFilter addTarget:_filter];

? ? [_movieFile addTarget:progressFilter];

? ? [uielement addTarget:_filter];

? ? _movieWriter.shouldPassthroughAudio = YES;

? ? //? ? movieFile.playAtActualSpeed = true;

? ? if ([[asset tracksWithMediaType:AVMediaTypeAudio] count] > 0){

? ? ? ? _movieFile.audioEncodingTarget = _movieWriter;

? ? } else {//no audio

? ? ? ? _movieFile.audioEncodingTarget = nil;

? ? }

? ? [_movieFile enableSynchronizedEncodingUsingMovieWriter:_movieWriter];

? ? // 顯示到界面

? ? [_filter addTarget:_movieWriter];

? ? [_movieWriter startRecording];

? ? [_movieFile startProcessing];

?? ?//? ? dlink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateProgress)];

? ? //? ? [dlink setFrameInterval:15];

? ? //? ? [dlink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

? ? //? ? [dlink setPaused:NO];

? ? __weak typeof(self) weakSelf = self;

? ? //渲染

? ? [progressFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {

? ? ? ? //水印可以移動

//? ? ? ? CGRect frame = coverImageView1.frame;

//? ? ? ? frame.origin.x += 1;

//? ? ? ? frame.origin.y += 1;

//? ? ? ? coverImageView1.frame = frame;

? ? ? ? //第5秒之后隱藏coverImageView2

//? ? ? ? if (time.value/time.timescale>=5.0) {

//? ? ? ? ? ? [coverImageView2 removeFromSuperview];

//? ? ? ? }

? ? ? ? [uielement update];


? ? }];

? ? //保存相冊

? ? [_movieWriter setCompletionBlock:^{

? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

//? ? ? ? ? ? __block typeof(self) weakSelf = self;

//? ? ? ? ? ? __strong typeof(self) strongSelf = weakSelf;

? ? ? ? ? ? [weakSelf.filter removeTarget:weakSelf.movieWriter];

? ? ? ? ? ? [weakSelf.movieWriter finishRecording];

? ? ? ? ? ? __block PHObjectPlaceholder *placeholder;

? ? ? ? ? ? if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathToMovie))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? NSError *error;

? ? ? ? ? ? ? ? [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{

? ? ? ? ? ? ? ? ? ? PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:movieURL];

? ? ? ? ? ? ? ? ? ? placeholder = [createAssetRequest placeholderForCreatedAsset];

? ? ? ? ? ? ? ? } error:&error];

? ? ? ? ? ? ? ? if (error) {

? ? ? ? ? ? ? ? ? ? [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else{

? ? ? ? ? ? ? ? ? ? [SVProgressHUD showSuccessWithStatus:@"視頻已經(jīng)保存到相冊"];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? });

? ? }];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末加矛,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子垃你,更是在濱河造成了極大的恐慌私杜,老刑警劉巖象泵,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡炕桨,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門肯腕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來献宫,“玉大人,你說我怎么就攤上這事实撒℃⑼荆” “怎么了涉瘾?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長捷兰。 經(jīng)常有香客問我立叛,道長,這世上最難降的妖魔是什么贡茅? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任秘蛇,我火速辦了婚禮,結(jié)果婚禮上顶考,老公的妹妹穿的比我還像新娘赁还。我一直安慰自己,他們只是感情好驹沿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布艘策。 她就那樣靜靜地躺著,像睡著了一般渊季。 火紅的嫁衣襯著肌膚如雪柬焕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天梭域,我揣著相機(jī)與錄音斑举,去河邊找鬼。 笑死病涨,一個(gè)胖子當(dāng)著我的面吹牛富玷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播既穆,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼赎懦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了幻工?” 一聲冷哼從身側(cè)響起励两,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎囊颅,沒想到半個(gè)月后当悔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡踢代,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年盲憎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片胳挎。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡饼疙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出慕爬,到底是詐尸還是另有隱情窑眯,我是刑警寧澤屏积,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站磅甩,受9級特大地震影響肾请,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜更胖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望隔显。 院中可真熱鬧却妨,春花似錦、人聲如沸括眠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掷豺。三九已至捞烟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間当船,已是汗流浹背题画。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留德频,地道東北人苍息。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像壹置,于是被迫代替她去往敵國和親竞思。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

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