diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst
index fe3421288c..58b5321b07 100644
--- a/DOCS/man/mpv.rst
+++ b/DOCS/man/mpv.rst
@@ -1492,6 +1492,10 @@ PROTOCOLS
This will play ``video.mkv`` in the archive file ``file.zip``.
+``env://variable``
+
+ Read the environment variable ``variable`` as source data.
+
PSEUDO GUI MODE
===============
diff --git a/meson.build b/meson.build
index b848cb4985..a0c8744adc 100644
--- a/meson.build
+++ b/meson.build
@@ -200,6 +200,7 @@ sources = files(
'stream/stream_null.c',
'stream/stream_slice.c',
'stream/stream_bstr.c',
+ 'stream/stream_env.c',
## Subtitles
'sub/ass_mp.c',
diff --git a/stream/stream.c b/stream/stream.c
index 53fb3aafff..5314eb3c93 100644
--- a/stream/stream.c
+++ b/stream/stream.c
@@ -62,6 +62,7 @@ extern const stream_info_t stream_info_edl;
extern const stream_info_t stream_info_libarchive;
extern const stream_info_t stream_info_cb;
extern const stream_info_t stream_info_curl;
+extern const stream_info_t stream_info_env;
static const stream_info_t *const stream_list[] = {
&stream_info_mpv,
@@ -96,6 +97,7 @@ static const stream_info_t *const stream_list[] = {
#endif
&stream_info_ffmpeg,
&stream_info_ffmpeg_unsafe,
+ &stream_info_env,
};
// Because of guarantees documented on STREAM_BUFFER_SIZE.
diff --git a/stream/stream_env.c b/stream/stream_env.c
new file mode 100644
index 0000000000..b833c43584
--- /dev/null
+++ b/stream/stream_env.c
@@ -0,0 +1,47 @@
+/*
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * mpv is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with mpv. If not, see .
+ */
+
+#include
+
+#include "common/common.h"
+#include "stream_bstr.h"
+
+static int open2(stream_t *stream, const struct stream_open_args *args)
+{
+ bstr data = bstr0(stream->url);
+ bstr_eatstart0(&data, "env://");
+
+ char *name = bstrdup0(stream, data);
+ char *val = getenv(name);
+ talloc_free(name);
+
+ if (!val) {
+ MP_FATAL(stream, "Could not read from environment variable '%.*s'\n",
+ BSTR_P(data));
+ return STREAM_ERROR;
+ }
+
+ data = bstrdup(stream, bstr0(val));
+ return stream_bstr_open2(stream, args, data);
+}
+
+const stream_info_t stream_info_env = {
+ .name = "env",
+ .open2 = open2,
+ .protocols = (const char*const[]){ "env", NULL },
+ .stream_origin = STREAM_ORIGIN_DIRECT,
+};