2

Anybody know how to upload a video to firebase? I used this link: record video in swift

I managed to record and have a playback in same view Gif of my video on giphy

The code to play the video I just recorded is:

    @IBAction func playVideo(sender: AnyObject) {
    print("Play a video")

    // Find the video in the app's document directory
    let paths = NSSearchPathForDirectoriesInDomains(
        NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory: AnyObject = paths[0]
    let dataPath = documentsDirectory.stringByAppendingPathComponent(saveFileName)

    let videoAsset = (AVAsset(URL: NSURL(fileURLWithPath: dataPath)))
    let playerItem = AVPlayerItem(asset: videoAsset)

    print(playerItem)


    let videoView = UIView(frame: CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.width, self.view.bounds.height))


    let pathURL = NSURL.fileURLWithPath(dataPath)
    moviePlayer = MPMoviePlayerController(contentURL: pathURL)
    if let player = moviePlayer {
        player.view.frame = videoView.bounds
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        videoView.addSubview(player.view)

    }

    moviePlayer!.view.frame = videoPreview.bounds
    moviePlayer!.view.center = CGPointMake(CGRectGetMidX(videoPreview.bounds), CGRectGetMidY(videoPreview.bounds))
    videoPreview.addSubview((moviePlayer?.view)!)
}

And I know how to upload a PICTURE to Firebase I need to use NSStringlike this:

var base64String : NSString!

        //let pic = UIImagePNGRepresentation(UIImage(named: "3")!)
    let picture = UIImageJPEGRepresentation(self.profilePictureImageView.image!, 0.1)!


    self.base64String = picture.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

    let updatedProfileInfo = [
        "provider": USER_REF.authData.provider,
        "email": self.emailTextfield.text!,
        "Username": self.usernameTextfield.text!,
        "ProfilePicture" :  self.base64String,
        "ProfileDescription" : self.bioDescriptionTextfield.text
    ]
    USER_REF.updateChildValues(updatedProfileInfo)

But how do you do with videos?

Thank you

  • Trying to store video in the Firebase Database is a Bad Idea. See my answer from half an hour ago here: http://stackoverflow.com/questions/36718006/handling-image-store-to-firebase-with-swift – Frank van Puffelen Apr 19 '16 at 15:29
  • 1
    ok, thank you. but why is that a bad idea? and if I where can I find video storage services? do you mean lite youtube ? –  Apr 19 '16 at 15:35
  • 2
    Firebase is a JSON database. Video (like images) is inherently not JSON data. Recommendations of what video storage service to use are off-topic here on Stack Overflow, but a few google searches should get you a list to last a lifetime. – Frank van Puffelen Apr 19 '16 at 15:57
  • 1
    I'm with @FrankvanPuffelen on this - storing video in Firebase should be avoided. However, if it's a very short video, say 10 seconds @ 10 frames per second, there are options; Quicktime supports Base64 encoding. Also, a video is simply a set of individual images viewed in succession. So, you could take the video, splice it into frames and then store each frame (move playhead, copy image, encode and write to firebase, rinse, repeat). Remember though that each node is limited to 10MB and you will push beyond that very quickly with video. Another storage solution is a much better idea. – Jay Apr 19 '16 at 17:33

1 Answers1

4

This was my solution on using the UIImagePicker object class to select a video or image in my device and upload it to Fireabase:

@IBAction func uploadButton(_ sender: Any) {
    // Configuration
    let picker = UIImagePickerController()
    picker.allowsEditing = true
    picker.delegate = self
    picker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]

    // Present the UIImagePicker Controller
    present(picker, animated: true, completion: nil)
}

// The didFinishPickingMediaWithInfo let's you select an image/video and let's you decide what to do with it. In my example, I decided to convert the selected data into video and upload it to Firebase Storage
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let videoURL = info[UIImagePickerControllerMediaURL] as? NSURL {
        // we selected a video
        print("Here's the file URL: ", videoURL)

        // Where we'll store the video:
        let storageReference = FIRStorage.storage().reference().child("video.mov")

        // Start the video storage process
        storageReference.putFile(videoURL as URL, metadata: nil, completion: { (metadata, error) in
            if error == nil {
                print("Successful video upload")
            } else {
                print(error?.localizedDescription)
            }
        })

      }
            //Dismiss the controller after picking some media
            dismiss(animated: true, completion: nil)
    }
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • 2
    does this work with a Documents Directory URL instead of the Photos Gallery ? ex. file:///var/mobile/Containers/Data/Application/E9319B41-05BF-4F5C-8D58-999CE0087D8F/Documents/temporary_video.mp4 – omarojo Sep 25 '17 at 01:58
  • 1
    Sure! As long as you can point with an URL to where the media is you can upload it to a server. Give it a shot, let me know how it goes :) –  Sep 25 '17 at 15:45