如何在iOS中将视频文件发送到Firebase存储 [英] How to send video file to firebase storage in ios

查看:53
本文介绍了如何在iOS中将视频文件发送到Firebase存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个视频共享iOS应用,并使用 Firebase5 作为后端.它允许用户选择视频并将其发送到存储.当我尝试时,视频无法推送到数据库服务器.下面是我的代码:

I am working on a Video sharing iOS app and using Firebase5 as backend. It allow User can choose video and send it to storage. When I tried the video cannot push to database server. Below is my code:

static func uploadVideoToFirebaseStorage(videoUrl: URL, onSuccess: @escaping (_ videoUrl: String) -> Void) {
    let videoIdString = NSUUID().uuidString
    //Config.STORAGE_ROOT_REF is var STORAGE_ROOT_REF = "gs://****-cdc3c.appspot.com"
    let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("posts").child(videoIdString)
    storageRef.putFile(from: videoUrl, metadata: nil) { (metadata, error) in
        if error != nil {
            ProgressHUD.showError(error!.localizedDescription)
            return
        }
    }
    storageRef.downloadURL(completion: { (url, error) in
        if let error = error {
            return
        }else {
            let videoUrl = url!.absoluteString
            onSuccess(videoUrl)
        }
    })
}      

推荐答案

调用此函数可将视频上传到Firebase存储

Call this function to upload the video to firebase storage

func uploadTOFireBaseVideo(url: URL,
                                  success : @escaping (String) -> Void,
                                  failure : @escaping (Error) -> Void) {

    let name = "\(Int(Date().timeIntervalSince1970)).mp4"
    let path = NSTemporaryDirectory() + name

    let dispatchgroup = DispatchGroup()

    dispatchgroup.enter()

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let outputurl = documentsURL.appendingPathComponent(name)
    var ur = outputurl
    self.convertVideo(toMPEG4FormatForVideo: url as URL, outputURL: outputurl) { (session) in

        ur = session.outputURL!
        dispatchgroup.leave()

    }
    dispatchgroup.wait()

    let data = NSData(contentsOf: ur as URL)

    do {

        try data?.write(to: URL(fileURLWithPath: path), options: .atomic)

    } catch {

        print(error)
    }

    let storageRef = Storage.storage().reference().child("Videos").child(name)
    if let uploadData = data as Data? {
        storageRef.putData(uploadData, metadata: nil
            , completion: { (metadata, error) in
                if let error = error {
                    failure(error)
                }else{
                    let strPic:String = (metadata?.downloadURL()?.absoluteString)!
                    success(strPic)
                }
        })
    }
}

以下功能可将视频转换为mp4格式,以便可以在iOS或android等任何设备上观看

Following function converts the video to mp4 format so that it can be viewed on any device either it be iOS or android

func convertVideo(toMPEG4FormatForVideo inputURL: URL, outputURL: URL, handler: @escaping (AVAssetExportSession) -> Void) {
    try! FileManager.default.removeItem(at: outputURL as URL)
    let asset = AVURLAsset(url: inputURL as URL, options: nil)

    let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)!
    exportSession.outputURL = outputURL
    exportSession.outputFileType = .mp4
    exportSession.exportAsynchronously(completionHandler: {
        handler(exportSession)
    })
}

这篇关于如何在iOS中将视频文件发送到Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆