【问题标题】:Record Video and upload to Firebase录制视频并上传到 Firebase
【发布时间】:2016-08-11 20:38:08
【问题描述】:

有人知道如何将视频上传到 Firebase 吗? 我使用了这个链接:record video in swift

我设法在同一视图中录制并播放Gif of my video on giphy

播放我刚刚录制的视频的代码是:

    @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)!)
}

而且我知道如何将图片上传到 Firebase 我需要使用NSString这样的:

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)

但是你如何处理视频呢?

谢谢

【问题讨论】:

  • 尝试在 Firebase 数据库中存储视频是个坏主意。在这里查看我半小时前的回答:stackoverflow.com/questions/36718006/…
  • 好的,谢谢。但为什么这是个坏主意?如果我在哪里可以找到视频存储服务?你的意思是 lite youtube 吗?
  • Firebase 是一个 JSON 数据库。视频(如图像)本质上不是 JSON 数据。关于使用什么视频存储服务的建议在 Stack Overflow 上是题外话,但一些谷歌搜索应该会为您提供一个可以持续一生的列表。
  • 我支持@FrankvanPuffelen - 应避免在 Firebase 中存储视频。但是,如果它是一个非常短的视频,比如 10 秒 @ 10 帧/秒,则可以选择; Quicktime 支持 Base64 编码。此外,视频只是连续观看的一组单独的图像。因此,您可以拍摄视频,将其拼接成帧,然后存储每一帧(移动播放头、复制图像、编码并写入 firebase、冲洗、重复)。请记住,虽然每个节点的大小限制为 10MB,但您将通过视频快速超越这一点。另一种存储解决方案是一个更好的主意。

标签: ios swift video firebase


【解决方案1】:

这是我使用 UIImagePicker 对象类在我的设备中选择视频或图像并将其上传到 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)
    }

【讨论】:

  • 这是否适用于文档目录 URL 而不是照片库?前任。 file:///var/mobile/Containers/Data/Application/E9319B41-05BF-4F5C-8D58-999CE0087D8F/Documents/temporary_video.mp4
  • 当然!只要您可以使用 URL 指向媒体所在的位置,您就可以将其上传到服务器。试一试,告诉我进展如何:)
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 2018-10-17
  • 2017-08-02
  • 2020-08-25
  • 2013-04-26
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多