I want to make avasset(duration 3.0 sec) from uiimage using swiftui and photosui.

But i got result Failed to write video. Status: AVAssetWriterStatus(rawValue: 3), error: The requested URL was not found on this server. on createVideo`s writer.finishWriting.

Below code i createVideo from uiimage.

More info about parameter

  • outputURL : make URL like this code 'URL(filePath: (UUID().uuidString))'
  • image : image source

I guess, the video only make, but cannot saved because the wrong filepath. Sorry for my english.

Have a nice day.

The file URL is here.

private func getURLFromConverted(with image: UIImage) {
    let url = URL(filePath: (UUID().uuidString))
    createVideo(from: image, outputURL: url, completion: { success in
      if success {
        self.photoConvertedVideoURL = url
        photoEditorViewOpened.toggle()
      }
    })
  }

And createVideo.

func createVideo(from image: UIImage, outputURL: URL, completion: @escaping (Bool) -> Void) {
        do {
            let writer = try AVAssetWriter(outputURL: outputURL, fileType: .mp4)
            let videoSettings: [String: Any] = [
                AVVideoCodecKey: AVVideoCodecType.h264,
                AVVideoWidthKey: image.size.width,
                AVVideoHeightKey: image.size.height
            ]
            let writerInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings)
            writer.add(writerInput)
            let bufferAdapter =  AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: writerInput, sourcePixelBufferAttributes: nil)
            writer.startWriting()
            writer.startSession(atSourceTime: .zero)
            DispatchQueue.global().async {
                let frameDuration = CMTimeMake(value: 1, timescale: 30)
                var frameCount = 0
                while frameCount < 90 { // 30fps * 3s = 90 frames
                    let frameTime = CMTimeMake(value: Int64(frameCount), timescale: 30)
                    guard let pixelBuffer = image.toCVPixelBuffer() else {
                        print("Error converting UIImage to CVPixelBuffer")
                        completion(false)
                        return
                    }
                    bufferAdapter.append(pixelBuffer, withPresentationTime: frameTime)
                    frameCount += 1
                }
                writerInput.markAsFinished()
                writer.finishWriting {
                    DispatchQueue.main.async {
                        if writer.status == .completed {
                            print("Video creation successful")
                            completion(true)
                        } else {
                            print("Failed to write video. Status: \(writer.status), error: \(writer.error?.localizedDescription ?? "Unknown error")")
                            completion(false)
                        }
                    }
                }
            }
        } catch {
            print("Error creating AVAssetWriter: \(error)")
            completion(false)
        }
    }

try

  1. remove file already existed -> not worked
  2. If no directory i make it. -> not worked.

Expecting

  1. avasset of 3 seconds duration from UIImage
0

There are 0 best solutions below