diff options
Diffstat (limited to 'projects/python-mpv')
-rw-r--r-- | projects/python-mpv/index.html | 438 |
1 files changed, 219 insertions, 219 deletions
diff --git a/projects/python-mpv/index.html b/projects/python-mpv/index.html index e789ace..46dbb4b 100644 --- a/projects/python-mpv/index.html +++ b/projects/python-mpv/index.html @@ -39,8 +39,8 @@ features of the player, just as the lua interface does.</p> <div class="section" id="installation"> <h2>Installation</h2> <pre class="code bash literal-block"> -pip<span class="whitespace"> </span>install<span class="whitespace"> </span>mpv -</pre> +<span class="lineno"></span><span class="line">pip<span class="w"> </span>install<span class="w"> </span>mpv +</span></pre> <p>...though you can also realistically just copy <a class="reference external" href="https://raw.githubusercontent.com/jaseg/python-mpv/main/mpv.py">mpv.py</a> into your project as it's all nicely contained in one file.</p> <div class="section" id="requirements"> <h3>Requirements</h3> @@ -73,11 +73,11 @@ since right now there is not many OSX users.</p> <div class="section" id="usage"> <h2>Usage</h2> <pre class="code python literal-block"> -<span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">ytdl</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'https://youtu.be/DOmdB7D-pUU'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">ytdl</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'https://youtu.be/DOmdB7D-pUU'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> <p>python-mpv mostly exposes mpv's built-in API to python, adding only some porcelain on top. Most "<a class="reference external" href="https://mpv.io/manual/master/#list-of-input-commands">input commands</a>" are mapped to methods of the MPV class. Check out these methods and their docstrings in <a class="reference external" href="https://github.com/jaseg/python-mpv/blob/main/mpv.py">the source</a> for things you can do. Additional controls and status information are exposed through <a class="reference external" href="https://mpv.io/manual/master/#properties">MPV properties</a>. These can be accessed like <tt class="docutils literal">player.metadata</tt>, <tt class="docutils literal">player.fullscreen</tt> and <tt class="docutils literal">player.loop_playlist</tt>.</p> <div class="section" id="threading"> <h3>Threading</h3> @@ -95,168 +95,168 @@ that can be passed to an event loop to tell it to wake up the python-mpv event h <div class="section" id="logging-properties-python-key-bindings-screenshots-and-youtube-dl"> <h4>Logging, Properties, Python Key Bindings, Screenshots and youtube-dl</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="keyword">def</span> <span class="name function">my_log</span><span class="punctuation">(</span><span class="name">loglevel</span><span class="punctuation">,</span> <span class="name">component</span><span class="punctuation">,</span> <span class="name">message</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name builtin">print</span><span class="punctuation">(</span><span class="literal string single">'[</span><span class="literal string interpol">{}</span><span class="literal string single">] </span><span class="literal string interpol">{}</span><span class="literal string single">: </span><span class="literal string interpol">{}</span><span class="literal string single">'</span><span class="operator">.</span><span class="name">format</span><span class="punctuation">(</span><span class="name">loglevel</span><span class="punctuation">,</span> <span class="name">component</span><span class="punctuation">,</span> <span class="name">message</span><span class="punctuation">))</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">log_handler</span><span class="operator">=</span><span class="name">my_log</span><span class="punctuation">,</span> <span class="name">ytdl</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">input_default_bindings</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">input_vo_keyboard</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="comment single"># Property access, these can be changed at runtime</span><span class="whitespace"> -</span><span class="name decorator">@player</span><span class="operator">.</span><span class="name">property_observer</span><span class="punctuation">(</span><span class="literal string single">'time-pos'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="keyword">def</span> <span class="name function">time_observer</span><span class="punctuation">(</span><span class="name">_name</span><span class="punctuation">,</span> <span class="name">value</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="comment single"># Here, _value is either None if nothing is playing or a float containing</span><span class="whitespace"> -</span> <span class="comment single"># fractional seconds since the beginning of the file.</span><span class="whitespace"> -</span> <span class="name builtin">print</span><span class="punctuation">(</span><span class="literal string single">'Now playing at </span><span class="literal string interpol">{:.2f}</span><span class="literal string single">s'</span><span class="operator">.</span><span class="name">format</span><span class="punctuation">(</span><span class="name">value</span><span class="punctuation">))</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">fullscreen</span> <span class="operator">=</span> <span class="keyword constant">True</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">loop_playlist</span> <span class="operator">=</span> <span class="literal string single">'inf'</span><span class="whitespace"> -</span><span class="comment single"># Option access, in general these require the core to reinitialize</span><span class="whitespace"> -</span><span class="name">player</span><span class="punctuation">[</span><span class="literal string single">'vo'</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal string single">'gpu'</span><span class="whitespace"> - -</span><span class="name decorator">@player</span><span class="operator">.</span><span class="name">on_key_press</span><span class="punctuation">(</span><span class="literal string single">'q'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="keyword">def</span> <span class="name function">my_q_binding</span><span class="punctuation">():</span><span class="whitespace"> -</span> <span class="name builtin">print</span><span class="punctuation">(</span><span class="literal string single">'THERE IS NO ESCAPE'</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name decorator">@player</span><span class="operator">.</span><span class="name">on_key_press</span><span class="punctuation">(</span><span class="literal string single">'s'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="keyword">def</span> <span class="name function">my_s_binding</span><span class="punctuation">():</span><span class="whitespace"> -</span> <span class="name">pillow_img</span> <span class="operator">=</span> <span class="name">player</span><span class="operator">.</span><span class="name">screenshot_raw</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="name">pillow_img</span><span class="operator">.</span><span class="name">save</span><span class="punctuation">(</span><span class="literal string single">'screenshot.png'</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'https://youtu.be/DLzxrzFCyOs'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span><span class="whitespace"> - -</span><span class="keyword">del</span> <span class="name">player</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">my_log</span><span class="p">(</span><span class="n">loglevel</span><span class="p">,</span> <span class="n">component</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">print</span><span class="p">(</span><span class="s1">'[</span><span class="si">{}</span><span class="s1">] </span><span class="si">{}</span><span class="s1">: </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">loglevel</span><span class="p">,</span> <span class="n">component</span><span class="p">,</span> <span class="n">message</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">log_handler</span><span class="o">=</span><span class="n">my_log</span><span class="p">,</span> <span class="n">ytdl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_default_bindings</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_vo_keyboard</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="c1"># Property access, these can be changed at runtime</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="nd">@player</span><span class="o">.</span><span class="n">property_observer</span><span class="p">(</span><span class="s1">'time-pos'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">time_observer</span><span class="p">(</span><span class="n">_name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># Here, _value is either None if nothing is playing or a float containing</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># fractional seconds since the beginning of the file.</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">print</span><span class="p">(</span><span class="s1">'Now playing at </span><span class="si">{:.2f}</span><span class="s1">s'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">value</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">fullscreen</span> <span class="o">=</span> <span class="kc">True</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">loop_playlist</span> <span class="o">=</span> <span class="s1">'inf'</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="c1"># Option access, in general these require the core to reinitialize</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="p">[</span><span class="s1">'vo'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'gpu'</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="nd">@player</span><span class="o">.</span><span class="n">on_key_press</span><span class="p">(</span><span class="s1">'q'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">my_q_binding</span><span class="p">():</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">print</span><span class="p">(</span><span class="s1">'THERE IS NO ESCAPE'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="nd">@player</span><span class="o">.</span><span class="n">on_key_press</span><span class="p">(</span><span class="s1">'s'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">my_s_binding</span><span class="p">():</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">pillow_img</span> <span class="o">=</span> <span class="n">player</span><span class="o">.</span><span class="n">screenshot_raw</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">pillow_img</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s1">'screenshot.png'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'https://youtu.be/DLzxrzFCyOs'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">del</span> <span class="n">player</span> +</span></pre> </div> <div class="section" id="skipping-silence-using-libav-filters"> <h4>Skipping silence using libav filters</h4> <p>The following code uses the libav silencedetect filter to skip silence at the beginning of a file. It works by loading the filter, then parsing its output from mpv's log. Thanks to Sean DeNigris on github (#202) for the original code!</p> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">sys</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">p</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">p</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="name">sys</span><span class="operator">.</span><span class="name">argv</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">])</span><span class="whitespace"> - -</span><span class="keyword">def</span> <span class="name function">skip_silence</span><span class="punctuation">():</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">set_loglevel</span><span class="punctuation">(</span><span class="literal string single">'debug'</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">af</span> <span class="operator">=</span> <span class="literal string single">'lavfi=[silencedetect=n=-20dB:d=1]'</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">speed</span> <span class="operator">=</span> <span class="literal number integer">100</span><span class="whitespace"> -</span> <span class="keyword">def</span> <span class="name function">check</span><span class="punctuation">(</span><span class="name">evt</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name">toks</span> <span class="operator">=</span> <span class="name">evt</span><span class="punctuation">[</span><span class="literal string single">'event'</span><span class="punctuation">][</span><span class="literal string single">'text'</span><span class="punctuation">]</span><span class="operator">.</span><span class="name">split</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="keyword">if</span> <span class="literal string single">'silence_end:'</span> <span class="operator word">in</span> <span class="name">toks</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="keyword">return</span> <span class="name builtin">float</span><span class="punctuation">(</span><span class="name">toks</span><span class="punctuation">[</span><span class="literal number integer">2</span><span class="punctuation">])</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">time_pos</span> <span class="operator">=</span> <span class="name">p</span><span class="operator">.</span><span class="name">wait_for_event</span><span class="punctuation">(</span><span class="literal string single">'log_message'</span><span class="punctuation">,</span> <span class="name">cond</span><span class="operator">=</span><span class="name">check</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">speed</span> <span class="operator">=</span> <span class="literal number integer">1</span><span class="whitespace"> -</span> <span class="name">p</span><span class="operator">.</span><span class="name">af</span> <span class="operator">=</span> <span class="literal string single">''</span><span class="whitespace"> - -</span><span class="name">skip_silence</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">p</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">sys</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">p</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">p</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">skip_silence</span><span class="p">():</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">set_loglevel</span><span class="p">(</span><span class="s1">'debug'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">af</span> <span class="o">=</span> <span class="s1">'lavfi=[silencedetect=n=-20dB:d=1]'</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">speed</span> <span class="o">=</span> <span class="mi">100</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="n">evt</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">toks</span> <span class="o">=</span> <span class="n">evt</span><span class="p">[</span><span class="s1">'event'</span><span class="p">][</span><span class="s1">'text'</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">if</span> <span class="s1">'silence_end:'</span> <span class="ow">in</span> <span class="n">toks</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">toks</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">time_pos</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">wait_for_event</span><span class="p">(</span><span class="s1">'log_message'</span><span class="p">,</span> <span class="n">cond</span><span class="o">=</span><span class="n">check</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">speed</span> <span class="o">=</span> <span class="mi">1</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">p</span><span class="o">.</span><span class="n">af</span> <span class="o">=</span> <span class="s1">''</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">skip_silence</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">p</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="video-overlays"> <h4>Video overlays</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">time</span><span class="whitespace"> -</span><span class="keyword namespace">from</span> <span class="name namespace">PIL</span> <span class="keyword namespace">import</span> <span class="name">Image</span><span class="punctuation">,</span> <span class="name">ImageDraw</span><span class="punctuation">,</span> <span class="name">ImageFont</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">()</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">loop</span> <span class="operator">=</span> <span class="keyword constant">True</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'test.webm'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_until_playing</span><span class="punctuation">()</span><span class="whitespace"> - -</span><span class="name">font</span> <span class="operator">=</span> <span class="name">ImageFont</span><span class="operator">.</span><span class="name">truetype</span><span class="punctuation">(</span><span class="literal string single">'DejaVuSans.ttf'</span><span class="punctuation">,</span> <span class="literal number integer">40</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="keyword">while</span> <span class="operator word">not</span> <span class="name">player</span><span class="operator">.</span><span class="name">core_idle</span><span class="punctuation">:</span><span class="whitespace"> - -</span> <span class="name">time</span><span class="operator">.</span><span class="name">sleep</span><span class="punctuation">(</span><span class="literal number float">0.5</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">overlay</span> <span class="operator">=</span> <span class="name">player</span><span class="operator">.</span><span class="name">create_image_overlay</span><span class="punctuation">()</span><span class="whitespace"> - -</span> <span class="keyword">for</span> <span class="name">pos</span> <span class="operator word">in</span> <span class="name builtin">range</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">500</span><span class="punctuation">,</span> <span class="literal number integer">5</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name">ts</span> <span class="operator">=</span> <span class="name">player</span><span class="operator">.</span><span class="name">time_pos</span><span class="whitespace"> -</span> <span class="keyword">if</span> <span class="name">ts</span> <span class="operator word">is</span> <span class="keyword constant">None</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="keyword">break</span><span class="whitespace"> - -</span> <span class="name">img</span> <span class="operator">=</span> <span class="name">Image</span><span class="operator">.</span><span class="name">new</span><span class="punctuation">(</span><span class="literal string single">'RGBA'</span><span class="punctuation">,</span> <span class="punctuation">(</span><span class="literal number integer">400</span><span class="punctuation">,</span> <span class="literal number integer">150</span><span class="punctuation">),</span> <span class="punctuation">(</span><span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">))</span><span class="whitespace"> -</span> <span class="name">d</span> <span class="operator">=</span> <span class="name">ImageDraw</span><span class="operator">.</span><span class="name">Draw</span><span class="punctuation">(</span><span class="name">img</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">d</span><span class="operator">.</span><span class="name">text</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="literal number integer">10</span><span class="punctuation">),</span> <span class="literal string single">'Hello World'</span><span class="punctuation">,</span> <span class="name">font</span><span class="operator">=</span><span class="name">font</span><span class="punctuation">,</span> <span class="name">fill</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">128</span><span class="punctuation">))</span><span class="whitespace"> -</span> <span class="name">d</span><span class="operator">.</span><span class="name">text</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="literal number integer">60</span><span class="punctuation">),</span> <span class="literal string affix">f</span><span class="literal string single">'t=</span><span class="literal string interpol">{</span><span class="name">ts</span><span class="literal string interpol">:</span><span class="literal string single">.3f</span><span class="literal string interpol">}</span><span class="literal string single">'</span><span class="punctuation">,</span> <span class="name">font</span><span class="operator">=</span><span class="name">font</span><span class="punctuation">,</span> <span class="name">fill</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">,</span> <span class="literal number integer">255</span><span class="punctuation">))</span><span class="whitespace"> - -</span> <span class="name">overlay</span><span class="operator">.</span><span class="name">update</span><span class="punctuation">(</span><span class="name">img</span><span class="punctuation">,</span> <span class="name">pos</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">2</span><span class="operator">*</span><span class="name">pos</span><span class="punctuation">,</span> <span class="name">pos</span><span class="punctuation">))</span><span class="whitespace"> -</span> <span class="name">time</span><span class="operator">.</span><span class="name">sleep</span><span class="punctuation">(</span><span class="literal number float">0.05</span><span class="punctuation">)</span><span class="whitespace"> - -</span> <span class="name">overlay</span><span class="operator">.</span><span class="name">remove</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">time</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span><span class="p">,</span> <span class="n">ImageDraw</span><span class="p">,</span> <span class="n">ImageFont</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">loop</span> <span class="o">=</span> <span class="kc">True</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'test.webm'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_until_playing</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">font</span> <span class="o">=</span> <span class="n">ImageFont</span><span class="o">.</span><span class="n">truetype</span><span class="p">(</span><span class="s1">'DejaVuSans.ttf'</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">while</span> <span class="ow">not</span> <span class="n">player</span><span class="o">.</span><span class="n">core_idle</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">overlay</span> <span class="o">=</span> <span class="n">player</span><span class="o">.</span><span class="n">create_image_overlay</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">for</span> <span class="n">pos</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">ts</span> <span class="o">=</span> <span class="n">player</span><span class="o">.</span><span class="n">time_pos</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">if</span> <span class="n">ts</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">break</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">'RGBA'</span><span class="p">,</span> <span class="p">(</span><span class="mi">400</span><span class="p">,</span> <span class="mi">150</span><span class="p">),</span> <span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">d</span> <span class="o">=</span> <span class="n">ImageDraw</span><span class="o">.</span><span class="n">Draw</span><span class="p">(</span><span class="n">img</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">d</span><span class="o">.</span><span class="n">text</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="s1">'Hello World'</span><span class="p">,</span> <span class="n">font</span><span class="o">=</span><span class="n">font</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">128</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">d</span><span class="o">.</span><span class="n">text</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">60</span><span class="p">),</span> <span class="sa">f</span><span class="s1">'t=</span><span class="si">{</span><span class="n">ts</span><span class="si">:</span><span class="s1">.3f</span><span class="si">}</span><span class="s1">'</span><span class="p">,</span> <span class="n">font</span><span class="o">=</span><span class="n">font</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">overlay</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">pos</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="n">pos</span><span class="p">))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.05</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">overlay</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="playlist-handling"> <h4>Playlist handling</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">ytdl</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">input_default_bindings</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">input_vo_keyboard</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">playlist_append</span><span class="punctuation">(</span><span class="literal string single">'https://youtu.be/PHIGke6Yzh8'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">playlist_append</span><span class="punctuation">(</span><span class="literal string single">'https://youtu.be/Ji9qSuQapFY'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">playlist_append</span><span class="punctuation">(</span><span class="literal string single">'https://youtu.be/6f78_Tf4Tdk'</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">playlist_pos</span> <span class="operator">=</span> <span class="literal number integer">0</span><span class="whitespace"> - -</span><span class="keyword">while</span> <span class="keyword constant">True</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="comment single"># To modify the playlist, use player.playlist_{append,clear,move,remove}. player.playlist is read-only</span><span class="whitespace"> -</span> <span class="name builtin">print</span><span class="punctuation">(</span><span class="name">player</span><span class="operator">.</span><span class="name">playlist</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">ytdl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_default_bindings</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_vo_keyboard</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">playlist_append</span><span class="p">(</span><span class="s1">'https://youtu.be/PHIGke6Yzh8'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">playlist_append</span><span class="p">(</span><span class="s1">'https://youtu.be/Ji9qSuQapFY'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">playlist_append</span><span class="p">(</span><span class="s1">'https://youtu.be/6f78_Tf4Tdk'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">playlist_pos</span> <span class="o">=</span> <span class="mi">0</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># To modify the playlist, use player.playlist_{append,clear,move,remove}. player.playlist is read-only</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">print</span><span class="p">(</span><span class="n">player</span><span class="o">.</span><span class="n">playlist</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="directly-feeding-mpv-data-from-python"> <h4>Directly feeding mpv data from python</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name decorator">@player</span><span class="operator">.</span><span class="name">python_stream</span><span class="punctuation">(</span><span class="literal string single">'foo'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="keyword">def</span> <span class="name function">reader</span><span class="punctuation">():</span><span class="whitespace"> -</span> <span class="keyword">with</span> <span class="name builtin">open</span><span class="punctuation">(</span><span class="literal string single">'test.webm'</span><span class="punctuation">,</span> <span class="literal string single">'rb'</span><span class="punctuation">)</span> <span class="keyword">as</span> <span class="name">f</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="keyword">while</span> <span class="keyword constant">True</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="keyword">yield</span> <span class="name">f</span><span class="operator">.</span><span class="name">read</span><span class="punctuation">(</span><span class="literal number integer">1024</span><span class="operator">*</span><span class="literal number integer">1024</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'python://foo'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="nd">@player</span><span class="o">.</span><span class="n">python_stream</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">def</span> <span class="nf">reader</span><span class="p">():</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'test.webm'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">while</span> <span class="kc">True</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">yield</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'python://foo'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="using-external-subtitles"> <h4>Using external subtitles</h4> <p>The easiest way to load custom subtitles from a file is to pass the <tt class="docutils literal"><span class="pre">--sub-file</span></tt> option to the <tt class="docutils literal">loadfile</tt> call:</p> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">loadfile</span><span class="punctuation">(</span><span class="literal string single">'test.webm'</span><span class="punctuation">,</span> <span class="name">sub_file</span><span class="operator">=</span><span class="literal string single">'test.srt'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">loadfile</span><span class="p">(</span><span class="s1">'test.webm'</span><span class="p">,</span> <span class="n">sub_file</span><span class="o">=</span><span class="s1">'test.srt'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> <p>Note that you can also pass many other options to <tt class="docutils literal">loadfile</tt>. See the mpv docs for details.</p> <p>If you want to add subtitle files or streams at runtime, you can use the <tt class="docutils literal"><span class="pre">sub-add</span></tt> command. <tt class="docutils literal"><span class="pre">sub-add</span></tt> can only be called once the player is done loading the file and starts playing. An easy way to wait for this is to wait for the <tt class="docutils literal"><span class="pre">core-idle</span></tt> property.</p> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'test.webm'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_until_playing</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">sub_add</span><span class="punctuation">(</span><span class="literal string single">'test.srt'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">player</span><span class="operator">.</span><span class="name">wait_for_playback</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'test.webm'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_until_playing</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">sub_add</span><span class="p">(</span><span class="s1">'test.srt'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span><span class="o">.</span><span class="n">wait_for_playback</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="using-mpv-s-built-in-gui"> <h4>Using MPV's built-in GUI</h4> @@ -264,92 +264,92 @@ called once the player is done loading the file and starts playing. An easy way GUI features such as the OSD or keyboard input. To enable the built-in GUI, use the following options when initializing the MPV instance. See <a class="reference external" href="https://github.com/jaseg/python-mpv/issues/61">Issue 102</a> for more details</p> <pre class="code python literal-block"> -<span class="comment single"># Enable the on-screen controller and keyboard shortcuts</span><span class="whitespace"> -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">input_default_bindings</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">input_vo_keyboard</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">osc</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="comment single"># Alternative version using the old "floating box" style on-screen controller</span><span class="whitespace"> -</span><span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">player_operation_mode</span><span class="operator">=</span><span class="literal string single">'pseudo-gui'</span><span class="punctuation">,</span><span class="whitespace"> -</span> <span class="name">script_opts</span><span class="operator">=</span><span class="literal string single">'osc-layout=box,osc-seekbarstyle=bar,osc-deadzonesize=0,osc-minmousemove=3'</span><span class="punctuation">,</span><span class="whitespace"> -</span> <span class="name">input_default_bindings</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span><span class="whitespace"> -</span> <span class="name">input_vo_keyboard</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span><span class="whitespace"> -</span> <span class="name">osc</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span> -</pre> +<span class="lineno"></span><span class="line"><span class="c1"># Enable the on-screen controller and keyboard shortcuts</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">input_default_bindings</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_vo_keyboard</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">osc</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="c1"># Alternative version using the old "floating box" style on-screen controller</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">player_operation_mode</span><span class="o">=</span><span class="s1">'pseudo-gui'</span><span class="p">,</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">script_opts</span><span class="o">=</span><span class="s1">'osc-layout=box,osc-seekbarstyle=bar,osc-deadzonesize=0,osc-minmousemove=3'</span><span class="p">,</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">input_default_bindings</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">input_vo_keyboard</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">osc</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> +</span></pre> </div> <div class="section" id="pyqt-embedding"> <h4>PyQT embedding</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">sys</span><span class="whitespace"> - -</span><span class="keyword namespace">from</span> <span class="name namespace">PyQt5.QtWidgets</span> <span class="keyword namespace">import</span> <span class="operator">*</span><span class="whitespace"> -</span><span class="keyword namespace">from</span> <span class="name namespace">PyQt5.QtCore</span> <span class="keyword namespace">import</span> <span class="operator">*</span><span class="whitespace"> - -</span><span class="keyword">class</span> <span class="name class">Test</span><span class="punctuation">(</span><span class="name">QMainWindow</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="keyword">def</span> <span class="name function magic">__init__</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="punctuation">,</span> <span class="name">parent</span><span class="operator">=</span><span class="keyword constant">None</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name builtin">super</span><span class="punctuation">()</span><span class="operator">.</span><span class="name function magic">__init__</span><span class="punctuation">(</span><span class="name">parent</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">container</span> <span class="operator">=</span> <span class="name">QWidget</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">setCentralWidget</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">container</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">container</span><span class="operator">.</span><span class="name">setAttribute</span><span class="punctuation">(</span><span class="name">Qt</span><span class="operator">.</span><span class="name">WA_DontCreateNativeAncestors</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">container</span><span class="operator">.</span><span class="name">setAttribute</span><span class="punctuation">(</span><span class="name">Qt</span><span class="operator">.</span><span class="name">WA_NativeWindow</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">player</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">wid</span><span class="operator">=</span><span class="name builtin">str</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">container</span><span class="operator">.</span><span class="name">winId</span><span class="punctuation">())),</span><span class="whitespace"> -</span> <span class="name">vo</span><span class="operator">=</span><span class="literal string single">'x11'</span><span class="punctuation">,</span> <span class="comment single"># You may not need this</span><span class="whitespace"> -</span> <span class="name">log_handler</span><span class="operator">=</span><span class="name builtin">print</span><span class="punctuation">,</span><span class="whitespace"> -</span> <span class="name">loglevel</span><span class="operator">=</span><span class="literal string single">'debug'</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name">player</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string single">'test.webm'</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="name">app</span> <span class="operator">=</span> <span class="name">QApplication</span><span class="punctuation">(</span><span class="name">sys</span><span class="operator">.</span><span class="name">argv</span><span class="punctuation">)</span><span class="whitespace"> - -</span><span class="comment single"># This is necessary since PyQT stomps over the locale settings needed by libmpv.</span><span class="whitespace"> -</span><span class="comment single"># This needs to happen after importing PyQT before creating the first mpv.MPV instance.</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">locale</span><span class="whitespace"> -</span><span class="name">locale</span><span class="operator">.</span><span class="name">setlocale</span><span class="punctuation">(</span><span class="name">locale</span><span class="operator">.</span><span class="name">LC_NUMERIC</span><span class="punctuation">,</span> <span class="literal string single">'C'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="name">win</span> <span class="operator">=</span> <span class="name">Test</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">win</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span><span class="whitespace"> -</span><span class="name">sys</span><span class="operator">.</span><span class="name">exit</span><span class="punctuation">(</span><span class="name">app</span><span class="operator">.</span><span class="name">exec_</span><span class="punctuation">())</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">sys</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">from</span> <span class="nn">PyQt5.QtWidgets</span> <span class="kn">import</span> <span class="o">*</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">from</span> <span class="nn">PyQt5.QtCore</span> <span class="kn">import</span> <span class="o">*</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">class</span> <span class="nc">Test</span><span class="p">(</span><span class="n">QMainWindow</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">container</span> <span class="o">=</span> <span class="n">QWidget</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">setCentralWidget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">container</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">container</span><span class="o">.</span><span class="n">setAttribute</span><span class="p">(</span><span class="n">Qt</span><span class="o">.</span><span class="n">WA_DontCreateNativeAncestors</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">container</span><span class="o">.</span><span class="n">setAttribute</span><span class="p">(</span><span class="n">Qt</span><span class="o">.</span><span class="n">WA_NativeWindow</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">player</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">wid</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">container</span><span class="o">.</span><span class="n">winId</span><span class="p">())),</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">vo</span><span class="o">=</span><span class="s1">'x11'</span><span class="p">,</span> <span class="c1"># You may not need this</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">log_handler</span><span class="o">=</span><span class="nb">print</span><span class="p">,</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">loglevel</span><span class="o">=</span><span class="s1">'debug'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">player</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s1">'test.webm'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">app</span> <span class="o">=</span> <span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="c1"># This is necessary since PyQT stomps over the locale settings needed by libmpv.</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="c1"># This needs to happen after importing PyQT before creating the first mpv.MPV instance.</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">locale</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_NUMERIC</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">win</span> <span class="o">=</span> <span class="n">Test</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">win</span><span class="o">.</span><span class="n">show</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">())</span> +</span></pre> </div> <div class="section" id="pygobject-embedding"> <h4>PyGObject embedding</h4> <pre class="code python literal-block"> -<span class="comment hashbang">#!/usr/bin/env python3</span><span class="whitespace"> -</span><span class="keyword namespace">import</span> <span class="name namespace">gi</span><span class="whitespace"> - -</span><span class="keyword namespace">import</span> <span class="name namespace">mpv</span><span class="whitespace"> - -</span><span class="name">gi</span><span class="operator">.</span><span class="name">require_version</span><span class="punctuation">(</span><span class="literal string single">'Gtk'</span><span class="punctuation">,</span> <span class="literal string single">'3.0'</span><span class="punctuation">)</span><span class="whitespace"> -</span><span class="keyword namespace">from</span> <span class="name namespace">gi.repository</span> <span class="keyword namespace">import</span> <span class="name">Gtk</span><span class="whitespace"> - - -</span><span class="keyword">class</span> <span class="name class">MainClass</span><span class="punctuation">(</span><span class="name">Gtk</span><span class="operator">.</span><span class="name">Window</span><span class="punctuation">):</span><span class="whitespace"> - -</span> <span class="keyword">def</span> <span class="name function magic">__init__</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name builtin">super</span><span class="punctuation">(</span><span class="name">MainClass</span><span class="punctuation">,</span> <span class="name builtin pseudo">self</span><span class="punctuation">)</span><span class="operator">.</span><span class="name function magic">__init__</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">set_default_size</span><span class="punctuation">(</span><span class="literal number integer">600</span><span class="punctuation">,</span> <span class="literal number integer">400</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">connect</span><span class="punctuation">(</span><span class="literal string double">"destroy"</span><span class="punctuation">,</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">on_destroy</span><span class="punctuation">)</span><span class="whitespace"> - -</span> <span class="name">widget</span> <span class="operator">=</span> <span class="name">Gtk</span><span class="operator">.</span><span class="name">Frame</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">add</span><span class="punctuation">(</span><span class="name">widget</span><span class="punctuation">)</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">show_all</span><span class="punctuation">()</span><span class="whitespace"> - -</span> <span class="comment single"># Must be created >after< the widget is shown, else property 'window' will be None</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">mpv</span> <span class="operator">=</span> <span class="name">mpv</span><span class="operator">.</span><span class="name">MPV</span><span class="punctuation">(</span><span class="name">wid</span><span class="operator">=</span><span class="name builtin">str</span><span class="punctuation">(</span><span class="name">widget</span><span class="operator">.</span><span class="name">get_property</span><span class="punctuation">(</span><span class="literal string double">"window"</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">get_xid</span><span class="punctuation">()))</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">mpv</span><span class="operator">.</span><span class="name">play</span><span class="punctuation">(</span><span class="literal string double">"test.webm"</span><span class="punctuation">)</span><span class="whitespace"> - -</span> <span class="keyword">def</span> <span class="name function">on_destroy</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="punctuation">,</span> <span class="name">widget</span><span class="punctuation">,</span> <span class="name">data</span><span class="operator">=</span><span class="keyword constant">None</span><span class="punctuation">):</span><span class="whitespace"> -</span> <span class="name builtin pseudo">self</span><span class="operator">.</span><span class="name">mpv</span><span class="operator">.</span><span class="name">terminate</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="name">Gtk</span><span class="operator">.</span><span class="name">main_quit</span><span class="punctuation">()</span><span class="whitespace"> - - -</span><span class="keyword">if</span> <span class="name variable magic">__name__</span> <span class="operator">==</span> <span class="literal string single">'__main__'</span><span class="punctuation">:</span><span class="whitespace"> -</span> <span class="comment single"># This is necessary since like Qt, Gtk stomps over the locale settings needed by libmpv.</span><span class="whitespace"> -</span> <span class="comment single"># Like with Qt, this needs to happen after importing Gtk but before creating the first mpv.MPV instance.</span><span class="whitespace"> -</span> <span class="keyword namespace">import</span> <span class="name namespace">locale</span><span class="whitespace"> -</span> <span class="name">locale</span><span class="operator">.</span><span class="name">setlocale</span><span class="punctuation">(</span><span class="name">locale</span><span class="operator">.</span><span class="name">LC_NUMERIC</span><span class="punctuation">,</span> <span class="literal string single">'C'</span><span class="punctuation">)</span><span class="whitespace"> - -</span> <span class="name">application</span> <span class="operator">=</span> <span class="name">MainClass</span><span class="punctuation">()</span><span class="whitespace"> -</span> <span class="name">Gtk</span><span class="operator">.</span><span class="name">main</span><span class="punctuation">()</span> -</pre> +<span class="lineno"></span><span class="line"><span class="ch">#!/usr/bin/env python3</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">gi</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">import</span> <span class="nn">mpv</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">gi</span><span class="o">.</span><span class="n">require_version</span><span class="p">(</span><span class="s1">'Gtk'</span><span class="p">,</span> <span class="s1">'3.0'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="kn">from</span> <span class="nn">gi.repository</span> <span class="kn">import</span> <span class="n">Gtk</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">class</span> <span class="nc">MainClass</span><span class="p">(</span><span class="n">Gtk</span><span class="o">.</span><span class="n">Window</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="nb">super</span><span class="p">(</span><span class="n">MainClass</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">set_default_size</span><span class="p">(</span><span class="mi">600</span><span class="p">,</span> <span class="mi">400</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">"destroy"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_destroy</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">widget</span> <span class="o">=</span> <span class="n">Gtk</span><span class="o">.</span><span class="n">Frame</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">widget</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">show_all</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># Must be created >after< the widget is shown, else property 'window' will be None</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">mpv</span> <span class="o">=</span> <span class="n">mpv</span><span class="o">.</span><span class="n">MPV</span><span class="p">(</span><span class="n">wid</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">widget</span><span class="o">.</span><span class="n">get_property</span><span class="p">(</span><span class="s2">"window"</span><span class="p">)</span><span class="o">.</span><span class="n">get_xid</span><span class="p">()))</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">mpv</span><span class="o">.</span><span class="n">play</span><span class="p">(</span><span class="s2">"test.webm"</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="k">def</span> <span class="nf">on_destroy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">widget</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="bp">self</span><span class="o">.</span><span class="n">mpv</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main_quit</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># This is necessary since like Qt, Gtk stomps over the locale settings needed by libmpv.</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="c1"># Like with Qt, this needs to happen after importing Gtk but before creating the first mpv.MPV instance.</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="kn">import</span> <span class="nn">locale</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_NUMERIC</span><span class="p">,</span> <span class="s1">'C'</span><span class="p">)</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">application</span> <span class="o">=</span> <span class="n">MainClass</span><span class="p">()</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span> <span class="n">Gtk</span><span class="o">.</span><span class="n">main</span><span class="p">()</span> +</span></pre> </div> <div class="section" id="using-opengl-from-pygobject"> <h4>Using OpenGL from PyGObject</h4> |