问题
I want to know if it is possible to retrieve the now playing info (name of file, time elapsed, etc.) for VLC from the command line.
I want to then use Java to use this info somewhere.
One possibility might be to use JNA, but not sure how that would work.
I searched everywhere but it only tells me how to start a file from command line.
回答1:
Apparently VLC comes with a small Http server which can be used to send commands to the player. You launch the server by issuing % vlc -I http (--http-src /directory/ --http-host host:port)
where --http-src
and --http-host
are optional. Using just vlc -I http
VLC listens on 127.0.0.1:8080
on my system.
You can then retrieve player status and track information by issuing a GET request to http://127.0.0.1:8080/requests/status.xml
, which returns an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
...
<time>75</time>
<volume>512</volume>
<length>326</length>
<rate>1</rate>
...
<state>playing</state>
<position>0.23139409720898</position>
<information>
<category name="meta">
<info name="artwork_url">file:///path/to/art/work</info>
<info name="genre">Genre</info>
<info name="album">Album Name</info>
<info name="publisher">Publisher</info>
<info name="title">Track Title</info>
<info name="track_number">1</info>
<info name="filename">File Name</info>
<info name="artist">Artist Name</info>
</category>
<category name="Stream 0">
<info name="Bitrate">128 kb/s</info>
<info name="Type">Audio</info>
<info name="Channels">Stereo</info>
<info name="Sample rate">44100 Hz</info>
<info name="Codec">MPEG Audio layer 1/2/3 (mpga)</info>
</category>
</information>
<stats>
...
</stats>
</root>
Where the track info is filled in from the tags present in the audio file.
You can even send commands to VLC to control playback using this method. Here is the full Http API reference: VLC Http Requests.
You can interface with the Http server by using one of the Http clients available for Java, such as Apache HttpClient, and you can parse the xml returned from your requests using something like JAXP, though I'm sure that alternatives to both of these exist.
来源:https://stackoverflow.com/questions/23436861/get-now-playing-info-from-vlc-command-line