Skip to content

Commit

Permalink
progress call back
Browse files Browse the repository at this point in the history
  • Loading branch information
黄锐灏 committed Jan 30, 2019
1 parent a333854 commit 6a606ef
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 55 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ PS :支持多线程处理

iOS 8.0 ++

V 1.0.2 更新
=====================
- 支持编辑进度回调
- 支持动态水印合成

使用指导
=====================

Expand Down Expand Up @@ -138,6 +143,3 @@ Box分析
3、commit:合成区域,将缓存区,合成区的视频移到工作区,视频操作对所有视频有效

tip:线程安全,适用于短视频处理



Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
scope = "0"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
12 changes: 9 additions & 3 deletions WAVideoBox/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ - (IBAction)addWaterMark:(id)sender {
[_videoBox appendVideoByPath:_videoPath];
[_videoBox appendImages:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"gifTest" ofType:@"gif"]] relativeRect:CGRectMake(0.6, 0.2, 0.3, 0)];

[_videoBox asyncFinishEditByFilePath:filePath complete:^(NSError *error) {
[_videoBox asyncFinishEditByFilePath:filePath progress:^(float progress) {
NSLog(@"progress -- %f",progress);
} complete:^(NSError *error) {
if (!error) {
[wself goToPlayVideoByFilePath:filePath];
}
Expand Down Expand Up @@ -200,7 +202,9 @@ - (IBAction)composeEdit:(id)sender {

[_videoBox rangeVideoByTimeRange:CMTimeRangeMake(CMTimeMake(2400, 600), CMTimeMake(3600, 600))];

[_videoBox asyncFinishEditByFilePath:filePath complete:^(NSError *error) {
[_videoBox asyncFinishEditByFilePath:filePath progress:^(float progress) {
NSLog(@"progress --- %f",progress);
} complete:^(NSError * error) {
if (!error) {
[wself goToPlayVideoByFilePath:filePath];
}
Expand Down Expand Up @@ -240,7 +244,9 @@ - (IBAction)magicEdit:(id)sender {
[_videoBox commit];
[_videoBox gearBoxWithScale:2];

[_videoBox asyncFinishEditByFilePath:filePath complete:^(NSError * error) {
[_videoBox asyncFinishEditByFilePath:filePath progress:^(float progress) {
NSLog(@"progress --- %f",progress);
} complete:^(NSError * error) {
if (!error) {
[wself goToPlayVideoByFilePath:filePath];
}
Expand Down
5 changes: 2 additions & 3 deletions WAVideoBox/WAVideoBox/WAAVSeCommand/WAAVSECommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
- (void)performAudioCompopsition;


/**
计算旋转角度
Expand All @@ -44,9 +43,9 @@
*/
- (NSUInteger)degressFromTransform:(CGAffineTransform)transForm;



@end

extern NSString* const WAAVSEExportCommandCompletionNotification;

extern NSString* const WAAVSEExportCommandError;

4 changes: 3 additions & 1 deletion WAVideoBox/WAVideoBox/WAAVSeCommand/WAAVSEExportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

@interface WAAVSEExportCommand : WAAVSECommand

@property AVAssetExportSession *exportSession;
@property (nonatomic , strong)AVAssetExportSession *exportSession;

@property (nonatomic , assign) NSInteger videoQuality;

- (void)performSaveByPath:(NSString *)path;

- (void)performSaveAsset:(AVAsset *)asset byPath:(NSString *)path;

- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;

@end
21 changes: 0 additions & 21 deletions WAVideoBox/WAVideoBox/WAAVSeCommand/WAAVSERotateCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,6 @@ - (void)performWithAsset:(AVAsset *)asset degress:(NSUInteger)degress{
}


// 调整旋转
- (NSUInteger)degressFromTransform:(CGAffineTransform)transForm
{
NSUInteger degress = 0;

if(transForm.a == 0 && transForm.b == 1.0 && transForm.c == -1.0 && transForm.d == 0){
// Portrait
degress = 90;
}else if(transForm.a == 0 && transForm.b == -1.0 && transForm.c == 1.0 && transForm.d == 0){
// PortraitUpsideDown
degress = 270;
}else if(transForm.a == 1.0 && transForm.b == 0 && transForm.c == 0 && transForm.d == 1.0){
// LandscapeRight
degress = 0;
}else if(transForm.a == -1.0 && transForm.b == 0 && transForm.c == 0 && transForm.d == -1.0){
// LandscapeLeft
degress = 180;
}

return degress;
}

- (void)converRect:(CALayer *)layer naturalRenderSize:(CGSize)size renderSize:(CGSize)renderSize{

Expand Down
18 changes: 18 additions & 0 deletions WAVideoBox/WAVideoBox/WAVideoBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ typedef NS_ENUM(NSUInteger,WAVideoExportRatio) {
#pragma mark 处理
/**
异步视频处理
@param filePath 存储位置
@param complete 回调block
*/
- (void)asyncFinishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *error))complete;
Expand All @@ -171,6 +172,23 @@ typedef NS_ENUM(NSUInteger,WAVideoExportRatio) {
*/
- (void)syncFinishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *error))complete;

/**
异步视频处理带进度回调
@param filePath 存储位置
@param progress 进度block
@param complete 回调block
*/
- (void)asyncFinishEditByFilePath:(NSString *)filePath progress:(void (^)(float progress))progress complete:(void (^)(NSError *error))complete;

/**
同步视频处理带进度回调
tip:主线程下无法使用此同步操作,因为进度回调也是在主线程,如果在主线程做同步等待,会造成死锁
@param filePath 存储位置
@param progress 进度block
@param complete 回调block
*/
- (void)syncFinishEditByFilePath:(NSString *)filePath progress:(void (^)(float progress))progress complete:(void (^)(NSError *error))complete;

/**
取消操作
*/
Expand Down
73 changes: 61 additions & 12 deletions WAVideoBox/WAVideoBox/WAVideoBox.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
#import "WAAVSEExtractSoundCommand.h"
#import <pthread.h>

@interface WAVideoBox ()
@interface WAVideoBox (){
CADisplayLink *_progressLink;
}

@property (nonatomic , strong) WAAVSEComposition *cacheComposition;

@property (nonatomic , weak) WAAVSEExportCommand *exportCommand;

@property (nonatomic , strong) NSMutableArray <WAAVSEComposition *>*workSpace;

@property (nonatomic , strong) NSMutableArray <WAAVSEComposition *>*composeSpace;

@property (nonatomic , strong) NSMutableArray <NSString *>*tmpVideoSpace;

@property (nonatomic , weak) WAAVSEExportCommand *exportCommand;
@property (nonatomic , strong) NSMutableArray <NSString *>*tmpVideoSpace; //临时视频文件

@property (nonatomic , assign) NSInteger directCompostionIndex;

Expand All @@ -40,11 +42,15 @@ @interface WAVideoBox ()

@property (nonatomic , copy) void (^editorComplete)(NSError *error);

@property (nonatomic , copy) void (^progress)(float progress);

@property (nonatomic , copy) NSString *presetName;

@property (nonatomic , assign) NSInteger composeCount; // 一共需要几次compose操作,用于记录进度

@property (nonatomic , assign ,getter=isSuspend) BOOL suspend; //线程 挂起

@property (nonatomic , assign ,getter=isCancel) BOOL cancel; //线程 挂起
@property (nonatomic , assign ,getter=isCancel) BOOL cancel; //用户取消操作

@end

Expand Down Expand Up @@ -430,19 +436,31 @@ - (BOOL)extractVideoSound{
#pragma mark video edit

- (void)syncFinishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *))complete{
[self syncFinishEditByFilePath:filePath progress:nil complete:complete];
}

- (void)asyncFinishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *))complete{
[self asyncFinishEditByFilePath:filePath progress:nil complete:complete];
}

- (void)syncFinishEditByFilePath:(NSString *)filePath progress:(void (^)(float))progress complete:(void (^)(NSError *))complete{

if ([[NSThread currentThread] isMainThread]) {
NSAssert(NO, @"You shouldn't make it in main thread!");
}

runSynchronouslyOnVideoBoxContextQueue(^{
[self finishEditByFilePath:filePath complete:complete];
[self finishEditByFilePath:filePath progress:progress complete:complete];
});
}

- (void)asyncFinishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *))complete{
- (void)asyncFinishEditByFilePath:(NSString *)filePath progress:(void (^)(float))progress complete:(void (^)(NSError *))complete{
runAsynchronouslyOnVideoBoxContextQueue(^{
[self finishEditByFilePath:filePath complete:complete];
[self finishEditByFilePath:filePath progress:progress complete:complete];
});
}

- (void)cancelEdit{

runSynchronouslyOnVideoBoxProcessingQueue(^{
self.cancel = YES;
if (self.exportCommand.exportSession.status == AVAssetExportSessionStatusExporting) {
Expand Down Expand Up @@ -472,7 +490,13 @@ - (void)__internalClean{
[self.tmpVideoSpace removeAllObjects];
[self.workSpace removeAllObjects];
[self.composeSpace removeAllObjects];
self.composeCount = 0;
self.progress = nil;
self.editorComplete = nil;
if (_progressLink) {
[_progressLink invalidate];
_progressLink = nil;
}
self.filePath = nil;
self.tmpPath = nil;

Expand Down Expand Up @@ -530,7 +554,16 @@ - (void)processVideoByComposition:(WAAVSEComposition *)composition{
self.exportCommand = exportCommand;
[exportCommand performSaveByPath:filePath];


if (self.progress && !_progressLink) {

_progressLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkCallback:)];
if (@available(iOS 10.0, *)) {
_progressLink.preferredFramesPerSecond = 10;
}else{
_progressLink.frameInterval = 6;
}
[_progressLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
}

- (void)successToProcessCurrentCompostion{
Expand Down Expand Up @@ -597,7 +630,7 @@ - (void)successToProcessVideo{

}

- (void)finishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *error))complete{
- (void)finishEditByFilePath:(NSString *)filePath progress:(void (^)(float progress))progress complete:(void (^)(NSError *error))complete{

[self commitCompostionToWorkspace];

Expand All @@ -610,6 +643,12 @@ - (void)finishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *er

self.filePath = filePath;
self.editorComplete = complete;
self.progress = progress;
self.composeCount = self.composeSpace.count;

if (self.composeCount != 1) { // 代表需要将compose里的视频生成后再合为一个
self.composeCount ++;
}

runSynchronouslyOnVideoBoxProcessingQueue(^{

Expand All @@ -625,13 +664,23 @@ - (void)finishEditByFilePath:(NSString *)filePath complete:(void (^)(NSError *er
}
});


}

- (NSString *)tmpVideoFilePath{
return [_tmpDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.mp4",[NSDate timeIntervalSinceReferenceDate]]];
}

- (void)displayLinkCallback:(CADisplayLink *)link{
if (self.progress && self.exportCommand) {
if (self.composeCount == 1) {
self.progress(1.0 / self.composeCount * (self.composeCount - self.composeSpace.count) + 1.0 / self.composeCount * self.exportCommand.exportSession.progress);
}else{
self.progress(1.0 / self.composeCount * (self.composeCount - self.composeSpace.count - 1) + 1.0 / self.composeCount * self.exportCommand.exportSession.progress);
}

}
}

#pragma mark notification
- (void)AVEditorNotification:(NSNotification *)notification{

Expand Down

0 comments on commit 6a606ef

Please sign in to comment.