Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Passthrough Export #21

Open
wants to merge 1 commit into
base: m@ster
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 14 additions & 70 deletions LoveLiver/QuickTimeMov.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,76 +58,20 @@ class QuickTimeMov {
}

func write(_ dest : String, assetIdentifier : String) {
do {
// --------------------------------------------------
// reader for source video
// --------------------------------------------------
guard let track = self.track(AVMediaTypeVideo) else {
print("not found video track")
return
}
let (reader, output) = try self.reader(track,
settings: [kCVPixelBufferPixelFormatTypeKey as String:
NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)])

// --------------------------------------------------
// writer for mov
// --------------------------------------------------
let writer = try AVAssetWriter(outputURL: URL(fileURLWithPath: dest), fileType: AVFileTypeQuickTimeMovie)
writer.metadata = [metadataFor(assetIdentifier)]

// video track
let input = AVAssetWriterInput(mediaType: AVMediaTypeVideo,
outputSettings: videoSettings(track.naturalSize))
input.expectsMediaDataInRealTime = true
input.transform = track.preferredTransform
writer.add(input)

// metadata track
let adapter = metadataAdapter()
writer.add(adapter.assetWriterInput)

// --------------------------------------------------
// creating video
// --------------------------------------------------
writer.startWriting()
reader.startReading()
writer.startSession(atSourceTime: kCMTimeZero)

// write metadata track
adapter.append(AVTimedMetadataGroup(items: [metadataForStillImageTime()],
timeRange: dummyTimeRange))

// write video track
input.requestMediaDataWhenReady(on: DispatchQueue(label: "assetAudioWriterQueue", attributes: [])) {
while(input.isReadyForMoreMediaData) {
if reader.status == .reading {
if let buffer = output.copyNextSampleBuffer() {
if !input.append(buffer) {
print("cannot write: \(writer.error)")
reader.cancelReading()
}
}
} else {
input.markAsFinished()
writer.finishWriting() {
if let e = writer.error {
print("cannot write: \(e)")
} else {
print("finish writing.")
}
}
}
}
}
while writer.status == .writing {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.5))
}
if let e = writer.error {
print("cannot write: \(e)")
}
} catch {
print("error")
guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) else {
print("cannot read asset: \(asset.debugDescription)")
return
}
exportSession.outputURL = URL(fileURLWithPath: dest)
exportSession.outputFileType = AVFileTypeQuickTimeMovie
exportSession.metadata = [metadataFor(assetIdentifier), metadataForStillImageTime()]
exportSession.exportAsynchronously {
}
while exportSession.status == .exporting || exportSession.status == .waiting {
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.5))
}
if let e = exportSession.error {
print("cannot write: \(e)")
}
}

Expand Down