MPV: Limit cache size and cacheable playback interval

By default, MPV has too high values for cache usage which can cause a large consumption of memory when playing a video, for example from YouTube. This note is about how to limit MPV cache usage and reduce memory consumption.

Limit cacheable playback interval

The first option is –cache-secs <seconds>. This sets how many seconds to be stored in the cache. Simple use:

mpv --cache-secs 360 'link to youtube video'

In this case in the cache will be stored no more than 6 minutes of a video.

Limit cache size

We can limit the cache size in bytes. I tried various ‘–cache‘ options, but it doesn’t work for me. It seems that ‘cache’ and ‘demuxer’ are work separately. Demuxer has own buffer options, therefore we can use only it. Need to disable ‘cache’, otherwise its value will be added together (demuxer buffer + cache).

Current buffer

In the following example, CURRENT cache size will not be above 20 megabytes:

mpv --demuxer-max-bytes=20M --no-cache 'link to youtube video'

‘M’ literally means ‘megabytes’. Use ‘K’,’G’ for kilobytes and gigabytes values respectively.

Disable previous buffer

But the example above is not perfect. There is also ‘back buffer’ which stores our previous video caches when we seek video forward and backward. When we return to the previous video fragments, they are taken from the cache again.

There is a way to disable ‘back buffer’ by using –demuxer-max-back-bytes option:

mpv --demuxer-max-bytes=20M --demuxer-max-back-bytes=0 --no-cache 'link to youtube video'

Now MPV cache will store exactly 20 megabytes.

The best way

If we allow the possibility of returning to the previous video fragment, it is better to have some ‘back buffer’ size:

mpv --demuxer-max-bytes=20M --demuxer-max-back-bytes=10M --no-cache 'link to youtube video'

Total video cache will not be more than 30 megabytes.

Other demuxer and cache options

Other ‘cache’ options can be listed by the following command:

mpv --list-options | grep cache
--cache                          Choices: no auto yes (or an integer) (32 to 2147483647) (default: auto)
--cache-backbuffer               Integer (0 to 2147483647) (default: 10000)
--cache-default                  Choices: no (or an integer) (32 to 2147483647) (default: 10000)
--cache-secs                     Double (0 to any) (default: 36000.000)
---And More

All demuxer options:

mpv --list-options | grep demuxer
--demuxer-max-back-bytes         ByteSize (0 to 2147483647) (default: 50.000 MiB)
--demuxer-max-bytes              ByteSize (0 to 2147483647) (default: 150.000 MiB)
--demuxer-readahead-secs         Double (0 to any) (default: 1.000)
--demuxer-seekable-cache         Choices: auto no yes (default: auto)
---And More

Additional information on MPV official webpage:

Demuxer options

Cache options