how to save video file into document directory

后端 未结 9 740
庸人自扰
庸人自扰 2020-12-12 21:45

I am capturing video using following code:

UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType =  UIImagePickerControllerSo         


        
相关标签:
9条回答
  • 2020-12-12 22:04
     videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
    urlString=[urlvideo path];
    NSLog(@"path url %@",videoUrl);
    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sourcePath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.mp4"];
    
    [videoData writeToFile:sourcePath atomically:YES];
    //Below code will save video to iOS Device
     ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:videoUrl
                                completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    

    Hope this help

    0 讨论(0)
  • 2020-12-12 22:09
        videoURL = info[UIImagePickerControllerMediaURL]as? NSURL
        print(videoURL!)
    
        let urlData=NSData(contentsOf: videoURL as! URL)
    
        if((urlData) != nil)
        {
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentDirectory = path.first! as NSString
            let fileName = "Video.MOV"
            let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)
    
            print(PDFPathFileName)
    
            DispatchQueue.main.async( execute: {
                urlData?.write(toFile: PDFPathFileName, atomically: true)
            })
    
            do {
                let asset = AVURLAsset(url: videoURL as! URL , options: nil)
                let imgGenerator = AVAssetImageGenerator(asset: asset)
                imgGenerator.appliesPreferredTrackTransform = true
                let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
                let thumbnail = UIImage(cgImage: cgImage)
                imgView.image = thumbnail
                // thumbnail here
    
            } catch let error {
                print("*** Error generating thumbnail: \(error.localizedDescription)")
            }
    
        }
        self.dismiss(animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-12 22:12

    Bit of R&D, this worked for me

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    

    {

     NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
      // Save video to app document directory
    
    NSString *filePath = [url path];
    NSString *pathExtension = [filePath pathExtension] ;
    if ([pathExtension length] > 0)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", [filePath lastPathComponent]]];
    
       // Method last path component is used here, so that each video saved will get different name.
    
        NSError *error = nil ;
        BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
    
        if (!res)
        {
            NSLog(@"%@", [error localizedDescription]) ;
        }
        else
        {
           NSLog(@"File saved at : %@",localFilePath);
        }
    }
    

    }

    //Also when you have to check for same video already exist in app document directory and you don't want create multiple copies of it then made some changes as below

    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
    NSURL *videoAsset = [info objectForKey:UIImagePickerControllerReferenceURL];
    
    __weak typeof(self) weakSelf = self;
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:videoAsset resultBlock:^(ALAsset *asset)
    {
        weakSelf.selectedFileName = [[asset defaultRepresentation] filename];
        NSLog(@"Video Filename %@",weakSelf.selectedFileName);
    
        // Save video to doc directory
        NSString *filePath = [url path];
        NSString *pathExtension = [filePath pathExtension] ;
        if ([pathExtension length] > 0)
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
            NSString *documentsDirectory = [paths objectAtIndex:0];
    
            NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", weakSelf.selectedFileName]];
    
            //check if same video is having its copy in app directory.
            //so that multiple entries of same file should not happen.
    
            BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
    
            if (!fileExists)
            {
                NSError *error = nil ;
    
                BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
    
                if (!res)
                {
                    NSLog(@"%@", [error localizedDescription]) ;
                }
                else
                {
                    NSLog(@"File saved at : %@",localFilePath);
                    weakSelf.filePathURL = [NSURL URLWithString:localFilePath];
                }
            }
            else
            {
                NSLog(@"File exist at : %@",localFilePath);
                weakSelf.filePathURL = [NSURL URLWithString:localFilePath];
            }
    
        }
    }
    

    }

    //Where weakSelf.selectedFileName & weakSelf.filePathURL are NSString and NSURL type properties of my class respectively.

    0 讨论(0)
  • 2020-12-12 22:16

    We can use NSFileManager's moveItemAtPath:toPath:error: method to move the video file to our app's document directory. It's more efficient.

    Also we should get the extension of the video file and use it as extension of our local video file name.

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [self dismissViewControllerAnimated:YES completion:nil] ;
    
        if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) {
            // video
            NSURL *url = [info[UIImagePickerControllerMediaType] ;
            NSString *filePath = [url path] ;
            NSString *pathExtension = [filePath pathExtension] ;
            if ([pathExtension length] > 0) {
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
                NSString *documentsDirectory = [paths objectAtIndex:0] ;
                NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"temp.%@", pathExtension]] ;
                NSError *error = nil ;
                BOOL res = [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:localFilePath error:&error] ;
                if (!res) {
                    NSLog(@"%@", [error localizedDescription]) ;
                }
            }
        }
    }
    

    It works for me.

    0 讨论(0)
  • 2020-12-12 22:17

    Just Call this function, and it will do every thing for you :)

        private func saveVideo(url:URL) {
        DispatchQueue.global(qos: .userInitiated).async {
            guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
            if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(url.lastPathComponent).path) {
                URLSession.shared.downloadTask(with: url) { (location, response, error) -> Void in
                    guard let location = location else { return }
                    let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? url.lastPathComponent)
    
                    do {
                        try FileManager.default.moveItem(at: location, to: destinationURL)
                        PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in
                            if authorizationStatus == .authorized {
                                PHPhotoLibrary.shared().performChanges({
                                    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                                        DispatchQueue.main.async {
                                            if completed {
                                                self.view.makeToast(NSLocalizedString("Video Saved!", comment: "Video Saved!"), duration: 3.0, position: .center)
                                            } else {
                                                print(error!)
                                            }
                                        }
                                }
                            }
                        })
                    } catch { print(error) }
    
                    }.resume()
            } else {
                print("File already exists at destination url")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 22:18

    Swift 3/4

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            // recover video URL
            let url = info[UIImagePickerControllerMediaURL] as? URL
            // check if video is compatible with album
            let compatible: Bool = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((url?.path)!)
            // save
            if compatible {
                UISaveVideoAtPathToSavedPhotosAlbum((url?.path)!, self, nil, nil)
                print("saved!!!! \(String(describing: url?.path))")
            }
            dismiss(animated: true, completion: nil)
        }
    //   error
        func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer) {
        }
    

    saving example Path:-

    "/private/var/mobile/Containers/Data/Application/EA53C844-7931-446C-800D-DA90717426BB/tmp/xxxxxx.MOV"
    
    0 讨论(0)
提交回复
热议问题