Saving video in locally (directory) in Swift?

3.2k Views Asked by At

I try to save given video locally after then I need those saved videos for playing video in my app. I can't handle the saving video. Here is my saving try :

func saveVideoDocumentDirectory(url : URL){
        let fileManager = FileManager.default
        let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(".MOV")
        do{
            let videoData = try Data(contentsOf: url)
            fileManager.createFile(atPath: paths as String, contents: videoData, attributes: nil)
        }catch{
            //
        }
        
}

here is the get file try

func getVideo(){
        let fileManager = FileManager.default
        let videoPAth = (self.getDirectoryPath() as NSString).appendingPathComponent(".MOV")
        if fileManager.fileExists(atPath: videoPAth){
        print(videoPAth)
        

        play(url: URL(string: videoPAth)!)
        }else{
            print("No Video")
        }
}

here is my play video func :

 func play(url : URL)
        {
        let player = AVPlayer(url: url)
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            present(playerViewController, animated: true)
            {
                playerViewController.player!.play()
            }
}
1

There are 1 best solutions below

0
On

Instead of Filemanager.createFile(), try using write instead.

let videoData = try Data(contentsOf: url)
try videoData.write(to: paths, options: .atomic)

Also, I recommend creating a folder first (from this answer).

extension URL {
    static func createFolder(folderName: String) -> URL? {
        let fileManager = FileManager.default
        // Get document directory for device, this should succeed
        if let documentDirectory = fileManager.urls(for: .documentDirectory,
                                                    in: .userDomainMask).first {
            // Construct a URL with desired folder name
            let folderURL = documentDirectory.appendingPathComponent(folderName)
            // If folder URL does not exist, create it
            if !fileManager.fileExists(atPath: folderURL.path) {
                do {
                    // Attempt to create folder
                    try fileManager.createDirectory(atPath: folderURL.path,
                                                    withIntermediateDirectories: true,
                                                    attributes: nil)
                } catch {
                    // Creation failed. Print error & return nil
                    print(error.localizedDescription)
                    return nil
                }
            }
            // Folder either exists, or was created. Return URL
            return folderURL
        }
        // Will only be called if document directory not found
        return nil
    }
}

Then, you can save like this:

guard let folderURL = URL.createFolder(folderName: "StoredVideos") else {
    print("Can't create url")
    return
}

let permanentFileURL = folderURL.appendingPathComponent(nameOfYourFile).appendingPathExtension("MOV")
let videoData = try Data(contentsOf: url)
try videoData.write(to: permanentFileURL, options: .atomic)

This will save you the hassle of NSSearchPathForDirectoriesInDomains.