Search code examples
swiftfirebasevideofirebase-storagemp4

Swift - AVPlayer only starts playing .mp4 videos from Firebase Storage after full download


I have MP4 video files stored in Firebase Storage that I'm trying to play in an iOS app using AVPlayer. Here's a snippet of my code:

private(set) var player: AVPlayer!
player = AVPlayer(playerItem: .init(url: url))
player.play()

However, the videos only start playing after they are fully downloaded, which is not what I want. I want the videos to start streaming immediately, even as the rest of the content is being downloaded.

From my understanding, AVPlayer should support streaming video content, so I'm wondering if Firebase Storage is the issue here.

How can I get AVPlayer to start playing video files from Firebase Storage before they're fully downloaded? Is there a way to enable streaming with Firebase Storage and AVPlayer, or should I consider other solutions like using a different hosting service? Any suggestions would be greatly appreciated!


Solution

  • Turns out if you capture a video with AVCaptureSession it forcefully puts the MOOV atom* at the end of the file. That means that AVPlayer won't know the duration of the video until it's fully downloaded and as a result, it cannot "stream" the video.

    Thankfully, there are many FFmpeg libraries (e.g. FFmpegKit) that can move the MOOV atom at the beginning of the file.

    • the MOOV atom defines the timescale, duration, and display characteristics of a video file