VLC — python-vlc, Command Line and RC Interface
[[[appendices.vlc]]]
Overview
[[[appendices.vlc#overview]]]
What was measured of the user's VLC 3.0.23 on Windows 11, for #319 ([[plugins#in-app-player]]). Two uses were measured, and one of them was dropped:
- libvlc in the player dock, through python-vlc (section 5): what ships. python-vlc's import behavior, the events and their threads, the snapshot that serves the paused still, what holds a file, and replaying after the end.
- VLC launched as a process of its own and followed over its RC interface (sections 1–4): built in #319 and removed in the same issue, once it was clear a separate window could carry none of the player's overlays. Kept for the record, since the facts were paid for and the RC route may be wanted elsewhere one day.
Everything was read with scratch scripts over three 8-second clips named a one, b#two and c_ünï, in a folder
with a # in its name. Transcripts are verbatim bar the file URI's folder, shortened to F:/. macOS and Linux are
untested.
1. Launching
[[[appendices.vlc#launch]]]
vlc --no-one-instance --no-playlist-enqueue --qt-continue=0 --no-loop --no-repeat
--extraintf=rc --rc-host=127.0.0.1:<port> --rc-quiet <playlist.m3u8>
--no-one-instanceopens a window of its own even when a VLC runs; without it the playlist would be handed to that VLC and the RC socket never opens.--no-playlist-enqueuekeeps the playlist from joining another's.--qt-continue=0(Never) turns off VLC's own continue where you left off prompt, so it never fights the position the playlist starts each entry at.--no-loop --no-repeat. Left out, the playlist wraps: after the last entry ends, entry 1 starts again. With them the last entry ends in stopped.--rc-quietis a Windows option (Do not open a DOS command box interface); it is passed only there.- The playlist is UTF-8
.m3u8:#EXTM3U, then per entry#EXTVLCOPT:start-time=<seconds>and the file's URI (Path.as_uri(), which escapes#and spaces and spells a UNC path asfile://server/share/...). The start time is honored: an entry given2.5reportedget_time3 a second and a half aftergoto. - Cold start. The RC port opened 12 s after the launch on the first run, while VLC rebuilt its plugin cache,
and 0.27 s on later ones. Waiting for it has to be generous (30 s, as
VlcToolallows the version read).
2. The playlist file is one element until it plays
[[[appendices.vlc#playlist-file]]]
Neither --no-playlist-autostart nor --start-paused can hold playback while the clicked entry is chosen. VLC
expands a playlist file only when it plays it: with autostart off, goto 2 answers Playlist has only 1 element, and
with --start-paused the first entry opens paused and the goto is lost. So VLC starts on entry 1 and goto <n>
moves it once the socket connects. Entry 1 can be heard briefly before that, for as long as the port takes to open.
Nothing it plays is recorded: the tracker records an entry only after it has been seen to be the one asked for.
A goto sent too early is lost: connecting warm, within a fraction of a second, RC can answer before VLC has
expanded the file, and the goto is accepted (returned 0) but does nothing. The tracker therefore sends it again on
each poll until the entry asked for is the one named.
3. The RC protocol
[[[appendices.vlc#rc-protocol]]]
- No greeting, no prompt. With
--rc-quietnothing is sent on connect, and no>prompt follows an answer. - Lines end in
\r\n. A command is a line ending in\n; several can be sent at once and answer in order. - Every command ends its answer with
<command>: returned 0 (no error).get_time,get_lengthandis_playingare the exceptions: they answer a bare integer. - Asynchronous
status change: ( ... )lines are mixed in anywhere, before or after an answer: a new input, a play-state number, the volume, a pause,( time: 6s )after a seek. The numbers afterplay state:are not stable across a pause and a seek (3while playing,2: Playafter a resume,4: Endas an entry ends), so they are not read. goto <n>is 1-based, the order of the playlist.seek <seconds>takes whole seconds.get_timeandget_lengthanswer whole seconds.quitcloses the socket (measured). VLC's window being closed was not measured; the tracker treats any closed socket as VLC having gone.
The poll
[[[appendices.vlc#rc-poll]]]
One request, status\nis_playing\nget_time\nget_length\n, answers in three shapes. Playing, entry 2:
status change: ( new input: F:/b%23two.mp4 )
status change: ( play state: 3 )
status: returned 0 (no error)
1
3
8
(the 1 is is_playing, then get_time 3, get_length 8). Paused: the answer names no input, and says so with
a line that is no status change:
status change: ( pause state: 4 )
Type 'pause' to continue.
status: returned 0 (no error)
0
4
8
Stopped, whether by stop or by the last entry ending: a stop state: 5 line, no input, and zeros.
status change: ( audio volume: 243 )
status change: ( stop state: 5 )
status: returned 0 (no error)
0
0
0
So the parser reads: the last new input: line is the input; a stop state: 5 line after it clears it and
means stopped; the three bare integers are is_playing, time and length. An answer with no input and not stopped
is a paused one, whose input stays the one before. An entry that has ended and been replaced shows as the next input,
with the new input line twice in one answer.
The input URI
[[[appendices.vlc#input-uri]]]
new input: repeats the playlist's URI as VLC holds it: file:///C:/.../clips%20%231/b%23two.mp4 and
.../c_%C3%BCn%C3%AF.mp4, percent-encoded as Path.as_uri() wrote them. It is matched to an entry by decoding it
(urllib.parse.unquote, url2pathname) and comparing the paths, case-folded on Windows. A UNC path was not measured;
the same decoding spells file://server/share/x as //server/share/x.
4. Behaviors worth knowing
[[[appendices.vlc#behaviors]]]
- An entry ending moves on by itself, and the poll sees the next input. The last position seen of the entry left is within one poll interval of its length, which is why reaching the end is judged with a tolerance, as the in-app player's is ([[field-schema#watch-progress]]).
prev(VLC's button;nextis the same) changes the input the same waygotodoes.seekandpausetoggle at once; apausewhile paused resumes.- A closed socket is how the tracker learns VLC has gone; it makes its last flush there.
5. python-vlc and libvlc in-process
[[[appendices.vlc#python-vlc]]]
python-vlc 3.0.21203, LGPL-2.1+ by its metadata, is one file of ctypes bindings; it ships no VLC code and loads the
user's own libvlc. The agent reaches it only through rehuco_core.VlcLibrary, never by a plain import vlc,
because of what the import does.
The import is the search
[[[appendices.vlc#python-vlc-import]]]
find_lib() runs at module import, and its outcomes were measured in child processes:
PYTHON_VLC_LIB_PATHset to the fulllibvlc.dllpath: a clean import, no search, no chdir,vlc.dll._nameis that path. This is the only way the app imports it.- No variable, VLC installed: found through the registry, PATH or
%ProgramFiles%\VideoLan\VLC; while the DLL loads, python-vlcos.chdirs the whole process into VLC's folder and back. A second thread saw the VLC folder as its working directory during the import. In a process with pool threads that is a race. - No variable, no VLC findable (registry key absent, no PATH entry,
PROGRAMFILESpointing nowhere): it ends inctypes.CDLL(".\libvlc.dll")relative to the working directory, and raisesFileNotFoundErrorfor<cwd>\libvlc.dll. PYTHON_VLC_LIB_PATHnaming a file that does not load:logger.errorandsys.exit(1)from inside the import.PYTHON_VLC_MODULE_PATHnaming no folder: the same.- No
ProgramFilesin the environment:KeyErrorfrom inside the import. vlc.Instance("--no-such-option")answersNone, not an exception (VLC prints unknown option to stderr).
So the proxy sets PYTHON_VLC_LIB_PATH to the library the Tools page resolved, imports inside
try/except BaseException, and a child process has imported the same library first, so a library that kills its
process kills the probe, not the app.
Events and their threads
[[[appendices.vlc#python-vlc-events]]]
Measured with set_hwnd on a native QWidget, mouse and key input off, audio muted. Each event arrives on a
libvlc thread of its own (python-vlc's callbacks show as Dummy-N, a new one per event), except two that are
raised synchronously on the thread that calls: MediaPlayerSnapshotTaken inside video_take_snapshot, and
MediaPlayerStopped inside stop(). The order on play() of a local file:
play() -> 0 (returns at once)
Opening +1 ms
Buffering (many, with a percent)
LengthChanged +15 ms
Playing +17 ms
Vout +215 ms first picture
PositionChanged, TimeChanged then about every 500 ms
- The snapshot is synchronous and works while paused:
video_take_snapshot(0, path, 0, 0)returned0after 42 ms playing and 19 ms paused, the file written and the event raised before the call returned. So the paused still is taken on the pause, before the stop that lets the file go. - What holds the file: renaming the clip's folder failed (Access is denied) while playing and while
paused, and succeeded right after
stop(), with the media still set, and afterset_media(None). Renaming the file itself succeeded even while paused (VLC opens it with delete sharing), which is not the case that matters: the app renames folders. So a paused video in the dock is a stopped player, as it is with Qt Multimedia. stop()cost on the GUI thread: 40–44 ms here (80–115 ms in #315's spike under a stream of time changes).- After the end:
EndReached, thenVoutandStopped;get_state()isEnded. Aplay()in that state returned0and did nothing (stillEndeda second later).stop()thenplay()replays from the start. set_time()takes effect at once and raises itsTimeChangedon the calling thread.- A snapshot draws on the picture unless told not to: VLC's on-screen display says Snapshot taken and shows a
thumbnail of it over the video. The instance is made with
--no-osdand--no-snapshot-preview(and--no-video-title-show), so nothing of VLC's is ever drawn on the dock's surface. - A native window's picture can be read in memory on Windows:
PrintWindow(hwnd, dc, PW_RENDERFULLCONTENT)returned libvlc's Direct3D picture -- playing and paused, on a monitor and off every monitor -- in 5--11 ms for 640x400. Measured while the engine still drew into a native window; superseded by section 6, where there is no window to read and the still is the last frame. vlc.EventTypemembers are not integers: they are ctypes enumerations,int(member)raises, and the number is inmember.value. Found by the first live run of the engine, after a fake built onIntEnumhad hidden it; the engine's tests now fake the members the same way.- A folder rename is refused while libvlc plays or pauses, in the dock too (the live run). The view's paused is stopped rule was applied to libvlc first, and reopening and seeking landed a few frames off on resume (seen by the owner), so under libvlc a pause now holds the file, paused on its frame, and the rename coordinator's before-rename call is what releases it.
- A mute is the audio output's (#574): libvlc takes
audio_set_muteonly while its audio output lives, and the state outlasts a player, carried by the instance. Muting beforeplay()leaked sound at the start of a paused load (the owner heard seconds of it from a background tab), and unmuting afterstop()was lost: the next video played silent (measured:audio_get_mute()read 1 while it played). Only an unmute made while still paused stuck. :no-audioon a media decodes no audio (#574):audio_get_track()is -1 against 1 without it. The track count still lists the file's tracks, so it proves nothing.:start-time=<s>opens a media at a position (23.49 s for 23.5 s asked). A seek's target is reported at once:get_time()reads it straight afterset_time(), ahead of any frame of it, so "the frame at the position" cannot be told by asking, and the file's first frame, still queued, passes for it whenever the GUI thread is busy (#613: a restored clip on its opening frame). So a paused load opens at its position. It opens:no-audio, played before its still it opens again with its audio there, and the engine never mutes.
Drawing over the surface
[[[appendices.vlc#overlay]]]
What overlays drawn over the maximized player's picture would have met: libvlc drew into a native child window, and Windows composes native windows, not Qt widgets. Measured with a label raised over a playing surface and a screen grab of the window:
| Overlay | Result |
|---|---|
| An ordinary Qt widget, sibling of the surface | a black hole: the video is cut out under it and the label is not drawn |
| A sibling with a native window of its own, opaque | drawn over the video, opaque |
The same native sibling, translucent (WA_TranslucentBackground) |
not shown at all |
| A frameless, translucent top-level window placed over the surface | drawn over the video with transparency |
This is why libvlc renders into memory (section 6): the picture is then an ordinary widget and the table does not
apply to it. It still describes QVideoWidget, which holds a native window too. The player's I, T and N are
panels beside the picture instead (owner, #574): the video shrinks to make room and nothing is drawn on it, so the Qt
engine keeps its QVideoWidget and never pays a frame conversion on the GUI thread
([[plugins#player-maximized]]).
6. Rendering into memory
[[[appendices.vlc#memory-rendering]]]
libvlc 3 has two outputs an application can use: a native window of its own (set_hwnd, set_nsobject,
set_xwindow) and memory (video_set_callbacks with video_set_format_callbacks); the OpenGL callbacks are
libvlc 4's. The dock uses memory, so that its picture is an ordinary widget.
- The format callback cannot be written through python-vlc's prototype:
VideoFormatCbdeclares the chromac_char_p, which ctypes hands over as copied bytes. A prototype of our own withPOINTER(c_char), cast to python-vlc's type where it is passed, letsmemmovewriteRV32into it (measured: the callback then saw640x384 I420for a 640x360 clip and the frames came in 32-bit). - The picture size libvlc gives is the decoder's, padded -- 384 lines for 360 -- so the frame is cropped to
video_get_size(0)once that is known (it is0x0until the video plays). - One picture in the pool (
setupreturns 1) means libvlc locks, writes, unlocks and displays each frame in turn, so copying the buffer inside the display callback, on libvlc's thread, is safe; the copy crosses to the GUI thread as aQImagethrough a queued signal and is painted byQPainter, scaled to fit. - Audio, the clock and subtitles stay libvlc's: only the video output module is replaced. Frames are displayed at
their presentation time against libvlc's audio clock; subtitles and anything else the video-output core blends
arrive inside the frames (verified with an
.srtbeside a clip: its text was in the painted frame), which is why the instance is made with--no-osd. - The cost, measured on a Ryzen 5 5600 with a 1080p60 clip in a window maximized on a 3440x1440 monitor (2752x1081 of picture): 59.3 fps, the paint 2.6 ms a frame, the whole process -- decoding included -- 67 % of one core (6 % of the machine); 100 % of one core with smooth scaling, or with libvlc scaling to the window itself (an 11 MB buffer). Memory is one buffer of the picture's size (8 MB at 1080p) and the copies in flight.
- The surface scales smoothly (#574), as the still a pause shows does: unsmoothed, a scale near 1 drew some rows and columns twice, which the owner saw as a doubled, jagged picture that got better on pause. Measured again with the player maximized (2734x1007 of picture), smoothed against unsmoothed: 60.0 fps both, the paint 3.0 ms against 2.9 ms, the whole process 132 % of one core against 74 % (about 11 % of the machine against 6 %).
- What it changes for the player: no native window, so any widget can be drawn over the picture; the still of a
pause is the last frame, with no snapshot and no
PrintWindow; and the engine is the same on every OS.
Subtitles
[[[appendices.vlc#subtitles]]]
Measured with libvlc 3.0.23 over python-vlc, memory rendering, on a 20 s clip with clip.vtt and clip.en.srt
beside it (#618).
- A subtitle file beside the video is found without being asked (
sub-autodetect-file), both of the two above, and listed asTrack 1andTrack 2 - [English]: the name is libvlc's, never the file's. The first is selected. - The list fills in steps.
video_get_spu_description()is empty asplay()returns, held two of the four tracks 50 ms later in one run of three, and all four by 100 ms (read every 50 ms, with memory rendering as the player uses it); on a cold first start it was once still empty after 2 s. So the player reads it until two reads agree, never once at load, and asks again at each key press. It starts with(-1, b"Disable"), whichvideo_set_spu(-1)selects; the engine does not rely on it and puts off first itself. - A change shows on a playing video and not on a paused one, but for turning the subtitles off. Waited 3 s, a
track switched on over a paused picture changed no pixel;
set_time(get_time())then changed tens of thousands and left the player paused. A paused change is therefore a seek to the same position, and the frame it brings is the new still. - The language is in the description, as an English name:
Track 3 - [French]forclip.fr.srt, from the file name; a bareclip.vttisTrack 1, with none.media.tracks_get(), which would give ISO codes, segfaulted here, so the description is what the player reads. libvlc's table of language names is not exposed; the Videos › Player page ships its own list of ISO 639-1 names to choose from. --sub-language=frand--no-spuare libvlc's own way of choosing at start (French was selected; none was), but they apply to the instance, not a rehu, so the player chooses itself once the list is settled ([[plugins#in-app-player]]).