2.1 APP啟動拍攝
首先包含七牛短視頻 SDK 頭文件 PLShortVideoKit.h :
#import <PLShortVideoKit/PLShortVideoKit.h>
然后添加一個 PLShortVideoRecorder 屬性:
@property (nonatomic,strong) PLShortVideoRecorder *shortVideoRecorder;
創建音視頻的采集和編碼配置對象,這里我們使用默認配置,開發者可以根據自己的需求修改配置:
PLSVideoConfiguration *videoConfiguration = [PLSVideoConfiguration defaultConfiguration]; PLSAudioConfiguration *audioConfiguration = [PLSAudioConfiguration defaultConfiguration];
創建拍攝 shortVideoRecorder 對象:
self.shortVideoRecorder = [[PLShortVideoRecorder alloc] initWithVideoConfiguration:videoConfiguration audioConfiguration:audioConfiguration]; self.shortVideoRecorder.delegate = self;
添加攝像頭預覽視圖:
[self.view addSubview:self.shortVideoRecorder.previewView];
至此,基本配置完成,我們可以啟動攝像頭預覽:
[self.shortVideoRecorder startCaptureSession];
2.2 APP給拍攝添加背景音樂
在開始錄制之前,我們可以添加背景音樂:
NSURL *audioURL = [NSURL fileURLWithString:@"PATH TO AUDIO"]; [self.shortVideoRecorder mixAudio:audioURL];
背景音樂只能在開始錄制之前添加,一旦錄制開始了,不能再添加,此時只有刪除已經錄制的視頻文件,才能添加背景音樂。
2.3 APP開始拍攝
錄制的視頻存放路徑由 SDK 內部自動生成:
[self.shortVideoRecorder startRecording];
開發者也可以自己傳入錄制的視頻存放路徑:
[self.shortVideoRecorder startRecording:customFileURL];
2.4 APP添加美顏
七牛短視頻 SDK 提供了美顏功能,開發者只需要一個簡單的參數設置即可以打開美顏功能:
[self.shortVideoRecorder setBeautifyModeOn:YES];
2.5 APP添加濾鏡
七牛短視頻 SDK 內部提供了 30 多種濾鏡格式,開發者使用濾鏡需要在工程中包含 PLShortVideoKit.bundle,這里面存放了濾鏡的圖片資源,開發者還可以添加自己的濾鏡圖片。
初始化濾鏡:
// 初始化濾鏡 self.filter = [[PLSFilter alloc] init]; // 設置濾鏡色彩圖片路徑 NSString *bundlePath = [NSBundle mainBundle].bundlePath; NSString *colorImagePath = [bundlePath stringByAppendingString:@"/PLShortVideoKit.bundle/colorFilter/candy/filter.png"]; self.filter.colorImagePath = colorImagePath;
在短視頻數據回調方法中,我們可以用上面初始化好的濾鏡:
- (CVPixelBufferRef)shortVideoRecorder:(PLShortVideoRecorder *)recorder cameraSourceDidGetPixelBuffer:(CVPixelBufferRef)pixelBuffer { // 進行濾鏡處理 pixelBuffer = [self.filter process:pixelBuffer]; return pixelBuffer; }
2.6 短視頻app添加人臉貼紙
七牛短視頻 SDK 沒有提供人臉識別的貼紙功能,但是我們 SDK 能很容易的接入友商的貼紙功能,我們以添加 涂圖 的貼紙舉例說明
包含涂圖的頭文件:
#import <TuSDKVideo/TuSDKVideo.h> #import "StickerScrollView.h"
增加貼紙添加器和貼紙選擇 view:
@property (nonatomic, strong) TuSDKFilterProcessor *filterProcessor; @property (nonatomic, strong) StickerScrollView *stickerView;
初始化貼紙添加的實例:
self.filterProcessor = [[TuSDKFilterProcessor alloc] initWithFormatType:kCVPixelFormatType_32BGRA isOriginalOrientation:NO]; self.filterProcessor.outputPixelFormatType = lsqFormatTypeBGRA; // TuSDKFilterProcessor 默認不啟用貼紙,這里需要主動啟用貼紙功能 [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;
選擇貼紙。在貼紙選擇的回調,處理貼紙:
(void)clickStickerViewWith:(TuSDKPFStickerGroup *)stickGroup { if (!stickGroup) { // 為nil時 移除已有貼紙組; [_filterProcessor removeMediaEffectsWithType:TuSDKMediaEffectDataTypeSticker]; } else { // 選中了某個貼紙,將其添加到 filterProcessor 中 [self.filterProcessor showGroupSticker:stickGroup]; } }
貼紙選擇完成之后,我們可以將貼紙應用到視頻錄制中。和濾鏡處理類似,在短視頻拍攝的視頻數據回調中,應用貼紙:
- (CVPixelBufferRef)shortVideoRecorder:(PLShortVideoRecorder *)recorder cameraSourceDidGetPixelBuffer:(CVPixelBufferRef)pixelBuffer { // 進行濾鏡處理 pixelBuffer = [self.filter process:pixelBuffer]; // TuSDK 進行貼紙處理 pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
2.7 短視頻app分段變速拍攝
如果想拍攝的視頻以快速或者慢速播放,可以設置拍攝速率:
self.shortVideoRecorder.recoderRate = PLSVideoRecoderRateTopFast;
2.8 短視頻app結束拍攝
如果要結束某一段視頻的錄制,可以調用停止錄制方法:
[self.shortVideoRecorder stopRecording];
調用停止錄制之后,保存的視頻會通過錄制完成回調返回出來:
- (void)shortVideoRecorder:(PLShortVideoRecorder *)recorder didFinishRecordingToOutputFileAtURL:(NSURL *)fileURL fileDuration:(CGFloat)fileDuration totalDuration:(CGFloat)totalDuration { }
停止音視頻采集。如果不再需要拍攝視頻,可以調用停止采集方法來結束拍攝:
[self.shortVideoRecorder stopCaptureSession];
3.1 短視頻app開始編輯
編輯類 PLShortVideoEditor 支持渲染音視頻、水印、濾鏡、背景音樂、MV 特效等功能
初始化和啟動編輯:
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添加背景音樂
添加背景音樂
[self.shortVideoEditor addMusic:musicURL timeRange:timeRange volume:1.0];
調節背景音樂音量
[self.shortVideoEditor updateMusic:timeRange volume:0.5];
3.3 短視頻app添加文字特效
添加文字的邏輯和添加貼紙使用的是同一個邏輯,用戶可以使用七牛短視頻 Demo 中已經包裝好的添加文字、貼紙的類 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 沒有集成特效,但是和人臉貼紙一樣,可以輕松的接入第三方的特效。下面我們還以添加 涂圖 的特效為例,演示特效的添加方式
首先,初始化特效添加器:
self.filterProcessor = [[TuSDKFilterProcessor alloc] initWithFormatType:kCVPixelFormatType_32BGRA isOriginalOrientation:isOriginalOrientation]; self.filterProcessor.delegate = self; self.filterProcessor.mediaEffectDelegate = self; // 默認關閉動態貼紙功能,即關閉人臉識別功能 self.filterProcessor.enableLiveSticker = NO;
添加靈魂出竅特效:
TuSDKMediaSceneEffectData *effectData = [[TuSDKMediaSceneEffectData alloc] initWithEffectsCode:@"LiveSoulOut01"]; effectData.atTimeRange = [TuSDKTimeRange makeTimeRangeWithStart:kCMTimeZero end:CMTimeMake(INTMAX_MAX, 1)]; [self.filterProcessor addMediaEffect:effectData];
應用特效。在短視頻編輯視頻數據回調里面,將特效應用,以便預覽特效效果:
- (CVPixelBufferRef)shortVideoEditor:(PLShortVideoEditor *)editor didGetOriginPixelBuffer:(CVPixelBufferRef)pixelBuffer timestamp:(CMTime)timestamp { // 應用特效 pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer frameTime:timestamp]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
上面我們的短視頻編輯就完成了,現在我們將編輯的視頻使用 PLSAVAssetExportSession
導出來保存到本地相冊
初始化視頻導出器:
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]; // 將導出信息設置到 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;
設置導出視頻相關 block:
__weak typeof(self) weakSelf = self; [exportSession setCompletionBlock:^(NSURL *url) { NSLog(@"視頻已保存到相冊中"); }]; [exportSession setFailureBlock:^(NSError *error) { NSLog(@"導出視頻失敗"); }]; [exportSession setProcessingBlock:^(float progress) { NSLog(@"視頻導出完成百分比: %f", process); }];
實現 PLSAVAssetExportSessionDelegate 的視頻數據回調方法,進行特效處理:
- (CVPixelBufferRef)assetExportSession:(PLSAVAssetExportSession *)assetExportSession didOutputPixelBuffer:(CVPixelBufferRef)pixelBuffer timestamp:(CMTime)timestamp { // 特效處理 pixelBuffer = [self.filterProcessor syncProcessPixelBuffer:pixelBuffer frameTime:timestamp]; [self.filterProcessor destroyFrameData]; return pixelBuffer; }
短視頻app開發,北京短視頻app制作專業技術流程,掌握以上短視頻app技術開發要點,開發一個優秀的短視頻app絕對沒有問題。短視頻直播帶貨app開發,2020年4月,非常流行短視頻帶貨直播電商app市場火爆,非常值得進入的短視頻朝陽產業。
生鮮商城APP開發有什么功能,多少錢?
短視頻app開發,北京制作短視頻app專業技術流程
基于人工智能的健身App開發中應包含的基本功能
?開發會所語音聊天APP中要包含的功能
社交網絡APP八大分類,讓您全面了解社交APP類型
在線課程APP平臺開發中包含的主要功能,讓用戶
客服QQ:121446412 聯系電話:15321250321
京ICP備17026149號-1版權所有@2011-2022 北京天品互聯APP開發公司 公司地址:北京市海淀區上地科實大廈D座11層