Firebase Storage Video Streaming
Asked Answered
T

9

92

I'm working on an app that has video streaming functionality. I'm using firebase database and firebase storage. I'm trying to find some documentation on how firebase storage handles video files, but can't really find much.

There's mentioning in the docs that firebase storage works with other google app services to allow for CDN and video streaming, but all searches seem to lead to a dead end. Any advice?

Tangelatangelo answered 16/6, 2016 at 17:12 Comment(4)
As far as I know there are no prefab integrations between Firebase Storage and CDN or video streaming services. Can you provide a link to the documentation where you saw this?Dorinda
"Firebase Storage stores your files in a Google Cloud Storage bucket shared with the default Google App Engine app, making them accessible through both Firebase and Google Cloud APIs. This allows you the flexibility to upload and download files from mobile clients via Firebase and do server-side processing such as image filtering or video transcoding using Google Cloud Platform." firebase.google.com/docs/storage/#key_functionsTangelatangelo
I am also trying to Firebase storage with fastly, but failed on setting domain. I think Firebase hosting can work with fastly, since Firebase hosting has its own CDN, I think it's unnecessary with Firebase hosting.Valve
@Tangelatangelo Did you find a solution?Couchman
M
77

I think there are several types of video streaming, which could change our answer here:

  • Live streaming (subscribers are watching as an event happens)
  • Youtube style (post a video and end users watch at their convenience)

Having built a live streaming Periscope style app using Firebase Storage and the Firebase Realtime Database, I pretty strongly recommend against it--we uploaded three second chunks and synced them via the Realtime Database. While it worked (surprisingly well), there was ~5 second latency over very good internet, and it also wasn't the most efficient solution (after all, you're uploading and storing that video, plus there wasn't any transcoding). I recommend using some WebRTC style, built for video transport, and using the Realtime Database for signaling along side the stream.

On the other side, it's definitely possible to build mobile YT on Firebase features. The trick here is going to be transcoding the video (using something like Zencoder or Bitmovin, more here: https://cloud.google.com/solutions/media/) to chop up your uploaded video into smaller chunks of different resolutions (and different formats, iOS requires HLS for streaming, for instance). You client can store chunk information in the Realtime Database (chunk name, resolutions available, number of chunks), and can download said chunks from Storage as the video progresses.

Ministrant answered 16/6, 2016 at 22:58 Comment(10)
Thanks for the tips and for sharing your experience. My app's requirement is more similar to option 2. users will be able to upload short video clips which other users will later be able to stream. I actually did read the article you sent me the the other day, but was hoping there may some tighter integration between the two services that isn't documented there. Sound like with Firebase storage and GCP pricing for these tasks, I may be better off looking into another service for video hosting. I'm looking into Sprout Video and Vimeo Pro at the moment.Tangelatangelo
I'd check out ziggeo.com as well, they're pretty easy to use and may have all the things you need to be successful in that space.Ministrant
@Tangelatangelo what did you exactly end up using? I'm also in the same boat using firebase. did you use firebase storage and transcoding using some other service(ie. zencoder).Contortive
Also I'm confused about Vimeo Pro as you mentioned, wouldn't Vimeo watermark show through iframe?Contortive
I also successfully uploaded video files to firebase storage. Now I'm trying to figure out how to stream them like YT does.Couchman
@MikeMcDonald , can you describe more on how to do YT style streaming, especially how to do chunk by chunk streaming instead of downloading whole video from Firebase Storage. Also, can WebRTC be must used for one to many video streaming i.e.: broadcasting?Swum
Even I'm working on YT style streaming using firebase. I'm facing the same problem, could anyone help me know how to do chunk by chunk streaming?Baggage
Anyone find out the solution for this?Bithynia
Has anyone tried this www.agora.io ?Dayledaylight
docs.agora.io/en/Video/start_live_flutter?platform=Flutter --integrating with Flutter.Dayledaylight
C
32

If you want to steam a video from Firebase Storage, this is the best way I found. This will depend on the size of your video file. I'm only requesting 10-30mb files so this solution works good for me. Just treat the Firebase Url as a regular url:

String str = "fire_base_video_URL";
Uri uri = Uri.parse(str);

videoViewLandscape.setVideoURI(uri);
progressBarLandScape.setVisibility(View.VISIBLE);
videoViewLandscape.requestFocus();
videoViewLandscape.start();

If you want to loop the video:

videoViewLandscape.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
});

And if you want to show a progress bar before the video starts do this:

videoViewLandscape.setOnInfoListener(new MediaPlayer.OnInfoListener() {
    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
            progressBarLandScape.setVisibility(View.GONE);
            return true;
        }
        else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START){
            progressBarLandScape.setVisibility(View.VISIBLE);
            return true;
        }
        return false;
    }
});

This is not the best way of doing things but it works for me for now until I can find a good video streaming service.

Couchman answered 10/3, 2017 at 20:15 Comment(2)
Is this not using any chunking? It almost looks like it just points the video source to the file URL. Is that right? Does that work okayRyan
performance for this probably sucks unless on Wifi :p PS sorry for taking like 1.5years to reply @RyanCouchman
B
15

2020: Yes, firebase storage video streaming is easy and possible.

All other questions suggest that you use a protocol like HLS. However, this is only necessary if you develop an app for the Apple AppStore that serves videos that are longer than 10 minutes.

In all other cases, you can simply encode your videos in mp4 and upload them to firebase. Your clients can then stream the mp4 without a problem. Just make sure that your moov atom is at the beginning of your mp4 file. This allows to start playing the video immediately, even if it is not fully loaded. Users can also skip ahead or go back thanks to variable bit requests which are supported by firebase storage.

To test it, just upload a video to your firebase storage and open it in your browser.

Bioastronautics answered 7/8, 2020 at 16:18 Comment(7)
I am just wondering if the video size is 1.4GB. if one watch this, then also in download, 1.4GB is also counted at same time ??? (pricing)Trudge
fair point, but without HLS (or DASH) you will still be wasting consumers' bandwidth as you won't be using adaptive resolution streams; not every device wants or needs a 1080p stream.Freshen
@Freshen yes, true. Also your costs could benefit from HLS or DASH because you have to pay for that bandwidth too.Bioastronautics
@Bioastronautics what do you mean "Just make sure that your moov atom is at the beginning of your mp4 file"Disprize
@LaxmikantDange the moov atom is something in the video file that tells how long the video is. If it is at the beginning of the file, your media player can start playing immediately. As far as I know this is usually the case. In my use case I didn't have to do anything. But I am sure google can help you with more details.Bioastronautics
But does it costly?Kantianism
@JohnMelodyMelissa for pricing check firebase costs.Bioastronautics
S
14

You can host HLS videos on Firebase Cloud Storage. It works pretty well for me. The trick is to modify the playlist .m3u8 files to contain the storage folder prefix, and the ?alt=media suffix for each file entry in the playlist:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:2.760000,
<folder_name>%2F1_fileSequence_0.ts?alt=media
#EXT-X-ENDLIST

You also don't really have to use server-side transcoding, you can have the client who uploads the video do it, and save considerable costs.

I've written a full tutorial with source code here: https://itnext.io/how-to-make-a-serverless-flutter-video-sharing-app-with-firebase-storage-including-hls-and-411e4fff68fa

Soothe answered 19/3, 2020 at 8:7 Comment(6)
Hi, do you know what happens in terms of Firebase pricing when we have a HLS video of 200MB in chunks of 10MBs and the user only watched/buffered half of the video? Do they charge us for half of the total MB of cost? And secondly, talking about Flutter, HLS is all we need for both iOS and Android right? there is no need for DASHMiddleclass
How do we manage security with these chunks of video files and m3u8 files? Let's say I don't want some users to access some video files. Is it possible? Lastly, do we have to use Firebase Cloud Storage? Could we instead use Firebase Storage?Middleclass
About the pricing, you're probably right, you're only charged for what's fetched from firebase, and with HLS you only fetch the next few chunks.Soothe
As I mentioned in the blog post, there is native support for HLS on both platforms, but DASH is supported natively only in AndroidSoothe
For security (also mentioned in the blog post), you'll have to add the firebase token to the file name of the m3u8 files before playing them. I haven't tested, but it should work.Soothe
And I think Firebase Storage and Firebase Cloud Storage are the same :)Soothe
F
1

this is my exact implementation for it to start a video playing from storage on firebase as soon as the view is open, and then have the view disappear and then addded a button to click after to replay the video.

I have a demo link with the key so you can see it works. any questions hit me up.

you will just have to create a IBAction if you want the button after the video disappears.

//  BackMuscles.swift
//  Messenger
//
//  Created by Zach Smith on 8/12/21.
//  Copyright © 2021 spaceMuleFitness. All rights reserved.
//

import UIKit
import AVKit
import AVFoundation


class BackMuscles: UIViewController {
    
    @IBOutlet weak var playv: UIButton!
    
  
    let avPlayerViewController = AVPlayerViewController()
    var avPlayer:AVPlayer?


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addBackground()
      
        let movieUrl:NSURL? = NSURL(string: "https://firebasestorage.googleapis.com/v0/b/messenger-test-d225b.appspot.com/o/test%2FTestVideo.mov?alt=media&token=bd4ccba3-b446-43bc-809e-b1152aa3c2ff")
        if let url = movieUrl {
        self.avPlayer = AVPlayer(url: url as URL)
        self.avPlayerViewController.player = self.avPlayer
        }
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: avPlayerViewController.player?.currentItem)
        
        
        self.present(self.avPlayerViewController, animated: true) { () -> Void in
                self.avPlayerViewController.player?.play()        // Do any additional setup after loading the view.
        }
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification) {
            self.avPlayerViewController.dismiss(animated: true)
        }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    @IBAction func playV(sender: UIButton) {
       
            

         
              
                let amovieUrl:NSURL? = NSURL(string: "https://firebasestorage.googleapis.com/v0/b/messenger-test-d225b.appspot.com/o/test%2FTestVideo.mov?alt=media&token=bd4ccba3-b446-43bc-809e-b1152aa3c2ff")
                if let aurl = amovieUrl {
                    self.avPlayer = AVPlayer(url: aurl as URL)
                self.avPlayerViewController.player = self.avPlayer
            
    self.present(self.avPlayerViewController, animated: true) { () -> Void in
            self.avPlayerViewController.player?.play()

       
    }

    }
}
}
Footlights answered 13/8, 2021 at 18:54 Comment(0)
E
0

If you want to create a YT like app, you can first compress the video, I recommend using this library to manage video compression, i recommend the one in this link. I've manage to compress a video of 118 mg to 6 mg in under 42 seconds. It also has a great demo app, just follow the example.

After you get the compressed file upload the file to Storage, in you client app you will play the video url using a player like Exo Player.

Ellata answered 23/3, 2020 at 17:36 Comment(0)
W
0

The video below is pretty good it uses exoplayer to stream instead of mediaplayer or videoViewLanscape https://www.youtube.com/watch?v=s_D5C5e2Uu0

try{
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));

    simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
    String vid = "https://www.youtube.com/watch?v=s_D5C5e2Uu0";
    Uri uri= Uri.parse(vid);
    DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    MediaSource mediaSource = new ExtractorMediaSource(uri,dataSourceFactory,
         extractorsFactory, null, null);
    videoView.setPlayer(simpleExoPlayer);
    simpleExoPlayer.prepare(mediaSource);
    simpleExoPlayer.setPlayWhenReady(true);
}

catch (Exception e){ 

}

The below is the implements in the buid gradle app file that you will need.

implementation 'com.google.android.exoplayer:exoplayer:r2.4.0'
implementation 'com.google.android.exoplayer:exoplayer-core:r2.4.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:r2.4.0'
implementation 'com.google.android.exoplayer:exoplayer-hls:r2.4.0'
implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:r2.4.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:r2.4.0'
Warfare answered 29/3, 2021 at 22:36 Comment(0)
R
0

To use firestorage to play videos, all you need is the full url to the video. You then pass this url into a video view or exoplayer. No full download is needed. The videoview will stream the content YT style

Romaic answered 19/9, 2021 at 14:36 Comment(0)
R
0

Video SDK (https://www.videosdk.live/) could be an option for you. It gives you a zoom-like api for video calling and if you use the record feature you're essentially live streaming your video.

At the moment in their console, you can configure a cloud provider bucket to store the recordings. They seem to have this feature available for AWS and azure. However, GCP is "coming soon" but I know there is a workaround using webhooks to get this working in the meantime.

This may be a more costly solution, but it's very easy to get up and running with their quick start guides.

Recollect answered 5/4, 2023 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.