Swift: error while saving video data from url

Okay, I had a similar task days ago (ie. downloading a video, regardless of the url source (local or remote) and then saving it locally).

I did an experiment using your function.

See github repo, with working sample and applied fix to your function https://github.com/glennposadas/AVFoundation-SaveLocal

What you’re doing wrong is the way you provide the URL path for the new file.

EDIT: I think you need first to create your folder path. My fileUrl works because the path exists.

This way, it works (.mp4 or .mov, either way):

let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

And it has a path (if you’re going to print it):


Meanwhile, your fileUrl:

    let homeDirectory = URL.init(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
    let fileUrl = homeDirectory
        .appendingPathComponent(UUID.init().uuidString, isDirectory: false)

gives the path:


EDIT: To fix the issue of missing file in your document every time you build your project, then see the changes in the repo I provided above. I added a fix for retrieving the video file from the document folder.

So basically how you should retrieve the file is you only save the fileName, because every run of the build produces a different filePath.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top