快速视频到文档目录

Posted

技术标签:

【中文标题】快速视频到文档目录【英文标题】:swift video to document directory 【发布时间】:2016-04-10 22:00:41 【问题描述】:

我需要将使用 UIImagePicker 捕获的视频保存到应用文档目录中的自定义文件夹中

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    picker.dismissViewControllerAnimated(true, completion: nil)
    if let fileURL = info[UIImagePickerControllerMediaURL] as? NSURL 
        if let videoData = NSData(contentsOfURL: fileURL) 
            //save file to doc dir or save NSData into Core Data binary field

        
    

或者,我可以使用外部存储将 videoData 保存在 Core Data 二进制字段中,但是由于无法将 NSData 转换为 NSURL,因此我无法使用媒体播放器播放视频。

【问题讨论】:

不要将整个电影数据加载到内存中,您的应用可能会因文件大小而崩溃。只需使用文件管理器在 url 方法移动项目将其移动到新位置 ***.com/a/33189238/2303865 【参考方案1】:

在 Swift 3 上试试这个

    var uniqueVideoID = ""
    var videoURL:NSURL? = NSURL()
    var uniqueID = ""

    //Add this to ViewDidLoad
    uniqueID = NSUUID().UUIDString


    //Getting the path as URL and storing the data in myVideoVarData.
            videoURL = info[UIImagePickerControllerMediaURL] as? URL as NSURL?
        let myVideoVarData = try! Data(contentsOf: videoURL! as URL)

        //Now writeing the data to the temp diroctory.
        let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject
            uniqueVideoID = uniqueID  + "TEMPVIDEO.MOV"
        let tempDataPath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String
            try? myVideoVarData.write(to: URL(fileURLWithPath: tempDataPath), options: [])

        //Getting the time value of the movie.
        let fileURL = URL(fileURLWithPath: tempDataPath)
        let asset = AVAsset(url: fileURL)
        let duration : CMTime = asset.duration
            videoAlertIdVar = duration.value // Control for the runVideoAlert function.

        //Now we remove the data from the temp Document Diroctory.
        do
        let fileManager = FileManager.default
        try fileManager.removeItem(atPath: tempDataPath)
         catch 
            //Do nothing
        

        // Cheacking to see if video is under the 18500 (:30 seconds).
        if duration.value <= 18500 
           yesOrNo = "no" //Control for the button in the mainViewController is hidden or not. The default is "yes"

        //Here we are writing the data to the Document Directory for use later on.
        let docPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentsDirectory: AnyObject = docPaths[0] as AnyObject
            uniqueVideoID = uniqueID  + "VIDEO.MOV"
        let docDataPath = documentsDirectory.appendingPathComponent(uniqueVideoID) as String
            try? myVideoVarData.write(to: URL(fileURLWithPath: docDataPath), options: [])
            print("docDataPath under picker ",docDataPath)

        //This creates a thumbnail image.
        let assetImageGenerate = AVAssetImageGenerator(asset: asset)
            assetImageGenerate.appliesPreferredTrackTransform = true
        let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)

         //This adds the thumbnail to the imageview.
        if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) 
            videoThumbnailOutlet.image = UIImage(cgImage: videoImage)
          
        else
            //Do nothing
   

播放视频

    //Note: The _videoData_ is the same as the _uniqueID_
    var videoData = ""

     func play ()

    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]

    let videoDataPath = documentsDirectory + "/" + videoData + "VIDEO.MOV"

    let filePathURL = URL(fileURLWithPath: videoDataPath)


    let player = AVPlayer(url: filePathURL)
    let playerController = AVPlayerViewController()
        playerController.player = player
    self.present(playerController, animated: true) 
        player.play()


    

【讨论】:

【参考方案2】:

试试这个。斯威夫特 2.2

    var uniqueVideoID = ""
    var videoURL: NSURL? = NSURL()
    var uniqueID = ""

    //Add this to ViewDidLoad
    uniqueID = NSUUID().UUIDString

            //Getting the path as URL and storing the data in myVideoVarData.
        videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
        let myVideoVarData = NSData(contentsOfURL: videoURL!)!

        //Now writeing the data to the temp diroctory.
        let tempPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let tempDocumentsDirectory: AnyObject = tempPath[0]
            uniqueVideoID = uniqueID  + "TEMPVIDEO.MOV"
        let tempDataPath = tempDocumentsDirectory.stringByAppendingPathComponent(uniqueVideoID)
            myVideoVarData.writeToFile(tempDataPath, atomically: false)

        //Getting the time value of the movie.
        let fileURL = NSURL(fileURLWithPath: tempDataPath)
        let asset = AVAsset(URL: fileURL)
        let duration : CMTime = asset.duration


        //Now we remove the data from the temp Document Diroctory.
        do
        let fileManager = NSFileManager.defaultManager()
        try fileManager.removeItemAtPath(tempDataPath)
         catch 
            //Do nothing
        

        // Cheacking to see if video is under the 18500 (:30 seconds).
        if duration.value <= 18500 


        //Here we are writing the data to the Document Directory for use later on.
        let docPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentsDirectory: AnyObject = docPaths[0]
            uniqueVideoID = uniqueID  + "VIDEO.MOV"
        let docDataPath = documentsDirectory.stringByAppendingPathComponent(uniqueVideoID)
            myVideoVarData.writeToFile(docDataPath, atomically: false)
            print("docDataPath under picker ",docDataPath)

        //This creates a thumbnail image.
        let assetImageGenerate = AVAssetImageGenerator(asset: asset)
            assetImageGenerate.appliesPreferredTrackTransform = true
        let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)

         //This adds the thumbnail to the imageview.
        if let videoImage = try? assetImageGenerate.copyCGImageAtTime(time, actualTime: nil) 
            videoThumbnailOutlet.image = UIImage(CGImage: videoImage)
          
        else
            //Do nothing
   

现在播放视频。

    //Note: The _videoData_ is the same as the _uniqueID_
    var videoData = ""


    func play ()

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]

    let videoDataPath = documentsDirectory + "/" + videoData + "VIDEO.MOV"

    let filePathURL = NSURL.fileURLWithPath(videoDataPath)


    let player = AVPlayer(URL: filePathURL)
    let playerController = AVPlayerViewController()
        playerController.player = player
    self.presentViewController(playerController, animated: true) 
        player.play()


    
    

【讨论】:

以上是关于快速视频到文档目录的主要内容,如果未能解决你的问题,请参考以下文章

我们可以使用 Facebook SDK 将文档目录视频上传到 Facebook

使用 MPMoviePlayerViewController 来自文档目录的视频

如何将 UIPickerViewController 中的图像或视频保存到文档目录?

如何将视频从应用程序文档目录移动到相机胶卷?

Swift - 视频文件数组可以很好地保存到应用程序库,但不能正确保存到应用程序文档目录

如何将 UIImage 保存到文档目录?