Controlling VLC player through bash script

Quick start with xdotool to play and pause videos in VLC media player. This enables a programmatic switching of play/ pause command without human intervention.

In this tutorial, will focus on how to control VLC videos using a bash script. This script can be used to play or pause the video being played in the VLC window. It can be integrated with a C++ program too. For this, we will write the following script :

#!/bin/bash
if [ $1 -eq 1 ]
then
    sleep 3
    xdotool key –clearmodifiers space
    echo “Video Paused !”
fi
Save this script in the system, and use it in a C++ program or wherever required.

This script will wait for the input parameter ‘$1’, as soon as it becomes equal to 1, the ‘if’ statement will activate and corresponding commands will be executed.

sleep 3 – this will pause the script for 3 seconds
xdotool key –clearmodifiers space – this will simulate a ‘spacebar’ action which will pause/play the video.
echo “Video Paused !” – this will print “Video Paused !” on the terminal.

In order to test the script, open the terminal, go to the folder where the above mentioned script is saved. Type the following commands :

cd “path_to_script”
    chmod +x ./scriptname.sh
    ./scriptname.sh 1

This will execute the script and simulate a ‘spacebar’ action on the current window after 3 seconds. Thus, after executing the script we need to point on the window on which we want to simulate the action, for e.g. VLC media player window in this case.

Hope you do good with the xdotool and bash scripting.