Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created September 23, 2024 20:24
Show Gist options
  • Save leonid-shevtsov/d4927f265b190dfa061158278129cff8 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/d4927f265b190dfa061158278129cff8 to your computer and use it in GitHub Desktop.
let writerReadySemaphore = DispatchSemaphore(value: 0)
let frameWrittenSemaphore = DispatchSemaphore(value: 0)
let isFinished = Atomic<Bool>(false)
let queue = DispatchQueue(label: "gif_convertor")
videoWriterInput.requestMediaDataWhenReady(on: queue) {
while !isFinished.load(ordering: .sequentiallyConsistent),
videoWriterInput.isReadyForMoreMediaData
{
writerReadySemaphore.signal()
frameWrittenSemaphore.wait()
}
}
// ...
while index < gif.frameCount {
writerReadySemaphore.wait()
// ...write next frame
index+=1
if index == gif.frameCount {
isFinished.store(true, ordering: .sequentiallyConsistent)
}
frameWrittenSemaphore.signal()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment