Boombox

repository·master·Indexed 42 days ago

https://github.com/membraneframework/boombox

A high-level tool for audio and video streaming built on the Membrane Framework. It enables transformations between media formats, containers, and streaming protocols, serving as a bridge between Elixir and external media pipelines. It provides a CLI, an Elixir API (Boombox.run/2), and a Python SDK (boomboxlib v0.2.13) for real-time media processing, such as converting WebRTC to HLS or applying video filters using NumPy.

Tokens
4.4K
Snippets
22
Records
24
Agent score
54%

What's inside Boombox

  1. Interact with raw media using RawData endpoints

    master

    For direct media manipulation (e.g., applying AI effects to video or audio), use the RawData endpoint. This allows Boombox to produce or accept AudioPacket and VideoPacket objects.

    There are two primary patterns for raw data interaction:

    1. Reading from Boombox: Define a RawData output and iterate over the stream using the boombox.read() generator.
    2. Writing to Boombox: Define a RawData input and use the boombox.write(packet) method to feed packets into the stream. This is typically used within a with context manager.
    # Pattern 1: Reading raw packets from a stream
    boombox = Boombox(
        input=any_input_endpoint,
        output=RawData(video=True, audio=True)
    )
    
    for packet in boombox.read():
        process_packet(packet)
    
    # Pattern 2: Writing raw packets to a stream
    with Boombox(
        input=RawData(video=True, audio=True),
        output=any_output_endpoint
    ) as boombox:
        for packet in some_packet_generator():
            boombox.write(packet)
  2. Transform media streams with Boombox

    master

    You can use Boombox to transform one stream format into another using a declarative interface. To do this, create a Boombox object and define its input and output using endpoints from the boombox.endpoints module (e.g., MP4, RTMP). Calling .wait() will start the process and block until the stream is finished.

    from boombox import Boombox, MP4, RTMP
    
    boombox = Boombox(
        input=RTMP("rtmp://my.stream.source:2137/app/key"),
        output=MP4("path/to/target.mp4")
    )
    boombox.wait()
  3. Install boomboxlib via pip

    master

    Install the boomboxlib package using pip. Note that because the package relies on native libraries for encoding and decoding, it is only available for the following platforms:

    • MacOS on ARM architecture
    • Linux on x86 architecture
    • Linux on ARM architecture
    pip install boomboxlib
  4. Generate an HLS livestream from WebRTC

    master

    This demo demonstrates how to use boomboxlib to receive a WebRTC stream (e.g., from a browser capturing a webcam feed) and convert it into a live HLS playlist.

    Prerequisites

    1. Install boomboxlib:

      pip install ../.. 

      (Note: The path above assumes you are running the install from within the example directory relative to the repository root. Adjust the path to point to the actual location of the boomboxlib package.)

    2. Hardware: A webcam and optionally a microphone.

    Usage Steps

    1. Start the server: Run the Python application:
      python main.py
    2. Start the WebRTC stream: Open http://localhost:8000/sender.html in your browser and click Connect. You should see your webcam feed on this page.
    3. View the HLS stream: Open http://localhost:8000/hls.html and click Play. This page provides access to the converted HLS livestream.
  5. Install the Boombox CLI

    master

    To use Boombox via the command line, download the binary, make it executable, and run it. Ensure Elixir is installed on your system. The first execution will install necessary artifacts to a default directory. You can customize this location using the MIX_INSTALL_DIR environment variable.

    wget https://raw.githubusercontent.com/membraneframework/boombox/v0.1.0/bin/boombox
    chmod u+x boombox
    ./boombox
  6. Run the Anonymization Demo

    master

    This demo demonstrates Boombox's ability to interact with media streams in Python. It captures a WebRTC stream from a browser webcam, processes it to blur faces, distort voices, and transcribe speech into subtitles, and then streams the processed output back to a browser.

    Prerequisites

    1. Dependencies: Install the required Python packages using pip:
      pip install -r requirements.txt
    2. Hardware: A webcam and a microphone are required.

    Execution Steps

    1. Start the Python script:
      python anonymization_demo.py
    2. Connect the Receiver: Open http://localhost:8000/receiver.html in your browser and click Connect.
    3. Connect the Sender: Open http://localhost:8000/sender.html in your browser and click Connect.

    Expected Result

    • The sender page will display the unmodified webcam feed.
    • The receiver page will display the processed stream (blurred face, distorted voice, and subtitles).
    # Install dependencies
    pip install -r requirements.txt
    
    # Run the demo
    python anonymization_demo.py
  7. Run the MP4 color inversion demo

    master

    This demo showcases how to use boomboxlib to invert the colors of an MP4 file.

    Prerequisites

    1. Install boomboxlib: Ensure you have the library installed via pip.
    2. Media Player: You will need a player like vlc or ffplay to view the resulting file.

    Steps to Run

    1. Execute the Python script to process the video:
    python main.py
    1. Play the generated output file (inverted.mp4) using ffplay:
    ffplay inverted.mp4
    # Run the python program
    python main.py
    
    # Play the output MP4
    ffplay inverted.mp4
  8. Transform media streams with Boombox

    master

    Boombox allows you to read media from a source (like a URL or an Array of streams) and write it to a destination (like a file or an Array). You can iterate over the stream using the .read() method to access individual packets, such as VideoPacket, allowing for real-time transformations (e.g., using NumPy to flip video frames) before writing them to the output via .write(packet).

    Always call .close(wait=True) on the output Boombox instance to ensure all buffers are flushed and the stream is properly finalized.

    boombox1 = Boombox(
    input="https://raw.githubusercontent.com/membraneframework/static/gh-pages/samples/big-buck-bunny/bun33s.mp4",
    output=Array(video=True, audio=True),
    )
    boombox2 = Boombox(input=Array(video=True, audio=True), output="output.mp4")
    for b in boombox1.read():
        if isinstance(b, VideoPacket):
            b.payload = np.flip(b.payload, axis=0)
        boombox2.write(b)
    boombox2.close(wait=True)
  9. Use Boombox.run/2 in Elixir

    master

    The primary way to use Boombox in Elixir is via Boombox.run/2. It accepts input and output options to perform media transformations.

    Example: RTMP to HLS

    Boombox.run(input: "rtmp://localhost:5432", output: "index.m3u8")

    Example: Generating a video stream from an Enumerable You can pass a stream of Boombox.Packet structs as input. This is useful for programmatic video generation.

    # ... (setup code for image stream) ...
    |> Boombox.run(
      input: {:stream, video: :image, audio: false},
      output: {:webrtc, "ws://localhost:8830"}
    )