SDL2 FFmpeg
A simple go library for playing video and audio. It uses the FFmpeg for decoding the video/audio and SDL2 for rendering the video.
The library is based on Reisen library which is based on libav (i.e. ffmpeg).
Dependencies
Following components are required for this library to work.
-
Go v1.17+
-
Windows
Go should be installed on Windows OS when MSYS2 is used.
-
Download and install Go on Windows OS (see Go install)
-
Set Go path in MSYS2
echo 'export PATH=/c/Program\ Files/Go/bin:$PATH' >> ~/.bashrc
-
Build Go package example
gi clone https://github.com/faiface/pixel-examples.git
cd platformer
go run main.go
go build
./platformer.exe
-
Linux
Go should be installed in default folder (see Go install).
-
MSYS2 (for Windows)
MSYS2 is required only if you need to build the project for Windows.
-
GCC toolchain
- Linux
sudo apt install build-essential libtool autotools-dev automake pkg-config bsdmainutils curl git
gcc --version
- Windows (MSYS2)
pacman -S --needed base-devel mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain
echo 'export PATH=/mingw64/bin:$PATH' >> ~/.bashrc
pacman -S git
echo 'export PATH=/c/Go/bin:$PATH' >> ~/.bashrc # set path to a default Go installation
# OPTIONAL
echo 'export GOPATH=$USERPROFILE/go' >> ~/.bashrc
-
SDL2
-
FFmpeg
Installation
go get github.com/boseca/go-sdl2-ffmpeg
Packaging
Copy all binaries necessary to run the applicaiton into the bundle
folder
ldd player.exe | python bundle.py
Test
-
Run tests for sfplay
package
go test -v github.com/boseca/go-sdl2-ffmpeg/sfplay
-
Build the test project and play a demo video
Usage
Any media file is composed of streams containing media data, e.g. audio, video and subtitles. The whole presentation data of the file is divided into packets. Each packet belongs to one of the streams and represents a single frame of its data. The process of decoding implies reading packets and decoding them into either video frames or audio frames.
The library provides read video frames as RGBA pictures. The audio samples are provided as raw byte slices in the format of AV_SAMPLE_FMT_DBL
(i.e. 8 bytes per sample for one channel, the data type is float64
). The channel layout is stereo (2 channels). The byte order is little-endian. The detailed scheme of the audio samples sequence is given below.
Once the frame is decoded the SDL texture is updated with the new frame pixels and SDL event is triggered to render the frame in the SDL window.
You are welcome to look at the examples to understand how to work with the library. Also please take a look at the detailed tutorial.