 Hello! Today we're going to be playing with MPV. Now, MPV is my favorite media player for playing both audio and video files. And it is great, easy to use, and it runs on just about everything. Can we control it remotely or with buttons? For example, on your keyboard, you might have media buttons. Two skips in the next song, previous song, play and pause. Your headphones might have buttons on the side for those type of controls. Can we control MPV with this? Yes, we can. If you watched one of my previous videos, I gave a quick overview on how to control MPV using shell commands. Once you know how to do something in the shell, then you can link it to anything, either a GUI interface or remotely through a network. And we're going to do both today. We're going to learn how to set up media keys to control MPV. But also, I'm going to show you how to make a simple interface to wear on your mobile device or any remote computer. You can have a nice GUI interface with buttons that can control a remote computer playing files with MPV. So let's go ahead, we're going to jump right in and let's see how this works. Okay, first things first, check out the link in the description. We'll bring you to this GitLab project. In this GitLab project, I just have a bunch of examples on how to do this. You're going to need two programs installed for this first thing. We're going to need, obviously, MPV and another program called SoCat. Both should be in your repositories for pretty much any Linux distribution. Go to this project and download it. In the main directory here, there is a quick notes text file. Let's go ahead and click on that and it gives basic examples. As I talked about in a previous video, you would start MPV. And in this case, I'm saying no Vid because in this example, I'm going to be playing audio files. So MP3s in this case are really any files that are media files. What this does is it says no video. Well, they're music files, but some files like especially MP3s may have art album embedded in them, in which case MP3 will display those on the screen. I don't want to, I just want the music playing in the background. So we're saying no Vid. Now, here's the magic command right here, dash dash input dash IPC dash server, we're going to run that and give it a file. This can be any file as long as you have rewrite access to it and it doesn't exist. MP3, MPV will create that file and then you can pipe commands into it using SoCat. MPV will issue those commands. Those commands can be commands such as cycle pause play. This will pause or unpause the song or video. If you're doing a playlist, you can jump ahead or back with previous or next. So playlist previous or next. You can also seek meaning jumping ahead in your current video or song, however many seconds you want. In this case, we're going five seconds forward or five seconds back. You can also send commands such as key presses. So let's say you use MPV and you know when you're at the shell, certain keys like I think it's J will turn subtitles or go through your different subtitles or zero will increase the volume of just MPV instead of your whole system and nine will decrease it. Well, if you know what keys are, you can say press key and give it whatever key that is. For example, lowercase O is an on-screen display. So we can echo this into SoCat, which will then redirect it into our MPV socket file and control MPV. Also note that this can be done since MPV, if you have YouTube DL or something similar installed, if you have a YouTube playlist going, you can control it as well. Just also realize that if you're playing a playlist on YouTube and you try to jump ahead a song, it might take a couple of seconds for that to load. Also note that if you already have MPV running with this command, you're going to want to kill any MPV processes that are using that before you start this. Otherwise, it will go, oh, MPV, there's something already using this file. We can't use it. So let's go ahead and look at that. But now that we know how to do this, we can also link that to buttons on our keyboard. So going into the shell, I'm using i3 window manager. So different window managers, you might link buttons different ways. But for me, I set up so that my media button for play, pause and stop. I want them all to act the same. They're going to cycle pause. So for my i3 configurations, I'm saying bind this key press, this media key, to execute echo cycle pause into Socat and into that file. So as long as I've started MPV already, with that dash dash input command, this will control the media. And then I can also say previous and next for our previous audio button and previous next button. Again, those are controls on my keyboard. You may have other media keys as well. And of course, you can link any key combination to do these. So if you don't have media keys, you can create other shortcut keys using whatever window manager desktop environment you use. So let's go ahead. And what I'm going to do here is I'm going to take this command. I'm going to say, MPV, no video, I'm using this input IPC server temp socket. So this is the file we're creating, as long as it doesn't already exist. And I'm just going to say all files in my music directory. And for me, that's my home directory music all lowercase. Lots of distributions have music with an uppercase m, but you can point to wherever. And it's going to start playing all the files in that folder, switch that over to my headphones so that I don't get any copyright strikes. But at this point, on my keyboard, I can press my forward button, and it jumps to the next song in the playlist. I can do that again. I can also go back or I'm sorry, hit the pause button, the play pause button. So I paused it. I unpaused it. And I can go back again, if you have a headphones with media controls, this should work the same way. Once you've linked it to those media buttons, because the media buttons all send the same signal, or at least in theory should. And then I'll just control C to kill that. So that is how you would link media buttons. Of course, if using a different desktop environment or window manager, you're going to set the keys, however you would set it. But again, looking at the notes here, these are the commands you would want to set for controlling those in whatever desktop environment you're using. Now, we're going to take it up a notch. So that's how you set up media keys on your keyboard, headphones, or just create shortcut keys on your keyboard. Now we're going to create a remote server. So we're going to need one other program installed. And that program is Busybox. Good chance you have it installed. If not, it's definitely in the repositories for your distribution. I've talked about Busybox many, many times. Busybox is a single binary file that has a load of basic core programs in there. And fully loaded, it's under one megabyte. If you have any type of device, a router, a phone, your TV that's running Linux in the background, all these things probably have Busybox or a not as good comparison, but toy box, which is a similar program, but not as powerful. And one of the things that Busybox has in it is an HTTP server. So I have two scripts here, one for music server and one for video server. And when I go in here, what I'm doing here is I'm running Busybox with its web server, HTTPD. And then I'm giving it a port. I just pick port 9876. It's just a port that's not being used on my computer. Next, I'm also making sure that MPV, if it's already running, is killed and then piping any errors to DevNol. So we don't see any errors there. And then we will start MPV, no vid, and then IPC, input IPC server, give it that file. And in my case, you'll want to change this to wherever your music directory is. And it's saying play all those files. You can also set it to play them all in a random order too, every time you start it. And then at this point, when you get to the end of the playlist, or you kill MPV, the command is going to continue in this kind of a sloppy way, but it's making sure that the Busybox server is also killed. So I've downloaded that. Let me go to my shell here. And I'm in that project. So I'm going to move into the server folder. And I again list out. So it's the same thing. So I'm going to run my MPV server for music. And now it's running that, but it's not just running MPV. It's also starting up the HTTP server. And in the current directory, if we go out, we can see that there's an index HTML, very, very short and simple with a couple of buttons, we'll look at it in a moment. And then I have some JavaScript that basically when you click one of these buttons, we'll call a shell that is inside the CGI bin, whether it's next. So you can see here, it's just giving a little command for your web browser to know that this is a plain text file. And then it's going to run our echo command into Socat for the next file on the playlist. We have the same thing for play pause previous. And then we also have seek forward and back. And there I also added not only are we seeking back, but also whenever I do those, I like to have it for a few seconds display the on screen display. So we're running that now, I can go to my local host on this machine, or if it was a remote machine, for example, my cell phone, I can then just type in the IP address and the port number. And now that the server's running, I can refresh this. This is what my interface looks like. So let me go ahead and bring this over so we can see the shell since we have the audio playing here, I can click next. And it's going to next song next previous pause play pause seek forward play seek back. And in this particular case, we're not getting on a on screen display, but if we were to run the video server, so I will control C to kill this. Now I will run a command to start the server for instead of music video. Let's look at that real quick before I run it. We'll go back here and you'll see back again, our video server, same thing only this time. It's not saying no video, but I'm going full screen. And it's going to play all files of my videos fuller, which I just downloaded a couple of my YouTube videos. So we'll go ahead, we will run that starts playing the video full screen. Let me take it out of full screen. But now I can go to my web interface here, and I can seek forward and you can see it gives an on screen display for a few seconds. So I know where I am in the video, I can go back, I can pause it, I can play it, I can go to the next video, and I'll start playing that. And I can go to a previous video, or the next video, I can pause, I can play, I can go next, I can go back. So now I'm doing this locally, but I can also do this remotely with any device on my network. Again, it's not encrypted. I didn't set up any password. You could do that. You could set up encryption keys and passwords, but I'm just controlling the media on my local network and my network secure. So you do you and set up how you want, but you can always make this more secure by adding encryption keys and passwords to your busybox command here. But that's basically how it works. So check out the links in the description that will bring you to this GitHub page. You can get lab page. You can download it. Again, the quick notes are there, but then you can also just go into the server and really you just have to make sure you have busybox installed, MPV, and Socat. And then at that point, just change these directories to your music directory or your video directory. And again, you can also set it to shuffle and play them randomly. But these are basic scripts to get you going. And yeah, I hope that you enjoyed this. Please visit FilmsByChris.com. That's Chris of the K. There's a link in the description. And as always, I hope that you have a great day. Be sure to like, share, subscribe, and comment. Real quick, I thought I'd give a better demonstration of using it remotely. So I have the video server running here and I've connected to it just by putting in my IP address and the port number in this case, 9876. And I have this very simple web interface. It's just a couple of buttons. And again, now I can press things like seek forward. And the video is now seeking forward back here. I can press pause, play, next and go to the next video. I can seek forward on that. I can seek back on that. So again, you don't even need to install anything. But also since we're doing just basic HTTP requests, any programming language you can create an interface for you can create an interface for that interacts with this. So for example, I could take a Godot game and I can make buttons and control my media and then have an application on my phone or any device that controls this. So I just wanted to give a little better demonstration of how that works. This is what the interface looks like in the basic project that I have up on GitLab. Very, very basic. Of course, you can make it look a lot nicer. And that's it. Again, thanks for watching. I hope that you have a great day.