播放时将 mpv --ytdl 中播放的视频保存到文件中

播放时将 mpv --ytdl 中播放的视频保存到文件中

我希望能够玩使用 mpv --ytdl 保存视频。我该怎么做?特别重要的是缓冲区也会被保存。

基本上,我想播放来自 youtube 的视频,然后退出 mpv 并仍然能够继续观看视频直到加载完成 - 这就是为什么我还需要将缓冲区保存到文件中。

我已经尝试使用 youtube-dl -o - 将视频流式传输到 stdout,然后使用 mpv 进行管道传输,即

youtube-dl -o - | mpv -

(我可以使用 tee 来分割流并简单地将其写入文件)——但是,这存在一个问题,我无法使用 mpv 浏览视频——毕竟它只是来自 stdin 的固定流。我的另一个想法是使用 mpv 的 -o 选项来指定输出文件。但是,这不会保存内部缓冲区。

答案1

另一个选项(仍处于高度实验阶段)是使用 mpv 自己的--record-file

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

我对此的成功率有限。因为 youtube-dl 抓取的源文件需要与您为录制文件指定的文件扩展名相匹配。然而,这似乎最接近问题中描述的内容。

(当前稳定的)手册

--record-file=<file>

将当前流记录到给定的目标文件。目标文件将始终被覆盖,而无需询问。

这会重新混合源流而不重新编码,因此这是一个非常脆弱且处于实验阶段的功能。这完全有可能写入损坏的文件、不符合标准的文件、无法在所有播放器(包括 mpv)上播放的文件或不完整的文件。

目标文件格式由目标文件名的文件扩展名决定。建议尽可能使用与源容器相同的目标容器,并以 Matroska 作为后备。

在流录制期间进行定位,或在播放期间启用/禁用流录制,可能会切断数据或在输出文件中产生“空洞”。这是技术限制。特别是,提前读取的视频数据或字幕可能会产生这样的空洞,这可能会导致各种播放器(包括 mpv)出现播放问题。

此选项的行为将来可能会发生变化,例如将其更改为模板(类似于--screenshot-template),重命名,删除或进行其他任何操作,直到被宣布为半稳定。

答案2

--record-file已被弃用,取而代之的是--stream-record。两者都不是完美的解决方案,因为超出缓存的快进将导致输出文件跳过。

来自 mpv 手册页:

 --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).

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

答案3

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

答案4

youtube-dl url --exec mpv

这不能流式传输但可以在下载后播放,但除非您有拨号帐户或类似的东西,否则您不会知道区别。

相关内容