Nuke icon indicating copy to clipboard operation
Nuke copied to clipboard

NukeVideo: local video preview is not generated

Open alexvasilkov opened this issue 6 months ago • 0 comments

I have several local video files and I need to display a grid of thumbnails.

I registered the video decoder like this:

ImageDecoderRegistry.shared.register(ImageDecoders.Video.init)

But it doesn't work, thumbnails are not loaded.

I noticed that decode(_ data: Data) function of the ImageDecoders.Video class always returns an empty image. But decodePartiallyDownloadedData(_ data: Data) implementation looks just fine.

I ended up implementing a simple wrapper that fixes the issue:

private class VideoDecoderFixed: ImageDecoding, @unchecked Sendable {
  private let decoder: ImageDecoders.Video
  var isAsynchronous: Bool { decoder.isAsynchronous }

  init?(context: ImageDecodingContext) {
    guard let decoder = ImageDecoders.Video(context: context) else { return nil }
    self.decoder = decoder
  }

  func decode(_ data: Data) throws -> ImageContainer {
    if let image = decoder.decodePartiallyDownloadedData(data) { return image }
    return try decoder.decode(data)
  }

  func decodePartiallyDownloadedData(_ data: Data) -> ImageContainer? {
    decoder.decodePartiallyDownloadedData(data)
  }
}

Is it expected that decode(_ data: Data) function returns an empty image? Do you think it could use the same logic as in decodePartiallyDownloadedData(_ data: Data)? Does the workaround described above seem fine?

Thanks!

alexvasilkov avatar Aug 22 '24 19:08 alexvasilkov