mirror of
https://github.com/samsonjs/SJSAssetExportSession.git
synced 2026-04-27 14:57:46 +00:00
Extract BaseTests class
This commit is contained in:
parent
4c7b64f045
commit
3be5b7f28e
2 changed files with 51 additions and 39 deletions
50
Tests/SJSAssetExportSessionTests/BaseTests.swift
Normal file
50
Tests/SJSAssetExportSessionTests/BaseTests.swift
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
//
|
||||||
|
// BaseTests.swift
|
||||||
|
// SJSAssetExportSession
|
||||||
|
//
|
||||||
|
// Created by Sami Samhuri on 2025-01-19.
|
||||||
|
//
|
||||||
|
|
||||||
|
import AVFoundation
|
||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
class BaseTests {
|
||||||
|
func resourceURL(named name: String) -> URL {
|
||||||
|
Bundle.module.resourceURL!.appending(component: name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeAsset(url: URL) -> sending AVAsset {
|
||||||
|
AVURLAsset(url: url, options: [
|
||||||
|
AVURLAssetPreferPreciseDurationAndTimingKey: true,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTemporaryURL(function: String = #function) -> AutoDestructingURL {
|
||||||
|
let timestamp = Int(Date.now.timeIntervalSince1970)
|
||||||
|
let f = function.replacing(/[\(\)]/, with: { _ in "" })
|
||||||
|
let filename = "\(Self.self)_\(f)_\(timestamp).mp4"
|
||||||
|
let url = URL.temporaryDirectory.appending(component: filename)
|
||||||
|
return AutoDestructingURL(url: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeVideoComposition(
|
||||||
|
assetURL: URL,
|
||||||
|
size: CGSize? = nil,
|
||||||
|
fps: Int? = nil
|
||||||
|
) async throws -> sending AVMutableVideoComposition {
|
||||||
|
let asset = makeAsset(url: assetURL)
|
||||||
|
let videoComposition = try await AVMutableVideoComposition.videoComposition(
|
||||||
|
withPropertiesOf: asset
|
||||||
|
)
|
||||||
|
if let size {
|
||||||
|
videoComposition.renderSize = size
|
||||||
|
}
|
||||||
|
if let fps {
|
||||||
|
let seconds = 1.0 / TimeInterval(fps)
|
||||||
|
videoComposition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid
|
||||||
|
videoComposition.frameDuration = CMTime(seconds: seconds, preferredTimescale: 600)
|
||||||
|
}
|
||||||
|
return videoComposition
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,45 +10,7 @@ import CoreLocation
|
||||||
import SJSAssetExportSession
|
import SJSAssetExportSession
|
||||||
import Testing
|
import Testing
|
||||||
|
|
||||||
final class ExportSessionTests {
|
final class ExportSessionTests: BaseTests {
|
||||||
private func resourceURL(named name: String) -> URL {
|
|
||||||
Bundle.module.resourceURL!.appending(component: name)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func makeAsset(url: URL) -> sending AVAsset {
|
|
||||||
AVURLAsset(url: url, options: [
|
|
||||||
AVURLAssetPreferPreciseDurationAndTimingKey: true,
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
private func makeTemporaryURL(function: String = #function) -> AutoDestructingURL {
|
|
||||||
let timestamp = Int(Date.now.timeIntervalSince1970)
|
|
||||||
let f = function.replacing(/[\(\)]/, with: { _ in "" })
|
|
||||||
let filename = "\(Self.self)_\(f)_\(timestamp).mp4"
|
|
||||||
let url = URL.temporaryDirectory.appending(component: filename)
|
|
||||||
return AutoDestructingURL(url: url)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func makeVideoComposition(
|
|
||||||
assetURL: URL,
|
|
||||||
size: CGSize? = nil,
|
|
||||||
fps: Int? = nil
|
|
||||||
) async throws -> sending AVMutableVideoComposition {
|
|
||||||
let asset = makeAsset(url: assetURL)
|
|
||||||
let videoComposition = try await AVMutableVideoComposition.videoComposition(
|
|
||||||
withPropertiesOf: asset
|
|
||||||
)
|
|
||||||
if let size {
|
|
||||||
videoComposition.renderSize = size
|
|
||||||
}
|
|
||||||
if let fps {
|
|
||||||
let seconds = 1.0 / TimeInterval(fps)
|
|
||||||
videoComposition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid
|
|
||||||
videoComposition.frameDuration = CMTime(seconds: seconds, preferredTimescale: 600)
|
|
||||||
}
|
|
||||||
return videoComposition
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test func test_sugary_export_720p_h264_24fps() async throws {
|
@Test func test_sugary_export_720p_h264_24fps() async throws {
|
||||||
let sourceURL = resourceURL(named: "test-4k-hdr-hevc-30fps.mov")
|
let sourceURL = resourceURL(named: "test-4k-hdr-hevc-30fps.mov")
|
||||||
let destinationURL = makeTemporaryURL()
|
let destinationURL = makeTemporaryURL()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue