Skip to content

流媒体播放

本节介绍 mpv 的流媒体播放功能和协议支持。

支持的协议

mpv 支持多种流媒体协议:

HTTP/HTTPS

bash
# HTTP 流
mpv http://example.com/video.mp4

# HTTPS 流
mpv https://example.com/video.mp4

RTSP/RTMP

bash
# RTSP 流
mpv rtsp://example.com/stream

# RTMP 流
mpv rtmp://example.com/live/stream

HLS (HTTP Live Streaming)

bash
# HLS 流
mpv https://example.com/stream.m3u8

# 带选项的 HLS
mpv --hls-segment-threads=4 https://example.com/stream.m3u8

DASH (Dynamic Adaptive Streaming over HTTP)

bash
# DASH 流
mpv https://example.com/manifest.mpd

YouTube

bash
# YouTube 视频
mpv https://www.youtube.com/watch?v=VIDEO_ID

# YouTube 播放列表
mpv https://www.youtube.com/playlist?list=PLAYLIST_ID

# YouTube 直播
mpv https://www.youtube.com/watch?v=LIVE_VIDEO_ID

其他平台

bash
# Bilibili
mpv https://www.bilibili.com/video/BV1xx411c7mD

# Twitch
mpv https://www.twitch.tv/username

# Vimeo
mpv https://vimeo.com/123456789

流媒体配置

网络缓冲

bash
# 缓冲大小(字节)
demuxer-max-bytes=500MiB

# 缓冲时长(秒)
demuxer-max-back-buffer=500MiB

# 预读取
cache=yes

连接设置

bash
# 连接超时(秒)
network-timeout=60

# 重试次数
stream-lavf-o=timeout=60

# 用户代理
user-agent="Mozilla/5.0"

代理设置

bash
# HTTP 代理
http-proxy=http://proxy:8080

# SOCKS 代理
socks-proxy=socks5://proxy:1080

YouTube 支持

YouTube 选项

bash
# 选择视频质量
ytdl-format=bestvideo+bestaudio

# 选择特定质量
ytdl-format=bestvideo[height<=720]+bestaudio

# 禁用 YouTube-DL
ytdl=no

YouTube-DL 配置

bash
# 使用 youtube-dl
script-opts=ytdl_hook-ytdl_path=youtube-dl

# 使用 yt-dlp
script-opts=ytdl_hook-ytdl_path=yt-dlp

# 自定义选项
ytdl-raw-options=cookies-from-browser=chrome

YouTube 质量选择

bash
# 最佳质量
ytdl-format=best

# 最佳视频 + 最佳音频
ytdl-format=bestvideo+bestaudio

# 720p 视频
ytdl-format=bestvideo[height<=720]+bestaudio

# 1080p 视频
ytdl-format=bestvideo[height<=1080]+bestaudio

直播流

直播配置

bash
# 直播缓冲
cache=yes
demuxer-max-bytes=500MiB

# 直播延迟
network-timeout=60

# 直播同步
video-sync=audio

直播平台

bash
# Twitch
mpv https://www.twitch.tv/username

# YouTube 直播
mpv https://www.youtube.com/watch?v=LIVE_VIDEO_ID

# Bilibili 直播
mpv https://live.bilibili.com/ROOM_ID

流媒体缓存

缓存配置

bash
# 启用缓存
cache=yes

# 缓存大小
demuxer-max-bytes=150MiB

# 缓存目录
cache-dir=~/.cache/mpv

# 缓存文件
cache-file=TMP

缓存策略

bash
# 预读取
demuxer-readahead-secs=20

# 缓存清理
cache-unlink-files=immediate

流媒体协议选项

HTTP 选项

bash
# HTTP 超时
network-timeout=60

# HTTP 头
http-header-fields="Referer: https://example.com"

# HTTP Cookies
cookies=yes
cookies-file=~/.config/mpv/cookies.txt

RTSP 选项

bash
# RTSP 传输
rtsp-transport=tcp
rtsp-transport=udp

# RTSP 端口
rtsp-port=554

HLS 选项

bash
# HLS 线程数
hls-segment-threads=4

# HLS 优先级
hls-bitrate=max

流媒体故障排除

常见问题

  1. 缓冲区溢出

    bash
    # 增加缓冲区大小
    demuxer-max-bytes=500MiB
  2. 连接超时

    bash
    # 增加超时时间
    network-timeout=120
  3. 视频卡顿

    bash
    # 降低质量
    ytdl-format=bestvideo[height<=720]+bestaudio

调试信息

bash
# 显示网络信息
mpv --msg-level=all=v https://example.com/video.mp4

# 显示缓冲信息
mpv --stats https://example.com/video.mp4

下一步