Skip to content

Instantly share code, notes, and snippets.

@aleksas
Created August 24, 2024 21:38
Show Gist options
  • Save aleksas/8b1f86ea517475f7835c6d112f56a4ac to your computer and use it in GitHub Desktop.
Save aleksas/8b1f86ea517475f7835c6d112f56a4ac to your computer and use it in GitHub Desktop.
Media Streaming Setup with Docker and GStreamer

Media Streaming Setup with Docker and GStreamer

This gist contains files to set up a media streaming server using Docker, GStreamer, and a looping script. The configuration allows you to stream media files using the Real-Time Streaming Protocol (RTSP). The goal is to stream two synchronized videos packed into an MKV container.

Files in this Gist

1. docker-compose.yml

This Docker Compose file sets up two services:

  • server: A media server using the bluenviron/mediamtx image. It listens on port 8554 for RTSP streaming.
  • streamer: A GStreamer-based streamer using the restreamio/gstreamer Docker image. It streams video files to the RTSP server.

Key parts of the docker-compose.yml:

  • Ports: The server exposes port 8554 for RTSP streaming.
  • Volumes:
    • ./videos directory is mounted to /opt/videos in the streamer container. Place your video files here.
    • ./loop_stream.sh is mounted to /root/loop_stream.sh in the streamer container. This script controls the streaming loop.
  • Command: The streamer service runs the loop_stream.sh script to start the streaming process.

2. loop_stream.sh

A bash script that handles the looping of video streams using GStreamer. This script should be placed in the same directory as the docker-compose.yml file. Make sure it has execution permissions.

Directory Structure

Your directory should look like this:

.
├── docker-compose.yml
├── loop_stream.sh
├── videos
│   └── output.mkv
│   └── trimmed_SampleVideo.mp4
│   └── flipped_SampleVideo.mp4
│   └── big_buck_bunny_720p_30mb.mp4
  • docker-compose.yml: Docker Compose configuration file.
  • loop_stream.sh: Script to loop video streaming.
  • videos/: Directory containing the output.mkv file created by combining videos.

Goal: Stream Two Synchronized Videos Packed into an MKV File

Video Preparation

The videos were created using a sample video file from Sample Videos.

  1. Download the Sample Video:

  2. Create the Video Files:

    • Trimmed Video: Extract the first 120 seconds of the sample video.
      ffmpeg -i SampleVideo_1280x720_30mb.mp4 -t 120 -c copy trimmed_SampleVideo.mp4
    • Flipped Video: Create a vertically flipped version of the first 120 seconds of the sample video.
      ffmpeg -i SampleVideo_1280x720_30mb.mp4 -t 120 -vf "vflip" flipped_SampleVideo.mp4
    • Combine into MKV: Combine the trimmed and flipped videos into a single MKV file, with both videos synchronized.
      ffmpeg -i trimmed_SampleVideo.mp4 -i flipped_SampleVideo.mp4 -map 0:v -map 1:v -c copy output.mkv
  3. Place the MKV File:

    • Place the output.mkv file in the videos directory that will be mounted into the Docker container.

How to Use

  1. Prepare Your Environment:

    • Ensure Docker and Docker Compose are installed on your system.
  2. Place Your Video Files:

    • Put the output.mkv file you created in the videos directory. You may need to create this directory in the same location as your docker-compose.yml file.
  3. Run Docker Compose:

    • Open a terminal and navigate to the directory containing docker-compose.yml.
    • Run the following command to start the services:
      docker-compose up
  4. Access the RTSP Stream:

    • Once the services are running, you can access the RTSP stream at rtsp://localhost:8554/stream1.
  5. Stop the Services:

    • To stop the services, press Ctrl + C in the terminal where Docker Compose is running, or run:
      docker-compose down

Additional Notes

  • You can customize the GStreamer pipeline in the docker-compose.yml file by editing the command section under the streamer service.
  • Adjust the video source and streaming settings in loop_stream.sh to fit your requirements.

License

This gist is provided under the MIT License. See LICENSE for details.

services:
server:
image: bluenviron/mediamtx
ports:
- 8554:8554
streamer:
image: restreamio/gstreamer:2024-07-19T10-11-18Z-prod
depends_on:
- server
volumes:
- ./videos:/opt/videos
- ./loop_stream.sh:/root/loop_stream.sh
command: /bin/bash /root/loop_stream.sh
#!/bin/bash
while true; do
gst-launch-1.0 filesrc location=/opt/videos/output.mkv ! matroskademux name=demux \
demux.video_0 ! queue ! decodebin ! videoconvert ! x264enc ! rtspclientsink location=rtsp://server:8554/stream1 \
demux.video_1 ! queue ! decodebin ! videoconvert ! x264enc ! rtspclientsink location=rtsp://server:8554/stream2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment