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

Make Audio Read and Write Optional #12

Open
wants to merge 1 commit into
base: master
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
38 changes: 24 additions & 14 deletions Waifu2x Video/ConvertingAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ class ConvertingAsset: Hashable, ObservableObject {
print("[SR] Output: \(outputUrl.absoluteString)")

let avAsset = self.avAsset
guard let sampleVideoTrack = avAsset.tracks(withMediaType: .video).first,
let sampleAudioTrack = avAsset.tracks(withMediaType: .audio).first else {
guard let sampleVideoTrack = avAsset.tracks(withMediaType: .video).first else {
throw SRError.tracksNotFound
}

let sampleVideoSize = sampleVideoTrack.naturalSize
let outputVideoSize = CGSize(
width: sampleVideoSize.width * CGFloat(model.options.inputOutputRatio),
Expand Down Expand Up @@ -209,15 +209,26 @@ class ConvertingAsset: Hashable, ObservableObject {
String(kCVPixelBufferPixelFormatTypeKey): kCVPixelFormatType_32ARGB
]
)
let audioReadOutput = AVAssetReaderTrackOutput(
track: sampleAudioTrack,
outputSettings: [
AVFormatIDKey: kAudioFormatLinearPCM
]
)

frameReadOutput.alwaysCopiesSampleData = false
assetReader.add(frameReadOutput)
assetReader.add(audioReadOutput)

// Optional audio reader
let audioReadOutput: AVAssetReaderTrackOutput? = {
if let sampleAudioTrack = avAsset.tracks(withMediaType: .audio).first {
return AVAssetReaderTrackOutput(
track: sampleAudioTrack,
outputSettings: [
AVFormatIDKey: kAudioFormatLinearPCM
]
)
}
return nil
}()

if let audioReadOutput {
assetReader.add(audioReadOutput)
}

// Remove existing file
if FileManager.default.fileExists(atPath: outputUrl.path) {
Expand All @@ -238,12 +249,12 @@ class ConvertingAsset: Hashable, ObservableObject {
]
)
srFrameOutput.expectsMediaDataInRealTime = false

let srAudioOutput = AVAssetWriterInput(
mediaType: .audio,
outputSettings: nil
)

assetWriter.add(srFrameOutput)
assetWriter.add(srAudioOutput)

Expand Down Expand Up @@ -352,7 +363,7 @@ class ConvertingAsset: Hashable, ObservableObject {
}
}
}

srAudioOutput.requestMediaDataWhenReady(on: _audioTrackQueue) { [weak self] in
guard let self = self else { return }
let cleanup = {
Expand All @@ -373,12 +384,11 @@ class ConvertingAsset: Hashable, ObservableObject {
return
}

guard let nextSample = audioReadOutput.copyNextSampleBuffer() else {
guard let audioReadOutput, let nextSample = audioReadOutput.copyNextSampleBuffer() else {
print("[SR] Reached the end of audio track.")
cleanup()
return
}

srAudioOutput.append(nextSample)
}
}
Expand Down