From d373932ef4b6031886dc047b75fb657ef49639c0 Mon Sep 17 00:00:00 2001 From: Sergey Petrachkov Date: Fri, 4 Sep 2020 14:16:48 +0300 Subject: [PATCH 1/2] support swift package manager --- .gitignore | 2 + LICENSE | 1 - Makefile | 8 ++ Package.swift | 21 ++++++ README.md | 74 +++++++++++++------ Sources/SPMAssetExporter/AssetExporter.h | 18 +++++ .../SPMAssetExporter/SDAVAssetExportSession.h | 0 .../SPMAssetExporter/SDAVAssetExportSession.m | 7 +- 8 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 Makefile create mode 100644 Package.swift create mode 100644 Sources/SPMAssetExporter/AssetExporter.h rename SDAVAssetExportSession.h => Sources/SPMAssetExporter/SDAVAssetExportSession.h (100%) rename SDAVAssetExportSession.m => Sources/SPMAssetExporter/SDAVAssetExportSession.m (99%) diff --git a/.gitignore b/.gitignore index 89c499e..420541b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ profile *.moved-aside DerivedData .idea/ +.build +Release diff --git a/LICENSE b/LICENSE index 7ee23d3..c9da694 100644 --- a/LICENSE +++ b/LICENSE @@ -16,4 +16,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fae959a --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +build: + swift build + +release: clean + swift build --configuration=release --build-path ./Release + +clean: + rm -rf ./.build diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..a1e94e8 --- /dev/null +++ b/Package.swift @@ -0,0 +1,21 @@ +// swift-tools-version:5.2 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "SPMAssetExporter", + products: [ + .library( + name: "SPMAssetExporter", + targets: ["SPMAssetExporter"] + ), + ], + dependencies: [], + targets: [ + .target( + name: "SPMAssetExporter", + dependencies: [] + ), + ] +) diff --git a/README.md b/README.md index a93ff71..53892f6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# SPMAssetExporter + SDAVAssetExportSession ====================== @@ -8,6 +10,7 @@ You want the ease of use of `AVAssetExportSession` but default provided presets Usage Example ------------- +For Objective-C: ``` objective-c SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:anAsset]; @@ -15,41 +18,68 @@ encoder.outputFileType = AVFileTypeMPEG4; encoder.outputURL = outputFileURL; encoder.videoSettings = @ { - AVVideoCodecKey: AVVideoCodecH264, - AVVideoWidthKey: @1920, - AVVideoHeightKey: @1080, - AVVideoCompressionPropertiesKey: @ + AVVideoCodecKey: AVVideoCodecH264, + AVVideoWidthKey: @1920, + AVVideoHeightKey: @1080, + AVVideoCompressionPropertiesKey: @ { - AVVideoAverageBitRateKey: @6000000, - AVVideoProfileLevelKey: AVVideoProfileLevelH264High40, + AVVideoAverageBitRateKey: @6000000, + AVVideoProfileLevelKey: AVVideoProfileLevelH264High40, }, }; encoder.audioSettings = @ { - AVFormatIDKey: @(kAudioFormatMPEG4AAC), - AVNumberOfChannelsKey: @2, - AVSampleRateKey: @44100, - AVEncoderBitRateKey: @128000, + AVFormatIDKey: @(kAudioFormatMPEG4AAC), + AVNumberOfChannelsKey: @2, + AVSampleRateKey: @44100, + AVEncoderBitRateKey: @128000, }; [encoder exportAsynchronouslyWithCompletionHandler:^ { - if (encoder.status == AVAssetExportSessionStatusCompleted) - { - NSLog(@"Video export succeeded"); - } - else if (encoder.status == AVAssetExportSessionStatusCancelled) - { - NSLog(@"Video export cancelled"); - } - else - { - NSLog(@"Video export failed with error: %@ (%d)", encoder.error.localizedDescription, encoder.error.code); - } + if (encoder.status == AVAssetExportSessionStatusCompleted) + { + NSLog(@"Video export succeeded"); + } + else if (encoder.status == AVAssetExportSessionStatusCancelled) + { + NSLog(@"Video export cancelled"); + } + else + { + NSLog(@"Video export failed with error: %@ (%d)", encoder.error.localizedDescription, encoder.error.code); + } }]; ``` +And for Swift: + +```swift +let exporter = SDAVAssetExportSession(asset: asset)! +exporter.outputFileType = processingParameters.outputFileType.rawValue +exporter.outputURL = processingParameters.outputUrl +exporter.videoSettings = [ + AVVideoCodecKey: AVVideoCodecType.h264, + AVVideoWidthKey: targetSize.width, + AVVideoHeightKey: targetSize.height, + AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 1024_000, AVVideoProfileLevelKey: AVVideoProfileLevelH264High40] +] +exporter.audioSettings = [ + AVFormatIDKey: kAudioFormatMPEG4AAC, + AVNumberOfChannelsKey: 1, + AVSampleRateKey: 44100, + AVEncoderBitRateKey: 96_000 +] +exporter.videoComposition = videoComposition + +exporter.exportAsynchronously(completionHandler: { + switch exporter.status { + // do your work here + } +}) +``` + Licenses -------- diff --git a/Sources/SPMAssetExporter/AssetExporter.h b/Sources/SPMAssetExporter/AssetExporter.h new file mode 100644 index 0000000..3f405f2 --- /dev/null +++ b/Sources/SPMAssetExporter/AssetExporter.h @@ -0,0 +1,18 @@ +// +// AssetExporter.h +// AssetExporter +// +// Created by sergey on 13.02.2020. +// Copyright © 2020 JuiceFit. All rights reserved. +// + +#import +#import + +//! Project version number for AssetExporter. +FOUNDATION_EXPORT double AssetExporterVersionNumber; + +//! Project version string for AssetExporter. +FOUNDATION_EXPORT const unsigned char AssetExporterVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import diff --git a/SDAVAssetExportSession.h b/Sources/SPMAssetExporter/SDAVAssetExportSession.h similarity index 100% rename from SDAVAssetExportSession.h rename to Sources/SPMAssetExporter/SDAVAssetExportSession.h diff --git a/SDAVAssetExportSession.m b/Sources/SPMAssetExporter/SDAVAssetExportSession.m similarity index 99% rename from SDAVAssetExportSession.m rename to Sources/SPMAssetExporter/SDAVAssetExportSession.m index e99993b..6f649fe 100755 --- a/SDAVAssetExportSession.m +++ b/Sources/SPMAssetExporter/SDAVAssetExportSession.m @@ -10,7 +10,6 @@ // file that was distributed with this source code. // - #import "SDAVAssetExportSession.h" @interface SDAVAssetExportSession () @@ -168,7 +167,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)(void))handler [self.writer addInput:self.audioInput]; } } - + [self.writer startWriting]; [self.reader startReading]; [self.writer startSessionAtSourceTime:self.timeRange.start]; @@ -196,7 +195,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)(void))handler else { videoCompleted = YES; } - + if (!self.audioOutput) { audioCompleted = YES; } else { @@ -232,7 +231,7 @@ - (BOOL)encodeReadySamplesFromOutput:(AVAssetReaderOutput *)output toInput:(AVAs handled = YES; error = YES; } - + if (!handled && self.videoOutput == output) { // update the video progress From d8b200330221a5032a3290f6c772a9abf33173fb Mon Sep 17 00:00:00 2001 From: Sergey Petrachkov Date: Fri, 4 Sep 2020 15:38:03 +0300 Subject: [PATCH 2/2] update project structure --- README.md | 37 +++++++++++-------- Sources/SPMAssetExporter/AssetExporter.h | 18 --------- .../{ => include}/SDAVAssetExportSession.h | 0 .../{ => include}/SDAVAssetExportSession.m | 0 4 files changed, 22 insertions(+), 33 deletions(-) delete mode 100644 Sources/SPMAssetExporter/AssetExporter.h rename Sources/SPMAssetExporter/{ => include}/SDAVAssetExportSession.h (100%) rename Sources/SPMAssetExporter/{ => include}/SDAVAssetExportSession.m (100%) diff --git a/README.md b/README.md index 53892f6..8c22094 100644 --- a/README.md +++ b/README.md @@ -10,23 +10,22 @@ You want the ease of use of `AVAssetExportSession` but default provided presets Usage Example ------------- -For Objective-C: - +For ObjC: ``` objective-c SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:anAsset]; encoder.outputFileType = AVFileTypeMPEG4; encoder.outputURL = outputFileURL; encoder.videoSettings = @ -{ - AVVideoCodecKey: AVVideoCodecH264, - AVVideoWidthKey: @1920, - AVVideoHeightKey: @1080, - AVVideoCompressionPropertiesKey: @ - { - AVVideoAverageBitRateKey: @6000000, - AVVideoProfileLevelKey: AVVideoProfileLevelH264High40, - }, -}; + { + AVVideoCodecKey: AVVideoCodecH264, + AVVideoWidthKey: @1920, + AVVideoHeightKey: @1080, + AVVideoCompressionPropertiesKey: @ + { + AVVideoAverageBitRateKey: @6000000, + AVVideoProfileLevelKey: AVVideoProfileLevelH264High40, + }, + }; encoder.audioSettings = @ { AVFormatIDKey: @(kAudioFormatMPEG4AAC), @@ -55,7 +54,15 @@ encoder.audioSettings = @ And for Swift: +* add the package to your Xcode project (https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) +* import package +* use it in swift code like + ```swift +import SPMAssetExporter + +// ... + let exporter = SDAVAssetExportSession(asset: asset)! exporter.outputFileType = processingParameters.outputFileType.rawValue exporter.outputURL = processingParameters.outputUrl @@ -63,7 +70,7 @@ exporter.videoSettings = [ AVVideoCodecKey: AVVideoCodecType.h264, AVVideoWidthKey: targetSize.width, AVVideoHeightKey: targetSize.height, - AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 1024_000, AVVideoProfileLevelKey: AVVideoProfileLevelH264High40] + AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 1024_000,AVVideoProfileLevelKey: AVVideoProfileLevelH264High40] ] exporter.audioSettings = [ AVFormatIDKey: kAudioFormatMPEG4AAC, @@ -74,8 +81,8 @@ exporter.audioSettings = [ exporter.videoComposition = videoComposition exporter.exportAsynchronously(completionHandler: { - switch exporter.status { - // do your work here + switch encoder.status { + // do your work here } }) ``` diff --git a/Sources/SPMAssetExporter/AssetExporter.h b/Sources/SPMAssetExporter/AssetExporter.h deleted file mode 100644 index 3f405f2..0000000 --- a/Sources/SPMAssetExporter/AssetExporter.h +++ /dev/null @@ -1,18 +0,0 @@ -// -// AssetExporter.h -// AssetExporter -// -// Created by sergey on 13.02.2020. -// Copyright © 2020 JuiceFit. All rights reserved. -// - -#import -#import - -//! Project version number for AssetExporter. -FOUNDATION_EXPORT double AssetExporterVersionNumber; - -//! Project version string for AssetExporter. -FOUNDATION_EXPORT const unsigned char AssetExporterVersionString[]; - -// In this header, you should import all the public headers of your framework using statements like #import diff --git a/Sources/SPMAssetExporter/SDAVAssetExportSession.h b/Sources/SPMAssetExporter/include/SDAVAssetExportSession.h similarity index 100% rename from Sources/SPMAssetExporter/SDAVAssetExportSession.h rename to Sources/SPMAssetExporter/include/SDAVAssetExportSession.h diff --git a/Sources/SPMAssetExporter/SDAVAssetExportSession.m b/Sources/SPMAssetExporter/include/SDAVAssetExportSession.m similarity index 100% rename from Sources/SPMAssetExporter/SDAVAssetExportSession.m rename to Sources/SPMAssetExporter/include/SDAVAssetExportSession.m