1. Introduction

mpv is a free, open-source, and powerful cross-platform media player based on MPlayer and mplayer2. It supports a wide range of video, audio, and subtitle formats, ensuring compatibility with most media files. On top of its fast performance via hardware acceleration, the scripting capabilities of mpv enable us to customize almost anything.

In this tutorial, we’ll learn how to prevent mpv from closing once a video ends.

2. Environment Setup

Before moving forward, let’s ensure we have all the prerequisites ready, including mpv and a sample video.

2.1. mpv Installation

To begin with, we install mpv via a local package manager such as apt:

$ sudo apt install mpv -y

Thus, we install the mpv player.

Now, let’s verify the installation by running the command:

$ mpv --version
mpv 0.34.1 Copyright © 2000-2021 mpv/MPlayer/mplayer2 projects

The –version option shows the version of mpv currently installed on the system.

2.2. Sample Video

Next, let’s get a sample video file using the wget command:

$ wget -q -O test_video.mkv https://freetestdata.com/wp-content/uploads/2024/03/Free_Test_Data_2.38MB_MKV.mkv

The -q flag in the wget command suppresses the output, and the -O option specifies the output filename.

We can play the downloaded video file with mpv:

$ mpv test_video.mkv

This command plays the specified video with mpv and once the video ends mpv closes automatically. This is the default behavior of mpv.

We can change the default behavior by using command-line options or modifying the mpv configuration file.

3. Using Command-Line Options

mpv is highly customizable and enables us to control it in different ways, including through the command line.

3.1. –keep-open Flag

For instance, let’s check the –keep-open flag, which preserves the open state of mpv after all supplied media files are finished playing:

$ mpv --keep-open test_video.mkv

This command launches the mpv and plays the video from the test_video.mkv file. The –keep-open flag keeps the mpv window open even after the video ends displaying the last frame of the video.

3.2. –idle and –force-window Flags

The –idle flag tells mpv to be in the idle state and prevent automatic closing:

$ mpv --idle test_video.mkv

This command plays the video and keeps mpv in an idle state once the video ends while closing the video output window.

Additionally, we can also keep this output window open with the –force-window option:

$ mpv --idle --force-window test_video.mkv

This command launches the mpv, plays the video file, and stays in the idle state with the video output window open after the video ends.

All of the flags above can be used as options with the value yes to achieve the same mpv behavior.

4. Using the Configuration File

In addition to the command line options, we can also control the behavior of mpv by altering the mpv configuration file.

Typically, mpv creates configuration files at two different locations:

  • user-specific configuration file at ~/.config/mpv/mpv.conf
  • system-wide configuration file at /etc/mpv/mpv.conf or /usr/local/etc/mpv/mpv.conf

First, mpv reads settings from the system-wide configuration file. Then, it reads settings from the user-specific configuration file, which can override the system-wide settings. Finally, any options specified directly in the command line can override both the user-specific and system-wide settings.

4.1. Configure keep-open

Let’s add the keep-open option in the local configuration file:

$ cat ~/.config/mpv/mpv.conf
# Keep the MPV window open after playback

Now, we can check the behavior of mpv:

$ mpv test_video.mkv

This command launches the mpv, plays the video, and keeps the mpv open by displaying the last frame once the video ends, preventing it from closing. Additionally, We can achieve the same mpv behavior by using keep-open instead of keep-open=yes in the configuration file.

4.2. Configure idle and force-window

Of course, another way to preserve the player open after media files have finished playing is to use the idle option along with the force-window:

​$ cat ~/.config/mpv/mpv.conf
# Keeping the mpv in idle state
# Forcing mpv to open a window

This configuration file prevents the mpv from closing once the video ends.

We can add =yes as a suffix to either or both idle and force-window options in the above configuration file and still achieve the same mpv behavior.

5. Conclusion

In this article, we learned several ways to prevent mpv from closing once a video ends.

First, we installed and tested the default behavior of mpv. Then, we used different flags to prevent mpv from closing automatically after the video ends. While the –keep-open flag displays the last frame of a video, the –idle flag puts mpv in the idle state once the video ends. We used the –force-window flag in combination with the –idle flag that forces the mpv to keep open a video output window.

Finally, we tested these options in the configuration file and verified the change in the default mpv behavior that prevents it from closing once the video ends.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.