1
SFML projects / Re: [Development] [Video Playback] SFVLC
« on: August 02, 2021, 01:32:49 pm »
Here is a complete example on how to play a video file with SFML. Using the cpp-binding of libvlc:
https://github.com/videolan/libvlcpp
You have to make sure that the vlcpp folder is available in your project, and also the dynamic library libvlc has to be available on your system. On linux you can just install it by apt install. On Mac and Windows it's a little more effort...
https://github.com/videolan/libvlcpp
You have to make sure that the vlcpp folder is available in your project, and also the dynamic library libvlc has to be available on your system. On linux you can just install it by apt install. On Mac and Windows it's a little more effort...
Code: [Select]
#include <SFML/Graphics.hpp>
#include "vlcpp/vlc.hpp"
int main()
{
int width = 1000;
int height = 500;
sf::RenderWindow window(sf::VideoMode(width, height), "SFML works!");
window.setFramerateLimit(24); // framerate of videofile
// VLC
VLC::Instance vlc = VLC::Instance(0, nullptr);
VLC::Media media = VLC::Media(vlc, "path/to/video.mp4", VLC::Media::FromPath);
VLC::MediaPlayer mp = VLC::MediaPlayer(media);
// Allocate SFML texture
sf::Texture m_video_texture = sf::Texture();
m_video_texture.setSmooth(true);
m_video_texture.create(width, height);
sf::Sprite m_video_sprite = sf::Sprite();
m_video_sprite.setTexture(m_video_texture);
// framebuffer. The mediaplayer will render to this array of pixels
sf::Uint8* imgBuffer = new sf::Uint8[width * height * 4];
// VLCs video Callbacks for writing to the texture
mp.setVideoCallbacks([imgBuffer](void** pBuffer) -> void* {
// Mutex Lock
*pBuffer = imgBuffer;
return NULL;
}, [](void*, void*const*) {
// Mutex Unlock
}, nullptr);
mp.setVideoFormat("RGBA", width, height, width * 4);
// Play mediafile to continuously update imgBuffer
mp.play();
// Main SFML Window routine
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
// Render video to sprite
m_video_texture.update(imgBuffer);
window.draw(m_video_sprite);
window.display();
}
// Remove garbage
mp.stop();
delete imgBuffer;
return 0;
}