Streaming
Architecture
The streaming system is designed with a robust architecture, encapsulated within a core player function. The key responsibilities include:
Video Uploading: Handles video file uploads, segments them for streaming, generates thumbnails, and prepares the video for playback.
Video Transcoding: Uses FFmpeg for transcoding video segments into streaming-compatible formats.
Video Playback: Uses the Media Source Extensions (MSE) API for dynamic playback of transcoded video segments.
Resource Management: Efficiently manages system resources, including FFmpeg instances and Web Workers.
Key Components and Their Functionalities
Dependencies and Imports
The system relies on specific dependencies to execute various tasks:
@custom/ffmpeg
: A browser-compatible FFmpeg module optimized for video transcoding.
Video Uploading with uploadToIndexDb
Function
uploadToIndexDb
FunctionThe video upload phase involves the following steps:
File Handling:
Creates and mounts a virtual filesystem in FFmpeg (
WORKERFS
) to store the uploaded video.
Metadata Extraction:
Extracts key metadata (e.g., duration, audio presence) using FFmpeg probes.
Thumbnail Generation:
Captures a single frame from the video to generate a thumbnail image.
Video Segmentation:
Splits the video into smaller segments (e.g., 5-second chunks) to enable efficient streaming.
Each segment is stored as an individual file in the virtual filesystem.
JSON Metadata Creation:
Compiles a JSON object detailing the video segments' metadata, including duration, segment count, and audio presence.
Video Playback with transcodeFileToMediaSource
Function
transcodeFileToMediaSource
FunctionThis function manages the playback phase, dynamically transcoding and streaming video segments.
Media Source Initialization:
Creates a
MediaSource
object and attaches it to the video element for MSE-based streaming.
Job Management:
Maintains a queue of transcoding jobs for video segments.
Uses FFmpeg to transcode each segment into a suitable format (e.g., MP4 with H.264 codec).
Buffer Management:
Appends transcoded segments to the
sourceBuffer
of theMediaSource
.Adjusts buffering logic dynamically based on playback position.
Event Handling:
Monitors video and buffer events (e.g., play, pause, seeking) to manage playback state and user interactions.
Error Handling and Recovery:
Implements mechanisms to retry failed transcoding tasks, ensuring a solid playback experience.
FFmpeg Commands
FFmpeg is central to the system, performing tasks such as thumbnail generation, video segmentation, transcoding, and merging.
Thumbnail Generation
Purpose: Captures a single frame from the video.
Key Parameters:
-i
: Input file.-ss
: Timestamp for the frame.-threads
: Number of threads for processing.-vframes 1
: Outputs a single frame.
Video Segmentation
Purpose: Splits the video into 5-second segments.
Key Parameters:
-segment_time
: Duration of each segment.-reset_timestamps
: Resets timestamps for synchronization.-c copy
: Avoids re-encoding to preserve quality.
Transcoding for Playback
Purpose: Transcodes video segments for streaming.
Key Parameters:
-c:v libx264
: Specifies the H.264 codec.-preset ultrafast
: Optimizes for speed.-movflags
: Configures the MP4 container for MSE compatibility.
Audio Transcoding and Merging
Purpose: Merges video and audio streams into a final MP4 file.
Key Parameters:
-c:a aac
: Specifies AAC codec for audio.-af aresample=async=1
: Ensures audio-video sync.
Concatenating Video Segments for Download
Purpose: Combines all video segments for user download.
Key Parameters:
-f concat
: Enables concatenation of video files.
Job Queue Management
Initialization
Jobs Array: Stores job objects representing video segments.
Chunk Parameters: Define segment durations and manage timing.
Creating Job Objects
Each job includes metadata about the segment:
Concurrent Processing
Concurrent FFmpeg Instances:
Job Assignment:
Appending Data to SourceBuffer
SourceBuffer
Event Listener:
Stream Finalization
Signals end-of-stream to the video element:
Directory Structure for Download
Video Folder:
Original file
0kb marker file
Thumbnail file
Preview folder
:Segments named sequentially (e.g.,
1
,2
,3
).metadata.json
.
This ensures a comprehensive and efficient streaming system while supporting user download capabilities.
Last updated