mpv

出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯,請訪問簡體中文翻譯團隊

附註: An update to the outdated translation from 2016 has started 2021-01-16 but has seen no progress since.(在 Talk:Mpv# 中討論)

mpv 是一個基於 MPlayer 和 MPlayer2 的多媒體播放器。 它支持多種視頻文件格式、音頻和視頻編解碼器和字幕類型。 在這裡可以找到mpv播放器與上述播放器之間的區別的綜合列表(可能不是完全統計)。

安裝[編輯 | 編輯原始碼]

安裝 official repositories裡的軟件包mpv or Arch User Repository 裡的 mpv-gitAUR

前端[編輯 | 編輯原始碼]

mpv 提供了一個叫做OSC的優雅的用戶界面(在移動鼠標時出現),為了降低普通用戶的使用難度,還有其他的圖形界面:

  • Baka MPlayer — 免費、開源、跨平台, 基於libmpv的多媒體播放器。(Qt 5)
https://github.com/u8sand/Baka-MPlayer/ || baka-mplayer, baka-mplayer-gitAUR
  • bomi — 強大易用的多媒體播放器 (Qt 5)。
https://bomi-player.github.io/ || bomi-gitAUR
  • Celluloid — mpv的簡單GTK前端,前身為GNOME mpv。
https://celluloid-player.github.io/ || celluloid
  • SMPlayer — Qt 寫的擁有額外特性的多媒體播放器 (CSS 主題, YouTube 整合等等) (Qt 5).
http://smplayer.sourceforge.net/ || smplayer
  • xt7-player-mpv — Qt/Gambas 寫的 mpv 圖形界面,提供了一套豐富的配置選項,包括過濾器和驅動,ladspa 插件支持以及資源庫/播放列表管理, 還有YouTube, 在線電台, podcasts, DVB-T 和更多。
https://github.com/kokoko3k/xt7-player-mpv || xt7-player-mpv-gitAUR
注意: CMPlayer/bomi 安裝包已在內部集成 mpv

配置[編輯 | 編輯原始碼]

mpv 默認配置在性能較弱的電腦上應當也能正常工作。 然而,如果你有性能更好的顯卡,通過配置 mpv 可以得到更好的使用體驗 (經過配置,mpv潛力無窮!)。 如果需要配置,只需要新建幾個配置文件即可 (這些配置文件默認不存在).

注意: 配置文件/etc/mpv從系統範圍內讀取,配置文件~/.config/mpv從用戶範圍內讀取。 (除非你設定了 environment variable XDG_CONFIG_HOME環境變量), 用戶配置文件的優先級要高於系統配置文件。而直接在命令行使用參數的優先級又高於用戶配置文件。建議使用用戶配置文件,因為在配置過程中可能需要不斷嘗試和試錯。

不懂如何開始? mpv 提供了範例配置文件。 把它們當做你的第一步吧:

$ cp -r /usr/share/doc/mpv/ ~/.config/

mpv.confmpv'的主要配置文件, input.conf 是按鍵綁定的配置文件。通讀它們兩個,以了解它們的工作原理以及可用的選項。

一般設定[編輯 | 編輯原始碼]

把以下的設定加入 ~/.config/mpv/mpv.conf.

高質量視頻配置[編輯 | 編輯原始碼]

一般使用 vo=gpu 視頻輸出參數作為高質量視頻的選項。大部分用戶這麼用沒有問題,但是為了避免少部分用不了這個選項的用戶在使用時出現問題,它並不是默認啟用的:

profile=gpu-hq

gpu-hq 默認採用spline36 縮放過濾器並可以提供中等的速度和質量。 為了使視頻輸出質量達到最優,如果你的硬件允許的話,你應該選擇 ewa_lanczossharp

profile=gpu-hq
scale=ewa_lanczossharp
cscale=ewa_lanczossharp

這最後三個選項可能有點複雜。首先是使讓音頻和視頻不同步,而不是丟失視頻幀,重新採樣音頻。 mpv維基上有一篇專門介紹這個的文章: Display Synchronization. 剩下的兩個就是通過更改幀的顯示方式從而讓運動在顯示器上看起來更為平滑,從而使源幀速率與顯示器的刷新率匹配。mpv維基上有一篇專門介紹這個的文章:Interpolation 它的一個別稱是 平滑運動.

profile=gpu-hq
scale=ewa_lanczossharp
cscale=ewa_lanczossharp
video-sync=display-resample
interpolation
tscale=oversample
注意: 如果你在用 NVIDIA Optimus , video-sync=display-resample這一行可能導致視頻加速.

除此之外你還能做很多事情。但是你會發現情況更加複雜,可能需要高性能的顯卡。比如,可以加載執行特殊縮放和銳化技術的特殊着色器。想要了解更多,請參閱mpv wiki, 特別是 有關用戶着色器的部分.

你可能會發現還有很多其它選擇。因此 mpv(1) 值得一看。 推薦在終端中運行 mpv ,因為這樣可以得知錯誤信息。

自定義[編輯 | 編輯原始碼]

In mpv.conf it is possible to create profiles which are essentially just "groups of options" with which you can:

  • Quickly switch between different configurations without having to rewrite the file.
  • Create special profiles for special content.
  • nest profiles so that you can make more complicated profiles out of simpler ones.

Creating a profile is easy. The area at the top of mpv.conf is called the top level, any options you write there will kick into effect once mpv is started. However, once you define a profile by writing its name in brackets then every option you write below it (until you define a new profile) is considered part of that profile. Here is an example mpv.conf:

profile=myprofile2        #Top level area, load myprofile2
ontop=yes                 #Always on top

[myprofile1]              #A simple profile, top level area ends here
profile-desc="a profile"  #Optional description for profile
fs=yes                    #Start in full screen

[myprofile2]              #Another simple profile
profile=gpu-hq            #A built in profile that comes with mpv
log-file=~~/log           #Sets a location for writing a log file, ~~/ translates to ~/.config/mpv

There are only two lines within the top level area and there are two separate profiles defined below it. When mpv starts it sees the first line, loads the options in myprofile2 (which means it loads the options in gpu-hq and log-file=~~/log) finally it loads ontop=yes and finishes starting up. Note, myprofile1 is never loaded because it is never called in the top level area.

Alternatively one could call mpv from the command line with:

$ mpv --profile=myprofile1 video.mkv

and it would ignore all options except the ones for myprofile1.

按鍵綁定[編輯 | 編輯原始碼]

Key bindings are fairly straightforward given the examples in /usr/share/doc/mpv/input.conf and the relevant section in the manual.

Add the following examples to ~/.config/mpv/input.conf:

shift+s         screenshot each-frame
Shift+UP        seek  600
Shift+DOWN      seek -600
=               cycle video-unscaled
-               cycle-values window-scale 2 3 1 .5
WHEEL_UP        add volume 5
WHEEL_DOWN      add volume -5
WHEEL_LEFT      ignore
WHEEL_RIGHT     ignore
Alt+RIGHT       add video-rotate 90
Alt+LEFT        add video-rotate -90
Alt+-           add video-zoom -0.25
Alt+=           add video-zoom 0.25
Alt+j           add video-pan-x -0.05
Alt+l           add video-pan-x 0.05
Alt+i           add video-pan-y 0.05
Alt+k           add video-pan-y -0.05
Alt+BS          set video-zoom 0; set video-pan-x 0; set video-pan-y 0

For an attempt to reproduce MPC-HC key bindings in mpv, see [1].

其他配置文件[編輯 | 編輯原始碼]

In addition there are a few more configuration files and directories that can be created, among which:

  • ~/.config/mpv/script-opts/osc.conf manages the On Screen Controller.
  • ~/.config/mpv/scripts/script-name.lua for Lua scripts. See [2] for an example.

See https://mpv.io/manual/master/#files for more.

腳本[編輯 | 編輯原始碼]

mpv 有一個相當豐富的腳本生態,拓寬了其功能. To this end, it has internal bindings for both Lua and JavaScript (added recently).

Scripts are typically installed by putting them in the ~/.config/mpv/scripts/ directory (you may have to create it first). After that they will be automatically loaded when mpv starts (this behavior can be altered with other mpv options). Some scripts come with their own installation and configuration instructions, so make sure to have a look. In addition some scripts are old, broken, and unmaintained.

JavaScript[編輯 | 編輯原始碼]

JavaScript (ES5 via MuJS) has been supported as an mpv scripting language since 2014. Currently only a few scripts are available, but documentation exists for anyone interested in making their own.

To get started, drop a script with a .js extension in the mpv scripts directory, e.g.:

~/.config/mpv/scripts/fullscreen-off-on-pause.js

function onPauseChange (prop, enabled) {
    if (enabled) {
        mp.set_property('fullscreen', 'no')
    }
}

mp.observe_property('pause', 'bool', onPauseChange)

For more details, e.g. on using require to load CommonJS modules, see the documentation.

JavaScript support is available in the mpv package, as well as some AUR packages, e.g. mpv-fullAUR and mpv-full-gitAUR.

Lua[編輯 | 編輯原始碼]

There are a lot of interesting Lua scripts for mpv. If you would like to make your own, the relevant documentation may be found here.

mpv-stats[編輯 | 編輯原始碼]

mpv-stats (or simply stats) is a Lua script that outputs a lot of live statistics showing how well mpv is currently doing. It is very useful for making sure that your hardware can keep up with your configuration and for comparing different configurations. Since version v0.28.0, the script is built into mpv and can be toggled on or off with the i or I keys (by default).

mpv-webm[編輯 | 編輯原始碼]

mpv-webm (or simply webm) is a very easy to use Lua script that allows one to create WebM files while watching videos. It includes several features and does not have any extra dependencies (relies entirely on mpv).

C[編輯 | 編輯原始碼]

mpv-mpris[編輯 | 編輯原始碼]

The C plugin mpv-mpris allows other applications to integrate with mpv via the MPRIS protocol. For example, with mpv-mpris installed, kdeconnect can automatically pause video playback in mpv when a phone call arrives. Another example is buttons (play\pause etc) on bluetooth audio-devices.

Install mpv-mpris and follow the post-installation instructions displayed by Pacman.

Vapoursynth[編輯 | 編輯原始碼]

Vapoursynth is an alternative to AviSynth that can be used on Linux and allows for Video manipulation via python scripts. Vapoursynths python scripts can be used as video filters for mpv.

To use vapoursynth filters you have to install the vapoursynth package (or vapoursynth-gitAUR) and compile mpv with the --enable-vapoursynth build flag.

This is easier to do by first installing Vapoursynth and then installing (or re-installing if it is already installed) mpv-gitAUR. The configure script for mpv-gitAUR will auto-detect Vapoursynth (as long as it has already been installed) and it will automatically compile mpv with support for Vapoursynth without having to manually change any configure options or anything (this makes it very easy to update mpv as well).

SVP 4 Linux (SmoothVideoProject)[編輯 | 編輯原始碼]

SmoothVideoProject SVP is a program that is primarily known for converting video to 60fps. It is free [as in beer] and full featured for 64bit Linux (costs money for Windows and OS X and is incompatible with 32bit Linux).

It has three main features and each one can be disabled/enabled as one chooses (you are not forced to use motion interpolation).

  1. Motion interpolation (youtube video) - An algorithm that converts video to 60fps. This creates the somewhat controversial "soap opera effect" that some people love and others hate. Unfortunately the algorithm is not perfect and it also introduces more than its share of weird artifacts. The algorithm can be tuned (via a slider) for either performance or quality. It also has some artifact reduction settings that interpolate actual frames with the generated frames reducing the noticeability of the artifacts. The framerate detection can be set to automatic or manual (manual seems to resolve performance issues for some users).
  2. Black bar lighting (youtube video) - If the image has an aspect ratio that produces black bars on your display then SVP will illuminate the black bars with "lights" generated by the content on the screen. It has some amount of customization but the defaults are pretty close to optimal.
  3. LED ambient lighting control (youtube video) - Has the ability to control LED ambient lighting attached to your television.

Once you have mpv compiled with Vapoursynth support it is fairly easy to get SVP working with mpv. Simply install svpAUR[損壞的鏈接:package not found], open the SVP program to let it assess your system performance (you may want to close other programs first so that it gets an accurate reading), and finally add the following mpv profile to your mpv.conf[3]:

mpv.conf
[svp]
input-ipc-server=/tmp/mpvsocket     # Receives input from SVP
hr-seek-framedrop=no                # Fixes audio desync
resume-playback=no                  # Not compatible with SVP

# Can fix stuttering in some cases, in other cases probably causes it. Try it if you experience stuttering.
#opengl-early-flush=yes

Then, in order to use SVP you must have the SVP program running in the background before opening the file using mpv with that profile. Either do:

$ mpv --profile=svp video.mkv

or set profile=svp in the top-level portion of the mpv configuration.

If you want to use hardware decoding then you must use a copy-back decoder since normal decoders are not compatible with Vapoursynth (choose a hwdec option that ends in -copy). For instance:

hwdec=auto-copy
hwdec-codecs=all

Either way, hardware decoding is discouraged by mpv developers and is not likely to make a significant difference in performance.

提示和技巧[編輯 | 編輯原始碼]

Hardware video acceleration[編輯 | 編輯原始碼]

See Hardware video acceleration.

Hardware accelerated video decoding is available via --hwdec=API option. For list of all supported APIs and other required options see relevant manual section.

For Wayland use --gpu-context=wayland option. For list of other available GPU APIs see manual.

Save position on quit[編輯 | 編輯原始碼]

By default you can save the position and quit by pressing Shift+q. The shortcut can be changed by setting quit_watch_later in the key bindings configuration file.

To automatically save the current playback position on quit, start mpv with --save-position-on-quit, or add save-position-on-quit to the configuration file.

聲音太小[編輯 | 編輯原始碼]

Set volume-max=value in your configuration file to a reasonable amount, such as volume-max=150, which then allows you to increase your volume up to 150%, which is more than twice as loud. Increasing your volume too high will result in clipping artefacts. Additionally (or alternatively), you can utilize dynamic range compression with af=acompressor.

聲音正常化[編輯 | 編輯原始碼]

這篇文章的某些內容需要擴充。

原因: Add little more details about the available filters, see [4] for a comparison of loudnorm and dynaudnorm. (在 Talk:Mpv 中討論)

Different sources may have different or inconsistent loudness, so mpv users may need to configure automatic volume normalization. For example:

~/.config/mpv/input.conf
n cycle_values af loudnorm=I=-30 loudnorm=I=-15 anull

This binds the key n to cycle the audio filter settings (af) through the specified values:

  • loudnorm=I=-30: loudnorm setting with I=-30, soft volume, might be suitable for background music
  • loudnorm=I=-15: louder volume, might be good for the video currently in view
  • anull: reset audio filter to null, i.e., disable the audio filter
注意: Binding a key does not change the default audio filter. To change the default, add e.g. af=loudnorm=I=-30 to the main configuration file.

Audio filtering in mpv is provided by the FFmpeg backend. See Wikipedia:EBU R 128 and ffmpeg loudnorm filter for details.

See also upstream issues [5] and [6] which mention different options.

播放DVD[編輯 | 編輯原始碼]

mpv does not support DVD menus. To start the main stream with the longest title of a video DVD, use the command:

$ mpv dvd://

An optional title specifier is a number (starting at 0) which selects between separate video streams on the DVD:

$ mpv dvd://[title] 

DVDs which have been copied on to a local file system (by e.g. the dvdbackup tool) are accommodated by specifying the path to the local copy: --dvd-device=PATH.

See the following desktop file example for playing DVDs from a local file system:

[Desktop Entry]
Type=Application
Name=mpv Media Player DVD 
GenericName=Multimedia player
Comment=Play movies and songs
Icon=mpv
Exec=mpv dvd:// --player-operation-mode=pseudo-gui --force-window --idle --dvd-device=%f
Terminal=false
Categories=AudioVideo;Audio;Video;Player;TV;
# (MimeType and X-KDE-Protocols omitted, see orginianl mpv.desktop file)

By replacing the Exec line with

Exec=mpv dvd://0 dvd://1 dvd://2 dvd://3 dvd://4 dvd://5 dvd://6 dvd://7 dvd://8 dvd://9  --player-operation-mode=pseudo-gui --force-window --idle --dvd-device=%f

the mpv player will queue DVD title 0 to 9 in the playlist, which allows the user to play the titles consecutively or jump forward and backward in the DVD titles with the mpv GUI.

Install libdvdcss, to fix the error:

[dvdnav] Error getting next block from DVD 1 (Error reading from DVD.)

Quickly cycle between aspect ratios[編輯 | 編輯原始碼]

You can cycle between aspect ratios using Shift+a.

Ignoring aspect ratio[編輯 | 編輯原始碼]

You can ignore aspect ratio using --keepaspect=no. To make option permanent, add line keepaspect=no to configuration file.

Draw to the root window[編輯 | 編輯原始碼]

Run mpv with --wid=0. mpv will draw to the window with a window ID of 0.

Always show application window[編輯 | 編輯原始碼]

To show application window even for audio files when launching mpv from command line use --force-window option. To make option permament, add line force-window=yes to the configuration file.

Disable video output[編輯 | 編輯原始碼]

To disable video output when launching from command line use --vid=no option, or its alias, --no-video.

恢復舊的OSC[編輯 | 編輯原始碼]

Since version 0.21.0, mpv has replaced the on-screen controls by a bottombar. In case you want on-screen controls back, you can edit the mpv configuration as described here.

Use as a browser plugin[編輯 | 編輯原始碼]

With the help of mozpluggerAUR, mpv can be used in a supported browser to play video. See Browser plugins#MozPlugger for configuration details. This coupled with a user script such as ViewTube, allows you to use mpv in place of a site's integrated video player.

It may be needed to specify a valid user agent for HTTP streaming, e.g. user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0".

Browser plugins#Multimedia playback page shows other easy ways to watch videos.

Improving mpv as a music player with Lua scripts[編輯 | 編輯原始碼]

The development of mpv's Lua scripts are documented in DOCS/man/lua.rst and examples are shown in TOOLS/lua of the mpv repository. This blog post introduces the music.lua script, which shows how Lua scripts can be used to improve mpv as a music player.

Twitch.tv streaming over mpv[編輯 | 編輯原始碼]

If youtube-dl is installed, mpv can directly open a Twitch livestream.

Alternatively, see Streamlink#Twitch.

Another alternative based on Livestreamer is this Lua script: https://gist.github.com/ChrisK2/8701184fe3ea7701c9cc

youtube-dl and choosing formats[編輯 | 編輯原始碼]

The default --ytdl-format is bestvideo+bestaudio/best. For youtube videos that have 4K resolutions available, this may mean that your device will struggle to decode 4K VP9 encoded video in software even if the attached monitor is much lower resolution.

Setting the right youtube-dl format selectors can fix this easily though. In the following configuration example, only videos with a vertical resolution of 1080 pixels or less will be considered.

ytdl-format="bestvideo[height<=?1080]+bestaudio/best"

If you wish to avoid a certain codec altogether because you cannot hardware-decode it, you can add this to the format selector. For example, we can additionally choose to ignore VP9 as follows:

ytdl-format="bestvideo[height<=?1080][vcodec!=vp9]+bestaudio/best"

If you prefer best quality open codecs (VP9 and Opus), use:

ytdl-format="((bestvideo[vcodec^=vp9]/bestvideo)+(bestaudio[acodec=opus]/bestaudio[acodec=vorbis]/bestaudio[acodec=aac]/bestaudio))/best"

youtube-dl audio with search[編輯 | 編輯原始碼]

To find and stream audio from your terminal emulator with yta search terms put the following function in your .bashrc:

function yta() {
    mpv --ytdl-format=bestaudio ytdl://ytsearch:"$*"
}

Creating a single screenshot[編輯 | 編輯原始碼]

An example of creating a single screenshot, by using a start time (HH:MM:SS):

$ mpv --no-audio --start=00:01:30 --frames=1 /path/to/video/file --o=/path/to/screenshot.png

Screenshots will be saved in /path/to/screenshot.png.

疑難解答[編輯 | 編輯原始碼]

一般調試方法[編輯 | 編輯原始碼]

If you are having trouble with mpv's playback (or if it is flat out failing to run) then the first three things you should do are:

  1. Run mpv from the command line (the -v flag increases verbosity). If you are lucky there will be an error message there telling you what is wrong.
    $ mpv -v video.mkv
  2. Have mpv output a log file. The log file might be difficult to sift through but if something is broken you might see it there.
    $ mpv -v --log-file=./log video.mkv
  3. Run mpv without a configuration. If this runs well then the problem is somewhere in your configuration (perhaps your hardware cannot keep up with your settings).
    $ mpv --no-config video.mkv

If mpv runs but it just does not run well then a fourth thing that might be worth taking a look at is installing the mpv-stats script and using it to see exactly how it is performing.

Fix jerky playback and tearing[編輯 | 編輯原始碼]

mpv defaults to using the OpenGL video output device setting on hardware that supports it. In cases such as trying to play video back on a 4K display using a Intel HD4XXX series card or similar, you will find video playback unreliable, jerky to the point of stopping entirely at times and with major tearing when using any OpenGL output setting. If you experience any of these issues, using the XV (Xorg only) video output device may help:

~/.config/mpv/mpv.conf
vo=xv
注意: This is the most compatible VO on X, but may be low-quality, and has issues with OSD and subtitle display.

It is possible to increase playback performance even more (especially on lower hardware), but this decreases the video quality dramatically in most cases.

The following options may be considered to increase the video playback performance:

~/.config/mpv/mpv.conf
vd-lavc-fast
vd-lavc-skiploopfilter=<skipvalue>
vd-lavc-skipframe=<skipvalue>
vd-lavc-framedrop=<skipvalue>
vd-lavc-threads=<threads>

Problems with window compositors[編輯 | 編輯原始碼]

Window compositors such as KWin or Mutter can cause trouble for playback smoothness. In such cases, it may help to set x11-bypass-compositor=yes to make mpv also disable window compositing when playing in windowed mode (if supported by the compositor).

With KWin compositing and hardware decoding, you may also want to set x11-bypass-compositor=no to keep compositing enabled in fullscreen, since reanabling compositing after leaving fullscreen may introduce stutter for a period of time.

No volume bar, cannot change volume[編輯 | 編輯原始碼]

Spin the mouse wheel over the volume icon.

GNOME Blank screen (Wayland)[編輯 | 編輯原始碼]

mpv may not suspend GNOME's Power Saving Settings if using Wayland resulting in screen saver turning off the monitor during video playback. A workaround is to add gnome-session-inhibit to the beginning of the Exec= line in mpv.desktop.

Use mpv with a compositor[編輯 | 編輯原始碼]

If you are using a compositor (e.g. in KDE Plasma 5) and find that composition is disabled (e.g. in Plasma this would make you unable to present windows or see window thumbnails in the default app switcher) when mpv is playing a video, try x11-bypass-compositor=no

Cursor theme not respected under GNOME Wayland[編輯 | 編輯原始碼]

On Wayland, clients can display different cursor themes because there is not a unique configuration file for it. For the cursor theme, Qt apps usually accept the value that is set for the environment variable XCURSOR_THEME. However, in the specific case of mpv, the cursor theme that is displayed needs to be the one set in ~/.icons/default/index.theme. Since GNOME does not update this file when changing the cursor theme with GNOME Tweaks, you will have to do it manually. See Cursor themes#XDG specification for more information.