add duration to onVideoChunkReady data

This commit is contained in:
Rui Rodrigues 2024-07-16 10:46:24 +01:00
parent 694d9cfa8c
commit b006b1e744
2 changed files with 16 additions and 7 deletions

View File

@ -351,11 +351,15 @@ public final class CameraView: UIView, CameraSessionDelegate {
onInitReady([ onInitReady([
"filepath": chunk.url.path, "filepath": chunk.url.path,
]) ])
case .data(index: let index): case let .data(index: index, duration: duration):
onVideoChunkReady([ var data: [String: Any] = [
"filepath": chunk.url.path, "filepath": chunk.url.path,
"index": index, "index": index,
]) ]
if let duration {
data["duration"] = duration.seconds
}
onVideoChunkReady(data)
} }
} }

View File

@ -14,7 +14,7 @@ class ChunkedRecorder: NSObject {
enum ChunkType { enum ChunkType {
case initialization case initialization
case data(index: UInt64) case data(index: UInt64, duration: CMTime?)
} }
struct Chunk { struct Chunk {
@ -48,7 +48,7 @@ extension ChunkedRecorder: AVAssetWriterDelegate {
case .initialization: case .initialization:
saveInitSegment(segmentData) saveInitSegment(segmentData)
case .separable: case .separable:
saveSegment(segmentData) saveSegment(segmentData, report: segmentReport)
@unknown default: @unknown default:
fatalError("Unknown AVAssetSegmentType!") fatalError("Unknown AVAssetSegmentType!")
} }
@ -60,11 +60,16 @@ extension ChunkedRecorder: AVAssetWriterDelegate {
onChunkReady(url: url, type: .initialization) onChunkReady(url: url, type: .initialization)
} }
private func saveSegment(_ data: Data) { private func saveSegment(_ data: Data, report: AVAssetSegmentReport?) {
let name = "\(chunkIndex).mp4" let name = "\(chunkIndex).mp4"
let url = outputURL.appendingPathComponent(name) let url = outputURL.appendingPathComponent(name)
save(data: data, url: url) save(data: data, url: url)
onChunkReady(url: url, type: .data(index: chunkIndex)) let duration = report?
.trackReports
.filter { $0.mediaType == .video }
.first?
.duration
onChunkReady(url: url, type: .data(index: chunkIndex, duration: duration))
chunkIndex += 1 chunkIndex += 1
} }