VapxFileHelper.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Tencent is pleased to support the open source community by making vap available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except in
  6. // compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  11. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "VapxFileHelper.h"
  15. #import <AppKit/AppKit.h>
  16. @implementation VapxFileHelper
  17. static NSString *workSpacePath = nil;
  18. - (void)clearCurrentWorkSpace {
  19. NSFileManager *fileManager = [NSFileManager defaultManager];
  20. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  21. NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:NULL];
  22. [directoryContent enumerateObjectsUsingBlock:^(NSString *content, NSUInteger idx, BOOL * _Nonnull stop) {
  23. if ([content.lastPathComponent hasPrefix:@"workspace"]) {
  24. [fileManager removeItemAtPath:[documentsPath stringByAppendingPathComponent:content] error:nil];
  25. }
  26. }];
  27. workSpacePath = nil;
  28. }
  29. - (void)createCurrentWorkSpaceIfNeed {
  30. [self clearCurrentWorkSpace];
  31. NSFileManager *fileManager = [NSFileManager defaultManager];
  32. if (![fileManager fileExistsAtPath:[self videoFramesPath]]) {
  33. NSError *error = nil;
  34. [fileManager createDirectoryAtPath:[self videoFramesPath] withIntermediateDirectories:YES attributes:nil error:&error];
  35. }
  36. if (![fileManager fileExistsAtPath:[self outputPath]]) {
  37. NSError *error = nil;
  38. [fileManager createDirectoryAtPath:[self outputPath] withIntermediateDirectories:YES attributes:nil error:&error];
  39. }
  40. if (![fileManager fileExistsAtPath:[self layoutDir]]) {
  41. NSError *error = nil;
  42. [fileManager createDirectoryAtPath:[self layoutDir] withIntermediateDirectories:YES attributes:nil error:&error];
  43. }
  44. if (![fileManager fileExistsAtPath:[self audioPath]]) {
  45. NSError *error = nil;
  46. [fileManager createDirectoryAtPath:[self audioPath] withIntermediateDirectories:YES attributes:nil error:&error];
  47. }
  48. }
  49. - (NSString *)saveUploadedAudioFile:(NSURL *)path {
  50. NSString *des = [[self audioPath] stringByAppendingPathComponent:path.lastPathComponent];
  51. NSError *e = nil;
  52. [[NSFileManager defaultManager] copyItemAtURL:path toURL:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", des]] error:&e];
  53. if (e) {
  54. NSLog(@"saveUploadedAudioFile error:%@ %@ %@", path, des, e);
  55. }
  56. return des;
  57. }
  58. - (void)saveUploadedVideoFrames:(NSArray<NSURL *> *)paths {
  59. [paths enumerateObjectsUsingBlock:^(NSURL * _Nonnull URL, NSUInteger idx, BOOL * _Nonnull stop) {
  60. NSString *des = [[self videoFramesPath] stringByAppendingPathComponent:URL.lastPathComponent];
  61. NSError *e = nil;
  62. [[NSFileManager defaultManager] copyItemAtURL:URL toURL:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", des]] error:&e];
  63. if (e) {
  64. NSLog(@"saveUploadedVideoFrames error:%@ %@ %@", URL, des, e);
  65. }
  66. }];
  67. }
  68. - (NSString *)saveUploadedMasks:(NSArray<NSURL *> *)paths identifier:(NSString *)identifier {
  69. NSString *maskDir = [self maskPathForIdentifier:identifier];
  70. NSFileManager *fileManager = [NSFileManager defaultManager];
  71. if (![fileManager fileExistsAtPath:maskDir]) {
  72. NSError *error = nil;
  73. [fileManager createDirectoryAtPath:maskDir withIntermediateDirectories:YES attributes:nil error:&error];
  74. }
  75. [paths enumerateObjectsUsingBlock:^(NSURL * _Nonnull URL, NSUInteger idx, BOOL * _Nonnull stop) {
  76. NSError *e = nil;
  77. NSString *des = [maskDir stringByAppendingPathComponent:URL.lastPathComponent];
  78. [[NSFileManager defaultManager] copyItemAtURL:URL toURL:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", des]] error:&e];
  79. if (e) {
  80. NSLog(@"saveUploadedMasks error:%@ %@ %@", URL, des, e);
  81. }
  82. }];
  83. return maskDir;
  84. }
  85. - (NSString *)maskPathForIdentifier:(NSString *)identifier {
  86. return [[self workSpacePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"mask-%@",identifier]];
  87. }
  88. - (NSString *)audioPath {
  89. return [[self workSpacePath] stringByAppendingPathComponent:@"audio"];
  90. }
  91. - (NSString *)videoFramesPath {
  92. return [[self workSpacePath] stringByAppendingPathComponent:@"videoFrames"];
  93. }
  94. - (NSString *)outputPath {
  95. return [[self workSpacePath] stringByAppendingPathComponent:@"output"];
  96. }
  97. - (NSString *)layoutDir {
  98. return [[self workSpacePath] stringByAppendingPathComponent:@"layout"];
  99. }
  100. - (NSString *)layoutMP4Name {
  101. return @"layout.mp4";
  102. }
  103. - (NSString *)workSpacePath {
  104. if (workSpacePath) {
  105. return workSpacePath;
  106. }
  107. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  108. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceReferenceDate];
  109. workSpacePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"workspace-%@", @(timeInterval)]];
  110. return workSpacePath;
  111. }
  112. - (NSString *)outputMP4Name {
  113. return @"vap.mp4";
  114. }
  115. @end