更新时间 : 2021-11-16 11:01:14
代码示例
SXTemplate *sxTemplate = [[SXTemplate alloc] init:_templatePath type:SXTemplateUsageRender];
//默认不开启素材预加载,需要开启请先查看内存使用情况,确保内存空间可用
// [sxTemplate setNeedSourcePrepare:YES];
SXTemplateRender *render = [[SXTemplateRender alloc] initWithTemplate:sxTemplate audioPath:_musicPath];
render.outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
render.delegate = self;
[sxTemplate commit];
[render start];
代理方法
//渲染成功返回视频地址
- (void)templateRenderFinished:(NSURL *) tempUrl {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
//渲染失败
- (void)templateRenderFailed:(NSError *)error {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
//开始渲染
- (void)templateRenderStarted {
//防止app进入后台代码 注:切换app,锁屏或退出到后台会导致渲染失败
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
NSString *configPath = [_templatePath stringByAppendingPathComponent:@"config.json"];
NSData *data = [NSData dataWithContentsOfFile:configPath];
NSError *error;
NSDictionary *configDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if ([configDic.allKeys containsObject:@"size"]) {
//导出视频大小
NSArray<NSNumber *> *size = configDic[@"size"];
CGSizeMake(size.firstObject.doubleValue, size[1].doubleValue);
}
}
//渲染取消 [render cancel];
- (void)templateRenderCancelled {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
NSLog(@"%@", NSStringFromSelector(_cmd));
}
//渲染进度回调
- (void)templateRenderProgress:(float)progress {
视频渲染进度
}
//素材添加,并替换字幕
- (void)prepareRender {
NSString *configPath = [_templatePath stringByAppendingPathComponent:@"config.json"];
SXConfigUtils *util = [[SXConfigUtils alloc] initWithPath:configPath fileCount:_resourceArr.count];
NSMutableArray *resourcePath = [NSMutableArray array];
for (int i = 0; i<_resourceArr.count; i++) {
BlockbusterResourceModel *model = _resourceArr[I];
NSMutableDictionary *mainDic = [NSMutableDictionary new];
//动态模板添加素材
[mainDic setObject:model.resourcPath forKey:@"main_file"];
NSMutableArray *textarr = [NSMutableArray new];
NSArray<SXTemplateAsset *> *assets = [util getRelatedAssetForFile:i];
for (SXTemplateAsset *asset in assets) {
if (asset.replaceType == 3 || asset.replaceType == 4) {
//字幕替换
if ([_textDic objectForKey:[NSString stringWithFormat:@"%d",i]]) {
NSDictionary *itemTextDic = @{@"type":@(asset.replaceType),@"attr":@{@"text":[_textDic objectForKey:[NSString stringWithFormat:@"%d",i]]}};
[textarr addObject:itemTextDic];
}
}
}
[mainDic setObject:textarr forKey:@"replaces"];
[resourcePath addObject:mainDic];
}
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resourcePath options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[_sxTemplate setReplaceableJson:string];
}
代码示例
SXTemplate *sxTemplate = [[SXTemplate alloc] init:_templatePath type:SXTemplateUsageRender];
//默认不开启素材预加载,需要开启请先查看内存使用情况,确保内存空间可用
// [sxTemplate setNeedSourcePrepare:YES];
SXTemplateRender *render = [[SXTemplateRender alloc] initWithTemplate:sxTemplate audioPath:_musicPath];
render.outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
render.delegate = self;
[sxTemplate commit];
[render start];
代理方法
//渲染成功返回视频地址
- (void)templateRenderFinished:(NSURL *) tempUrl {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
//渲染失败
- (void)templateRenderFailed:(NSError *)error {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
//开始渲染
- (void)templateRenderStarted {
//防止app进入后台代码 注:切换app,锁屏或退出到后台会导致渲染失败
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
NSString *configPath = [_templatePath stringByAppendingPathComponent:@"config.json"];
NSData *data = [NSData dataWithContentsOfFile:configPath];
NSError *error;
NSDictionary *configDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if ([configDic.allKeys containsObject:@"size"]) {
//导出视频大小
NSArray<NSNumber *> *size = configDic[@"size"];
CGSizeMake(size.firstObject.doubleValue, size[1].doubleValue);
}
}
//渲染取消 [render cancel];
- (void)templateRenderCancelled {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
NSLog(@"%@", NSStringFromSelector(_cmd));
}
//渲染进度回调
- (void)templateRenderProgress:(float)progress {
视频渲染进度
}
//素材添加,并替换字幕
- (void)prepareRender {
NSString *configPath = [_templatePath stringByAppendingPathComponent:@"config.json"];
SXConfigUtils *util = [[SXConfigUtils alloc] initWithPath:configPath fileCount:_resourceArr.count];
NSMutableArray *resourcePath = [NSMutableArray array];
for (int i = 0; i<_resourceArr.count; i++) {
BlockbusterResourceModel *model = _resourceArr[I];
NSMutableDictionary *mainDic = [NSMutableDictionary new];
//动态模板添加素材
[mainDic setObject:model.resourcPath forKey:@"main_file"];
NSMutableArray *textarr = [NSMutableArray new];
NSArray<SXTemplateAsset *> *assets = [util getRelatedAssetForFile:i];
for (SXTemplateAsset *asset in assets) {
if (asset.replaceType == 3 || asset.replaceType == 4) {
//字幕替换
if ([_textDic objectForKey:[NSString stringWithFormat:@"%d",i]]) {
NSDictionary *itemTextDic = @{@"type":@(asset.replaceType),@"attr":@{@"text":[_textDic objectForKey:[NSString stringWithFormat:@"%d",i]]}};
[textarr addObject:itemTextDic];
}
}
}
[mainDic setObject:textarr forKey:@"replaces"];
[resourcePath addObject:mainDic];
}
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resourcePath options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[_sxTemplate setReplaceableJson:string];
}