Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Development] [Video Playback] SFVLC  (Read 8174 times)

0 Members and 1 Guest are viewing this topic.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
[Development] [Video Playback] SFVLC
« on: November 10, 2016, 01:28:21 am »
Since i need to play videos with SFML and i found this comment of Ceylo (the creator of sfeMovie)

Quote
Indeed there is the feature/OpenFromStream branch. However it is not finished. It is roughly what a user from SFML forum proposed, but I don't wish to work on sfeMovie anymore so it'll have to be finished by someone else. Basically what lacks at the moment is cleanly design integration and manual tests.

And beacuse i don't want use death projects i decide to write my own video player an take advantage of libVLC, that its core is LPGL-licensed and most of its plug-ins too.
The libVLC plug-in systems works like add-ons, you carry the dlls you want without have to recompile,
so we can have both commercial and non-commercial projects.

I need to polish the final details, that includes a loadFromStream feature, then i will release the source code on github, share the MSVC 2013 libraries, and hope someone help me to create a CMAKE build system.

The players deliver a high-performance, so you won't never be worried about it.

This is an simple example showing few features:



#include <iostream>
#include <cstdio>

#include <SFML/Graphics.hpp>
#include "sf_vlc_video.hpp"

int main()
{  
   
        sf::VideoMode fullScreenMode = sf::VideoMode::getDesktopMode() ;
        sf::VideoMode windowedMode = sf::VideoMode( 800 , 600 ) ;

        sf::RenderWindow window( windowedMode , "Video" ) ;
                         window.setFramerateLimit( 60 ) ;
   
    freopen( "null" , "w+" , stderr ) ; // Disable error/warning messaging. There are no way to fix|disable them.
       
        SFVLC::Video video( { windowedMode.width , windowedMode.height } ) ;

    video.loadFromURL( "http://video.webmfiles.org/big-buck-bunny_trailer.webm" ) ;
    // Or: video.loadFromFile( "myvideo.ogv" ) ;

        video.play() ;

        bool isFullscreen = false ;

        sf::Clock deltaClock ;

    while ( window.isOpen() )
    {  
        sf::Event event;
        while ( window.pollEvent( event ) )
        {
                        switch( event.type )
                        {
                                case sf::Event::Closed :
                                     video.stop() ;
                                         window.close() ;
                                         break ;

                                case sf::Event::KeyPressed :
                                {  
                                        switch( event.key.code )
                                        {
                                            case sf::Keyboard::Escape : // Exit.
                                                     video.stop() ;
                                                     window.close() ;
                                                         break ;
                                                case sf::Keyboard::Space : // Pause | Resume video with the space bar.
                                                {
                                                        if( video.getStatus() == SFVLC::Video::Status::playing )
                                                                video.pause() ;
                                                        else
                                                                video.play() ;
                                                        break ;
                                                }
                                                case sf::Keyboard::F : // Resize video to fullscreen or windowed mode.
                                                {  
                                                   if( !( isFullscreen = !isFullscreen ) )
                                                   {  
                                                       window.create( windowedMode , "Video" ) ;
                                                           window.setFramerateLimit( 60 ) ;
                                                           video.setSize( { windowedMode.width , windowedMode.height } ) ;
                                                   }
                                                   else
                                                   {  
                                                       window.create( fullScreenMode , "Video" sf::Style::Fullscreen ) ;
                                                           window.setFramerateLimit( 60 ) ;
                                                           video.setSize( { fullScreenMode.width , fullScreenMode.height } ) ;
                                                   }
                                                   break ;
                                                }
                                                case sf::Keyboard::BackSpace : // Restart the video.
                                                         video.restart() ;
                                                         break ;          
                                   }
                                }
            }
        }

                const float delta = deltaClock.restart().asSeconds() ;
               
                // Inherits from sf::Transformable.
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) ) { video.move( delta * 10 , 0 ) ; }
                else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) ) { video.move( - delta * 10 , 0 ) ; }

                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) ) { video.move( 0 , - delta * 10 ) ; }
                else if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) ) { video.move( 0 , delta * 10 ) ; }

        window.clear();
                window.draw( video ) ; // It's a sf::Drawable too.
        window.display();
    }

    return 0;
}

 


Hope in this (or the next) week i can release the code on GitHub with a good documentation and test-suite.

Features actually implemented:
-play for local file
-play for url
-get video information (only for local file)
-pause/play/stop/restart
-resize (native way)
-change video playing time.
-set audio channel.
-decrease/increase volume
-set aspect ratio.
-control update rate.
« Last Edit: November 10, 2016, 01:34:27 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: [Development] [Video Playback] SFVLC
« Reply #1 on: November 16, 2016, 01:01:16 pm »
Seems simple enough to use and distribute, I like it.

migoun

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: [Development] [Video Playback] SFVLC
« Reply #2 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...

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;
}

 

anything