Search code examples
javaswingrotationvlcj

How to rotate a video placed on a JFrame (VLCJ)?


I am wondering if it is possible to rotate 90 degrees a video played with VLCJ. Part of the code used for displaying the video is the following:

        mediaPlayerFactory = new MediaPlayerFactory();     

        mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();

        frame.setResizable(false);  
        frame.setUndecorated(true);

        Canvas c = new Canvas();
        c.setBackground(Color.black);
        final JPanel p = new JPanel();

        p.setLayout(new BorderLayout());

        p.add(c, BorderLayout.CENTER);
        frame.add(p, BorderLayout.CENTER);

        mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));

        frame.setLocation(650, 200);
        frame.setSize(1050, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);             

        mediaPlayer.playMedia(file);
        mediaPlayer.mute(false);

The code works nicely, the video can be watched without problems, but I would like to perform a rotation on it. I have looked up on the Internet but most of the posts are about rotation of images... so anyone can help me with that? Thank you!


Solution

  • When you create the MediaPlayerFactory, make sure to specify the video filter and options you want as factory arguments, for example:

    String[] args = {
        "--video-filter", 
        "rotate",
        "rotate-angle",
        "10"
    };
    
    mediaPlayerFactory = new MediaPlayerFactory(args);
    

    I don't think there's any way to set this dynamically while the video is playing.

    The available filters come from:

    $vlc --list
    

    The available options come from:

    $vlc -H
    

    Alternatively, you could use a DirectMediaPlayer where you render the video yourself into a Graphics2D or OpenGL or whatever context and apply whatever rotation/transformation you want.