7

I would like to be able to play and save a video with mpv --ytdl. How do I got about doing that? What's particularly important is that the buffer gets saved too.

Basically, I would like to play a video from youtube, then quit mpv and still be able to keep watching the video up until the point it was loaded - that's why I also need the buffers to be saved to a file.

I have already tried using youtube-dl -o - to stream the video to stdout and then piping that with mpv, i.e.

youtube-dl -o - | mpv -

(with the idea that I could use tee to split the stream and simply write it to a file) - however, this has the problem that I can't use mpv to navigate through the video - it's just a fixed stream coming from stdin after all. Another idea I had was using mpv's -o option to specify an output file. That does not save the internal buffer, however.

1
  • If you seek outside the buffer while saving to file you will corrupt the output. You're best off telling youtube-dl to save to an mkv and then playing the mkv while the file downloads. You can seek within the downloaded space. Shows typically download several times real time. What you really want is a player that has all the features of the good old Miro. It let you play a file while downloading, seek around, and it would finish downloading the parts you skipped later. I'm sadly unaware of such a player today.
    – Wil
    Aug 7 '21 at 5:54
11

Another option, that is still highly experimental, is to use mpv’s own --record-file:

mpv --record-file=video.mkv https://www.youtube.com/watch?v=…

I have had limited success with it. As the source files youtube-dl is grabbing need to match up with the file extension you specify for the recording file. This seems like the closest to what is described in the question, however.

From the (current stable) manual:

--record-file=<file>

Record the current stream to the given target file. The target file will always be overwritten without asking.

This remuxes the source stream without reencoding, which makes this a highly fragile and experimental feature. It's entirely possible that this writes files which are broken, not standards compliant, not playable with all players (including mpv), or incomplete.

The target file format is determined by the file extension of the target filename. It is recommended to use the same target container as the source container if possible, and preferring Matroska as fallback.

Seeking during stream recording, or enabling/disabling stream recording during playback, can cut off data, or produce "holes" in the output file. These are technical restrictions. In particular, video data or subtitles which were read ahead can produce such holes, which might cause playback problems with various players (including mpv).

The behavior of this option might changed in the future, such as changing it to a template (similar to --screenshot-template), being renamed, removed, or anything else, until it is declared semi-stable.

8
youtube-dl -o - | tee video.mp4 | mpv -
7

--record-file was deprecated, in favor of --stream-record. both are not a perfect solution, because fast-forwarding beyond the cache will result in a skip in the output file.

from the mpv man page:

--record-file=<file>
       Deprecated, use --stream-record, or the dump-cache command.

       Record the current stream to the given target file. The target file will always be overwritten without asking.

       This was deprecated because it isn't very nice to use. For one, seeking while this is enabled will be directly reflected in  the
       output, which was not useful and annoying.
 --stream-record=<file>
        Write  received/read  data from the demuxer to the given output file.
        The output file will always be overwritten without asking.
        The output format is determined by the extension of the output file.

        Switching streams or seeking during recording might result in recording
        being stopped and/or broken files. Use with care.

        Seeking outside of the demuxer cache will result in "skips" in the output file,
        but seeking within  the demuxer cache should not affect  recording. 
        One exception is when you seek back far enough to exceed the forward buffering size,
        in which case the cache stops actively reading.
        This will return in dropped data if it's a live stream.

        If this is set at runtime, the old file is closed, and the new file is opened.
        Note that this will write only data that  is  appended at the end of the cache,
        and the already cached data cannot be written.
        You can try the dump-cache command as an alternative.

        External files (--audio-file etc.) are ignored by this,
        it works on the "main" file only. Using this with  files  using  ordered
        chapters or EDL files will also not work correctly in general.

        There  are  some  glitches with this because it uses FFmpeg's libavformat for writing the output file.
        For example, it's typical that it will only work if the output format is the same as the input format.
        This is the case even if it works with  the  ffmpeg tool.
        One reason for this is that ffmpeg and its libraries contain certain hacks and workarounds for these issues,
        that are unavailable to outside users.

        This replaces --record-file.
        It is similar to the ancient/removed --stream-capture/-capture options,
        and provides better  behavior in most cases (i.e. actually works).

usage: mpv --stream-record=$HOME/Downloads/path/name.mp4 <URL>

-2

youtube-dl url --exec mpv

This doesn't stream but plays after download, but you won't know the difference unless you have a dialup account or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.