版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.17 |
前言
针对短视频的上传、编辑等功能有很多的SDK,比如腾讯的SDK、七牛的SDK等,这里我就说一下我用过的美摄的SDK - 1.8.0,希望对大家有所帮助。感兴趣的可以看我上面几篇。
1. 美摄SDK的使用(一)—— 产品介绍
2. 美摄SDK的使用(二)—— 框架介绍
短视频录制工具类的封装
下面就是短视频录制中工具类的封装的实现。
1. JJNvsLiveContext.h
#import <Foundation/Foundation.h>
#import "NvsStreamingContext.h"
@interface JJNvsLiveContext : NSObject
@property (nonatomic, strong) NvsStreamingContext *context;//流媒体上下文
//开始录制
- (BOOL)startRecording:(NSString *)path;
//结束录制
- (BOOL)stopRecording;
//添加预览界面
- (BOOL)addPreView:(NvsLiveWindow *)preView;
//开始预览
- (BOOL)startCapturePreview;
//保存流媒体
- (void)saveRecording:(NSString *)path;
//销毁流媒体上下文
- (void)destroyInstance;
//开启美颜
- (void)appendBeautyCaptureVideoFx;
//获取视频流时间
- (NSInteger)getVideoStreamDuration;
//是否是前置摄像头
- (BOOL)isFrontCamera;
//切换摄像图头
- (void)switchCamera;
//开关闪光灯
- (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;
}
//结束录制
- (void)stopRecording
{
[self.context stopRecording];
}
//添加预览界面
- (BOOL)addPreView:(NvsLiveWindow *)preView
{
self.preView = preView;
return [self.context connectCapturePreviewWithLiveWindow:preView];
}
//开始预览
- (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];
// //在轨道上添加片段。(请自行实现获取手机存储器里视频和图片的代码部分)
//
// 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(@"采集设备准备完成");
}
- (void)didCaptureDevicePreviewResolutionReady:(unsigned int)captureDeviceIndex
{
NSLog(@"采集设备预览解析度准备完成");
}
- (void)didCaptureDevicePreviewStarted:(unsigned int)captureDeviceIndex
{
NSLog(@"采集设备预览开始");
}
- (void)didCaptureDeviceError:(unsigned int)captureDeviceIndex errorCode:(int32_t)errorCode
{
NSLog(@"采集设备错误");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集设备停止
* \param captureDeviceIndex 设备索引
* \return 返回值为空
* \endif
*/
- (void)didCaptureDeviceStopped:(unsigned int)captureDeviceIndex
{
NSLog(@"采集设备停止");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集设备自动对焦完成
* \param captureDeviceIndex 设备索引
* \param succeeded 对焦是否完成
* \return 返回值为空
* \endif
*/
- (void)didCaptureDeviceAutoFocusComplete:(unsigned int)captureDeviceIndex succeeded:(BOOL)succeeded
{
NSLog(@"采集设备自动对焦完成");
}
/*! \if ENGLISH
* \brief
* method detailed description
* \param sender sender parameter description
* \return return value description
* \else
* \brief 采集录制完成
* \param captureDeviceIndex 设备索引
* \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 设备索引
* \return 返回值为空
* \endif
*/
- (void)didCaptureRecordingError:(unsigned int)captureDeviceIndex
{
NSLog(@"采集录制失败");
}
@end
该工具类可以满足利用美摄SDK进行短视频录制的需求,实现了预览、录制等功能。
后记
未完,待续~~