版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.08.17 |
前言
針對短視頻的上傳、編輯等功能有很多的SDK次坡,比如騰訊的SDK钩乍、七牛的SDK等辞州,這里我就說一下我用過的美攝的SDK - 1.8.0,希望對大家有所幫助件蚕。感興趣的可以看我上面幾篇孙技。
1. 美攝SDK的使用(一)—— 產(chǎn)品介紹
2. 美攝SDK的使用(二)—— 框架介紹
短視頻錄制工具類的封裝
下面就是短視頻錄制中工具類的封裝的實現(xiàn)产禾。
1. JJNvsLiveContext.h
#import <Foundation/Foundation.h>
#import "NvsStreamingContext.h"
@interface JJNvsLiveContext : NSObject
@property (nonatomic, strong) NvsStreamingContext *context;//流媒體上下文
//開始錄制
- (BOOL)startRecording:(NSString *)path;
//結(jié)束錄制
- (BOOL)stopRecording;
//添加預(yù)覽界面
- (BOOL)addPreView:(NvsLiveWindow *)preView;
//開始預(yù)覽
- (BOOL)startCapturePreview;
//保存流媒體
- (void)saveRecording:(NSString *)path;
//銷毀流媒體上下文
- (void)destroyInstance;
//開啟美顏
- (void)appendBeautyCaptureVideoFx;
//獲取視頻流時間
- (NSInteger)getVideoStreamDuration;
//是否是前置攝像頭
- (BOOL)isFrontCamera;
//切換攝像圖頭
- (void)switchCamera;
//開關(guān)閃光燈
- (void)startFlight;
- (void)stop;
@end
2. JJNvsLiveContext.m
#import "JJNvsLiveContext.h"
@interface JJNvsLiveContext () <NvsStreamingContextDelegate>
@property (nonatomic, strong) NvsTimeline *timeLine;//時間線
@property (nonatomic, weak) NvsLiveWindow *preView;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, assign) BOOL isFront;
@end
@implementation JJNvsLiveContext
static NSString *tempPath;
#pragma mark - Override Base Function
- (void)dealloc
{
DDLogWarn(@"%@ 已釋放", self);
}
#pragma mark - Object Private Function
- (BOOL)startCapturePreview:(unsigned int)currentDeviceIndex
{
return [self.context startCapturePreview:currentDeviceIndex videoResGrade:NvsVideoCaptureResolutionGradeHigh flags:0 aspectRatio:nil];
}
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
DDLogError(@"Save image failed");
}
else
{
DDLogInfo(@"Save image successful");
}
}
#pragma mark - Object Public Function
//開始錄制
- (BOOL)startRecording:(NSString *)path
{
tempPath = path;
BOOL isSuccess = [self.context startRecording:path withFlags:2];
return isSuccess;
}
//結(jié)束錄制
- (void)stopRecording
{
[self.context stopRecording];
}
//添加預(yù)覽界面
- (BOOL)addPreView:(NvsLiveWindow *)preView
{
self.preView = preView;
return [self.context connectCapturePreviewWithLiveWindow:preView];
}
//開始預(yù)覽
- (BOOL)startCapturePreview
{
return [self startCapturePreview:0];
}
//銷毀流媒體上下文
- (void)destroyInstance
{
[NvsStreamingContext destroyInstance];
}
//開啟美顏
- (void)appendBeautyCaptureVideoFx
{
[self.context appendBeautyCaptureVideoFx];
}
//是否是前置攝像頭
- (BOOL)isFrontCamera
{
return ![self.context isCaptureDeviceBackFacing:0];
}
//切換攝像圖頭
- (void)switchCamera
{
if (!_isFront) {
[self startCapturePreview:0];
_isFront = YES;
}
else {
[self startCapturePreview:1];
_isFront = NO;
}
}
- (void)startFlight
{
if (_isOpen == NO) {
[self.context toggleFlash:YES];
_isOpen = YES;
}
else {
[self.context toggleFlash:NO];
_isOpen = NO;
}
}
- (void)stop
{
[self.context stop];
[self.context clearCachedResources:YES];
self.context.delegate = nil;
}
//先將視頻保存到沙盒
- (void)saveRecording:(NSString *)tmpPath
{
//再將視頻保存到相冊
UISaveVideoAtPathToSavedPhotosAlbum(tmpPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
#pragma mark - Lazy Load
//上下文
- (NvsStreamingContext *)context
{
if (!_context) {
_context = [NvsStreamingContext sharedInstance];
_context.delegate = self;
}
return _context;
}
//時間線
- (NvsTimeline *)timeLine
{
if (!_timeLine) {
NvsVideoResolution videoEditRes;
videoEditRes.imageWidth = 720/2;
videoEditRes.imageHeight = 1280/2;
videoEditRes.imagePAR = (NvsRational){1, 1};
NvsRational videoFps = {25, 1};
NvsAudioResolution audioEditRes;
audioEditRes.sampleRate = 48000;
audioEditRes.channelCount = 2;
audioEditRes.sampleFormat = NvsAudSmpFmt_S16;
_timeLine = [self.context createTimeline:&videoEditRes videoFps:&videoFps audioEditRes:&audioEditRes];
// NvsVideoTrack *videoTrack = [_timeLine appendVideoTrack];
// NvsVideoTrack *videoTrack = [_timeLine appendVideoTrack];
// //在軌道上添加片段。(請自行實現(xiàn)獲取手機(jī)存儲器里視頻和圖片的代碼部分)
//
// NSString* videoUrl = @"file:///var/mobile/Media/DCIM/102APPLE/IMG_2625.MOV";
// [videoTrack appendClip:videoUrl];
}
return _timeLine;
}
#pragma mark - NvsStreamingContextDelegate
- (void)didCaptureDeviceCapsReady:(unsigned int)captureDeviceIndex
{
NSLog(@"采集設(shè)備準(zhǔn)備完成");
}
- (void)didCaptureDevicePreviewResolutionReady:(unsigned int)captureDeviceIndex
{
NSLog(@"采集設(shè)備預(yù)覽解析度準(zhǔn)備完成");
}
- (void)didCaptureDevicePreviewStarted:(unsigned int)captureDeviceIndex
{
NSLog(@"采集設(shè)備預(yù)覽開始");
}
- (void)didCaptureDeviceError:(unsigned int)captureDeviceIndex errorCode:(int32_t)errorCode
{
NSLog(@"采集設(shè)備錯誤");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集設(shè)備停止
* \param captureDeviceIndex 設(shè)備索引
* \return 返回值為空
* \endif
*/
- (void)didCaptureDeviceStopped:(unsigned int)captureDeviceIndex
{
NSLog(@"采集設(shè)備停止");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集設(shè)備自動對焦完成
* \param captureDeviceIndex 設(shè)備索引
* \param succeeded 對焦是否完成
* \return 返回值為空
* \endif
*/
- (void)didCaptureDeviceAutoFocusComplete:(unsigned int)captureDeviceIndex succeeded:(BOOL)succeeded
{
NSLog(@"采集設(shè)備自動對焦完成");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集錄制完成
* \param captureDeviceIndex 設(shè)備索引
* \sa didCaptureRecordingError:
* \return 返回值為空
* \endif
*/
- (void)didCaptureRecordingFinished:(unsigned int)captureDeviceIndex
{
NSLog(@"采集錄制完成");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集錄制失敗
* \param captureDeviceIndex 設(shè)備索引
* \return 返回值為空
* \endif
*/
- (void)didCaptureRecordingError:(unsigned int)captureDeviceIndex
{
NSLog(@"采集錄制失敗");
}
@end
該工具類可以滿足利用美攝SDK進(jìn)行短視頻錄制的需求牵啦,實現(xiàn)了預(yù)覽亚情、錄制等功能。
后記
未完哈雏,待續(xù)~~