Empowering Your Media Experience: MPV Control with Command Line and socat
Category: Linux
Date: 9 months ago
Views: 523
Introduction:
In this article, I'm going to show you how to take command of MPV, a powerful media player, directly from the comfort of your command line using socat. Together, we'll unravel the intricacies of socat's capabilities, enabling us to interact with MPV seamlessly and efficiently, all with just a few keystrokes.
MPV and socat
Let's delve into the fundamentals of MPV and socat to grasp their roles in command line media control.
MPV stands out as a versatile media player renowned for its wide range of features and robust performance. It supports various audio and video formats, making it a popular choice among media enthusiasts.
On the other hand, socat serves as a powerful utility for establishing bidirectional data transfer between two independent processes. Its flexibility and efficiency make it an ideal tool for facilitating communication between MPV and the command line interface.
Setting up MPV and socat
Let's walk through the steps to configure MPV and socat for seamless command line control.
To begin, ensure that MPV and socat are installed on your system. You can easily install them using your package manager on Linux distributions.
Once installed, you have two options to configure MPV for remote control:
Pass the
--input-ipc-server=/path/to/your/socketfile
option when starting MPV.Alternatively, you can directly modify the MPV configuration file "
~/.config/mpv/mpv.conf
" like this:
# add the following line to ~/.config/mpv/mpv.conf
input-ipc-server =/tmp/mpvsocket
Bash functions for controlling MPV
Let's dive into the bash functions designed to streamline MPV control directly from the command line.
Function: mpvVolume()
# $1 +<number> or -<number>
function mpvVolume(){
echo "add volume $1" | socat - "$mpvsocketfile"
data=$(
echo '{ "command": ["get_property", "volume"] }' \
| socat - "$mpvsocketfile" \
| jq '.data'
)
msg="${data%.*}"
}
This function adjusts the volume of MPV by a specified increment or decrement.
Usage: mpvVolume +<number>
or mpvVolume -<number>
The function sends the volume adjustment command to MPV via socat, followed by querying the current volume level. The jq command is used to filter the retrieved data. If you want to get to know how to use it check this article : jq tutorial : How To Get COVID stats and data from your terminal
The message retrieved is utilized for notifications at the end of my shell script.
Function: mpvPause()
function mpvPause(){
echo 'cycle pause' | socat - "$mpvsocketfile"
data=$(
echo '{ "command": ["get_property", "pause"] }' \
| socat - "$mpvsocketfile" \
| jq '.data'
)
if [[ "$data" == "true" ]]
then msg=pause
else msg=play
fi
}
This function toggles the pause state of MPV.
Usage: mpvPause
The function sends the 'cycle pause' command to MPV via socat to toggle the pause state. It then retrieves the current pause state using jq to filter the data.
If the pause state is true, indicating that MPV is paused, the message is set to 'pause'; otherwise, it is set to 'play'.
Functions: mpvNext(), mpvPrev()
function mpvNext(){
echo 'playlist-next' | socat - "$mpvsocketfile"
}
function mpvPrev(){
echo 'playlist-prev' | socat - "$mpvsocketfile"
}
The mpvNext()
function instructs MPV to play the next item in the playlist.
This function sends the 'playlist-next' command to MPV via socat, prompting it to play the next item in the playlist.
The function mpvPrev()
instructs MPV to play the previous item in the playlist.
This function sends the 'playlist-prev' command to MPV via socat, prompting it to play the previous item in the playlist.
Other functions
# Function to retrieve the total number of items in the MPV playlist
function mpvPlaylistCount(){
cmd='{ "command": ["get_property", "playlist-count"] }'
echo "$cmd" \
| socat - "$mpvsocketfile" 2>/dev/null \
| jq '.data'
}
# Function to retrieve the current position in the MPV playlist
function mpvPlaylistPos(){
cmd='{ "command": ["get_property", "playlist-pos"] }'
echo "$cmd" \
| socat - "$mpvsocketfile" 2>/dev/null \
| jq '.data'
}
# Function to set the current position in the MPV playlist to the specified number
function mpvSetPlaylistPos(){
number=$1
cmd='{ "command": ["set_property", "playlist-pos", "'
cmd+="$number"
cmd+='" ] }'
echo "$cmd" \
| socat - "$mpvsocketfile" 2>/dev/null \
| jq '.data'
}
These additional functions provide essential functionality for managing MPV playlists with ease. The `mpvPlaylistCount()
` function retrieves the total number of items in the playlist, allowing users to ascertain the size of their playlist dynamically. The `mpvPlaylistPos()
` function retrieves the current position in the playlist, enabling users to track their progress or determine which item is currently playing. Finally, the `mpvSetPlaylistPos()`
function sets the current position in the playlist to the specified number, facilitating seamless navigation within the playlist. Together, these functions enhance the control and management of MPV playlists, providing users with a convenient way to interact with their media collections.
Adding notifications
Additionally, you can enhance your MPV control setup by incorporating notifications using the media title. By appending the media title to the message, retrieved using the 'get_property', 'media-title'
command, you can provide informative notifications while utilizing the MPV control functions. These notifications can be displayed using tools like dunstify, ensuring that you stay informed about the currently playing media and control actions.
msg+=" | "
msg+=$(
echo '{ "command": ["get_property", "media-title"] }' |
socat - "$mpvsocketfile" |
jq '.data'
)
dunstify -a "media-controls" -u low -r "$msgId" "mpv: $msg"
echo "$msg"
Practical examples
Let me show you how I use these functions in my workflow
For instance, in my i3 window manager configuration file, I've integrated MPV control into various keybindings. This setup allows me to effortlessly control MPV even when its window is in another workspace. For example, Let's say I'm writing something in workspace 3 and mpv is playing in workspace 5, No need to navigate there, just by typing "Alt+Space" to pause MPV and "Alt+V" to increase the volume, I don't have to disrupt my workflow.
Conclusion
As we conclude our exploration, it's evident that controlling MPV from the command line using socat opens up a world of possibilities for seamless media management.
With the power of bash functions and socat, you can effortlessly interact with MPV, whether it's adjusting volume, toggling playback, or navigating playlists.
By incorporating these techniques into your workflow, you can streamline your media experience and enhance productivity, all while enjoying the flexibility and customization offered by command line control.
References
For more information and official documentation on the tools and concepts discussed in this article, refer to the following resources:
MPV Documentation
Official MPV documentation provides comprehensive guidance on installation, configuration, and usage of the MPV media player.
https://mpv.io/manual/
socat Documentation
The official socat documentation offers detailed information on the usage and features of socat, a versatile tool for data transfer between processes.
http://www.dest-unreach.org/socat/doc/socat.html
dunstify Documentation
Explore the official dunstify documentation to learn more about creating desktop notifications from the command line using dunstify.
https://github.com/dunst-project/dunst
0 Comments, latest
No comments.