2.1 APP啟動(dòng)拍攝
首先包含七牛短視頻 SDK 頭文件 PLShortVideoKit.h :
#import <PLShortVideoKit/PLShortVideoKit.h>
然后添加一個(gè) PLShortVideoRecorder 屬性:
@property (nonatomic,strong) PLShortVideoRecorder *shortVideoRecorder;
創(chuàng)建音視頻的采集和編碼配置對(duì)象,這里我們使用默認(rèn)配置,開(kāi)發(fā)者可以根據(jù)自己的需求修改配置:
PLSVideoConfiguration *videoConfiguration = [PLSVideoConfiguration defaultConfiguration]; PLSAudioConfiguration *audioConfiguration = [PLSAudioConfiguration defaultConfiguration];
創(chuàng)建拍攝 shortVideoRecorder 對(duì)象:
self.shortVideoRecorder = [[PLShortVideoRecorder alloc] initWithVideoConfiguration:videoConfiguration audioConfiguration:audioConfiguration]; self.shortVideoRecorder.delegate = self;
添加攝像頭預(yù)覽視圖:
[self.view addSubview:self.shortVideoRecorder.previewView];
至此,基本配置完成,我們可以啟動(dòng)攝像頭預(yù)覽:
[self.shortVideoRecorder startCaptureSession];
2.2 APP給拍攝添加背景音樂(lè)
在開(kāi)始錄制之前,我們可以添加背景音樂(lè):
NSURL *audioURL = [NSURL fileURLWithString:@"PATH TO AUDIO"]; [self.shortVideoRecorder mixAudio:audioURL];
背景音樂(lè)只能在開(kāi)始錄制之前添加,一旦錄制開(kāi)始了,不能再添加,此時(shí)只有刪除已經(jīng)錄制的視頻文件,才能添加背景音樂(lè)。
2.3 APP開(kāi)始拍攝
錄制的視頻存放路徑由 SDK 內(nèi)部自動(dòng)生成:
[self.shortVideoRecorder startRecording];
開(kāi)發(fā)者也可以自己傳入錄制的視頻存放路徑:
[self.shortVideoRecorder startRecording:customFileURL];
2.4 APP添加美顏
七牛短視頻 SDK 提供了美顏功能,開(kāi)發(fā)者只需要一個(gè)簡(jiǎn)單的參數(shù)設(shè)置即可以打開(kāi)美顏功能:
[self.shortVideoRecorder setBeautifyModeOn:YES];
2.5 APP添加濾鏡
七牛短視頻 SDK 內(nèi)部提供了 30 多種濾鏡格式,開(kāi)發(fā)者使用濾鏡需要在工程中包含 PLShortVideoKit.bundle,這里面存放了濾鏡的圖片資源,開(kāi)發(fā)者還可以添加自己的濾鏡圖片。
初始化濾鏡:
// 初始化濾鏡 self.filter = [[PLSFilter alloc] init]; // 設(shè)置濾鏡色彩圖片路徑 NSString *bundlePath = [NSBundle mainBundle].bundlePath; NSString *colorImagePath = [bundlePath stringByAppendingString:@"/PLShortVideoKit.bundle/colorFilter/candy/filter.png"]; self.filter.colorImagePath = colorImagePath;
在短視頻數(shù)據(jù)回調(diào)方法中,我們可以用上面初始化好的濾鏡:
- (CVPixelBufferRef)shortVideoRecorder:(PLShortVideoRecorder *)recorder cameraSourceDidGetPixelBuffer:(CVPixelBufferRef)pixelBuffer { // 進(jìn)行濾鏡處理 pixelBuffer = [self.filter process:pixelBuffer]; return pixelBuffer; }
2.6 短視頻app添加人臉貼紙
七牛短視頻 SDK 沒(méi)有提供人臉識(shí)別的貼紙功能,但是我們 SDK 能很容易的接入友商的貼紙功能,我們以添加 涂圖 的貼紙舉例說(shuō)明
包含涂圖的頭文件:
#import <TuSDKVideo/TuSDKVideo.h> #import "StickerScrollView.h"
增加貼紙?zhí)砑悠骱唾N紙選擇 view:
@property (nonatomic, strong) TuSDKFilterProcessor *filterProcessor; @property (nonatomic, strong) StickerScrollView *stickerView;
初始化貼紙?zhí)砑拥膶?shí)例:
self.filterProcessor = [[TuSDKFilterProcessor alloc] initWithFormatType:kCVPixelFormatType_32BGRA isOriginalOrientation:NO]; self.filterProcessor.outputPixelFormatType = lsqFormatTypeBGRA; // TuSDKFilterProcessor 默認(rèn)不啟用貼紙,這里需要主動(dòng)啟用貼紙功能 [self.filterProcessor setEnableLiveSticker:YES]; self.stickerView = [[StickerScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)]; self.stickerView.stickerDelegate = self; self.stickerView.cameraStickerType = lsqCameraStickersTypeSquare;
選擇貼紙。在貼紙選擇的回調(diào),處理貼紙:
(void)clickStickerViewWith:(TuSDKPFStickerGroup *)stickGroup { if (!stickGroup) { // 為nil時(shí) 移除已有貼紙組; [_filterProcessor removeMediaEffectsWithType:TuSDKMediaEffectDataTypeSticker]; } else { // 選中了某個(gè)貼紙,將其添加到 filterProcessor 中 [self.filterProcessor showGroupSticker:stickGroup]; } }
貼紙選擇完成之后,我們可以將貼紙應(yīng)用到視頻錄制中。和濾鏡處理類似,在短視頻拍攝的視頻數(shù)據(jù)回調(diào)中,應(yīng)用貼紙:
- (CVPixelBufferRef)shortVideoRecorder:(PLShortVideoRecorder *)recorder cameraSourceDidGetPixelBuffer:(CVPixelBufferRef)pixelBuffer { // 進(jìn)行濾鏡處理 pixelBuffer = [self.filter process:pixelBuffer]; // TuSDK 進(jìn)行貼紙?zhí)幚? pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
2.7 短視頻app分段變速拍攝
如果想拍攝的視頻以快速或者慢速播放,可以設(shè)置拍攝速率:
self.shortVideoRecorder.recoderRate = PLSVideoRecoderRateTopFast;
2.8 短視頻app結(jié)束拍攝
如果要結(jié)束某一段視頻的錄制,可以調(diào)用停止錄制方法:
[self.shortVideoRecorder stopRecording];
調(diào)用停止錄制之后,保存的視頻會(huì)通過(guò)錄制完成回調(diào)返回出來(lái):
- (void)shortVideoRecorder:(PLShortVideoRecorder *)recorder didFinishRecordingToOutputFileAtURL:(NSURL *)fileURL fileDuration:(CGFloat)fileDuration totalDuration:(CGFloat)totalDuration { }
停止音視頻采集。如果不再需要拍攝視頻,可以調(diào)用停止采集方法來(lái)結(jié)束拍攝:
[self.shortVideoRecorder stopCaptureSession];
3.1 短視頻app開(kāi)始編輯
編輯類 PLShortVideoEditor 支持渲染音視頻、水印、濾鏡、背景音樂(lè)、MV 特效等功能
初始化和啟動(dòng)編輯:
self.shortVideoEditor = [[PLShortVideoEditor alloc] initWithAsset:asset videoSize:CGSizeZero]; self.shortVideoEditor.delegate = self; self.shortVideoEditor.loopEnabled = YES; [self.view addSubview:self.shortVideoEditor.preview]; [self.shortVideoEditor startEditing];
3.2 短視頻app添加背景音樂(lè)
添加背景音樂(lè)
[self.shortVideoEditor addMusic:musicURL timeRange:timeRange volume:1.0];
調(diào)節(jié)背景音樂(lè)音量
[self.shortVideoEditor updateMusic:timeRange volume:0.5];
3.3 短視頻app添加文字特效
添加文字的邏輯和添加貼紙使用的是同一個(gè)邏輯,用戶可以使用七牛短視頻 Demo 中已經(jīng)包裝好的添加文字、貼紙的類 PLSStickerView:
PLSStickerView *stickerView = [[PLSStickerView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)]; // 以字典的形式,保存 stickerView 信息 NSMutableDictionary *stickerSettings = [[NSMutableDictionary alloc] init]; stickerSettings[PLSStickerKey] = stickerView; stickerSettings[PLSSizeKey] = [NSValue valueWithCGSize:viewSize]; stickerSettings[PLSPointKey] = [NSValue valueWithCGPoint:viewPoint]; CGFloat rotation = atan2f(transform.b, transform.a); rotation = rotation * (180 / M_PI); stickerSettings[PLSRotationKey] = [NSNumber numberWithFloat:rotation]; [self.stickerSettingsArray addObject:stickerSettings];
3.4 短視頻app添加抖音特效
七牛短視頻 SDK 沒(méi)有集成特效,但是和人臉貼紙一樣,可以輕松的接入第三方的特效。下面我們還以添加 涂圖 的特效為例,演示特效的添加方式
首先,初始化特效添加器:
self.filterProcessor = [[TuSDKFilterProcessor alloc] initWithFormatType:kCVPixelFormatType_32BGRA isOriginalOrientation:isOriginalOrientation]; self.filterProcessor.delegate = self; self.filterProcessor.mediaEffectDelegate = self; // 默認(rèn)關(guān)閉動(dòng)態(tài)貼紙功能,即關(guān)閉人臉識(shí)別功能 self.filterProcessor.enableLiveSticker = NO;
添加靈魂出竅特效:
TuSDKMediaSceneEffectData *effectData = [[TuSDKMediaSceneEffectData alloc] initWithEffectsCode:@"LiveSoulOut01"]; effectData.atTimeRange = [TuSDKTimeRange makeTimeRangeWithStart:kCMTimeZero end:CMTimeMake(INTMAX_MAX, 1)]; [self.filterProcessor addMediaEffect:effectData];
應(yīng)用特效。在短視頻編輯視頻數(shù)據(jù)回調(diào)里面,將特效應(yīng)用,以便預(yù)覽特效效果:
- (CVPixelBufferRef)shortVideoEditor:(PLShortVideoEditor *)editor didGetOriginPixelBuffer:(CVPixelBufferRef)pixelBuffer timestamp:(CMTime)timestamp { // 應(yīng)用特效 pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer frameTime:timestamp]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
上面我們的短視頻編輯就完成了,現(xiàn)在我們將編輯的視頻使用 PLSAVAssetExportSession
導(dǎo)出來(lái)保存到本地相冊(cè)
初始化視頻導(dǎo)出器:
NSMutableDictionary *outputSettings = [[NSMutableDictionary alloc] init]; NSMutableDictionary *movieSettings = [[NSMutableDictionary alloc] init]; NSMutableDictionary *backgroundAudioSettings = [NSMutableDictionary alloc] init]; NSMutableArray *stickerSettingsArray = [[NSMutableArray alloc] init]; NSMutableArray *audioSettingsArray = [[NSMutableArray alloc] init]; // 將導(dǎo)出信息設(shè)置到 outputSettings 中 // do setting... AVAsset *asset = movieSettings[PLSAssetKey]; PLSAVAssetExportSession *exportSession = [[PLSAVAssetExportSession alloc] initWithAsset:asset]; exportSession.outputFileType = PLSFileTypeMPEG4; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.outputSettings = self.outputSettings; exportSession.delegate = self; exportSession.isExportMovieToPhotosAlbum = YES;
設(shè)置導(dǎo)出視頻相關(guān) block:
__weak typeof(self) weakSelf = self; [exportSession setCompletionBlock:^(NSURL *url) { NSLog(@"視頻已保存到相冊(cè)中"); }]; [exportSession setFailureBlock:^(NSError *error) { NSLog(@"導(dǎo)出視頻失敗"); }]; [exportSession setProcessingBlock:^(float progress) { NSLog(@"視頻導(dǎo)出完成百分比: %f", process); }];
實(shí)現(xiàn) PLSAVAssetExportSessionDelegate 的視頻數(shù)據(jù)回調(diào)方法,進(jìn)行特效處理:
- (CVPixelBufferRef)assetExportSession:(PLSAVAssetExportSession *)assetExportSession didOutputPixelBuffer:(CVPixelBufferRef)pixelBuffer timestamp:(CMTime)timestamp { // 特效處理 pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer frameTime:timestamp]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
短視頻app開(kāi)發(fā),北京短視頻app制作專業(yè)技術(shù)流程,掌握以上短視頻app技術(shù)開(kāi)發(fā)要點(diǎn),開(kāi)發(fā)一個(gè)優(yōu)秀的短視頻app絕對(duì)沒(méi)有問(wèn)題。短視頻直播帶貨app開(kāi)發(fā),2020年4月,非常流行短視頻帶貨直播電商app市場(chǎng)火爆,非常值得進(jìn)入的短視頻朝陽(yáng)產(chǎn)業(yè)。
接入ChatGPT系統(tǒng)多少錢?
APP如何接入ChatGPT系
小程序開(kāi)發(fā)費(fèi)用,開(kāi)發(fā)小程序需要
怎么選擇一家靠譜的小程序制作公
app制作流程-策略|評(píng)估規(guī)劃
開(kāi)發(fā)一個(gè)app多少錢?
客服QQ:121446412 聯(lián)系電話:15321250321
京ICP備17026149號(hào)-1版權(quán)所有@2011-2022 北京天品互聯(lián)科技有限公司 公司地址:北京市海淀區(qū)上地信息路甲28號(hào)B座(二層)02D室-010號(hào)