命令行下怎樣遠程控制VLC播放

開宗明義,在Headless ( i.e. without a monitor or input device)的服務器(例如,各類嵌入式「玩具板子」)上如果架起VLC進行多媒體播放,怎樣才是最方便的控制方式?

同時,因為希望是可以腳本控制,所以最好是可以做到非互動式的指令。

如下是簡單的步驟:

使能VLC的Remote Control接口

VLC實際上擁有靈活強大的控制方式,由于我們上來就限定了Headless,那么命令行顯然是先決條件,VLC文檔中已經列出了支持的幾種Remote方式:

  • rc interface
  • ncurses interface
  • telnet interface

考慮到三者中rc應該是最強大和靈活的(ncurses需要configure,telnet更多的是互動型,何況rc本身就可以支持telnet訪問),我們就focus在rc 接口上。

使能方法非常簡單,調用vlc的時候,用:

cvlc -I rc --rc-fake-tty --rc-host <ip>:<port>

選項 意義
'-I rc' 啟動rc接口;
'--rc-fake-tty' 強迫將標準輸入當做tty
'--rc-host' 選定的監聽地址和端口

注意:如果需要局域網內訪問,請填寫本機ip,如果只要本機訪問,用localhost就可以了

得到如下信息(忽略那些錯誤吧,畢竟Headless么,很多接口對VLC來說都缺失,但是不影響使用)

cvlc -I rc --rc-fake-tty --rc-host localhost:8888
VLC media player 2.0.3 Twoflower (revision 2.0.2-93-g77aa89e)
[0x11eed70] inhibit interface error: Failed to connect to the D-Bus session daemon: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
[0x11eed70] main interface error: no suitable interface module
[0x11eed70] main interface error: no suitable interface module
[0x10858f0] main libvlc error: interface "globalhotkeys,none" initialization failed
[0x11eed70] [cli] lua interface: Listening on host "localhost:8888".

RC 接口指令

直接借助幫助文件, 可見還是非常強大的:

+----[ CLI commands ]
| add XYZ . . . . . . . . . . . . . . . . . . . . add XYZ to playlist
| enqueue XYZ . . . . . . . . . . . . . . . . . queue XYZ to playlist
| playlist . . . . . . . . . . . . . show items currently in playlist
| search [string] . . search for items in playlist (or reset search)
| sort key . . . . . . . . . . . . . . . . . . . . . sort the playlist
| sd [sd] . . . . . . . . . . . . . show services discovery or toggle
| play . . . . . . . . . . . . . . . . . . . . . . . . . . play stream
| stop . . . . . . . . . . . . . . . . . . . . . . . . . . stop stream
| next . . . . . . . . . . . . . . . . . . . . . . next playlist item
| prev . . . . . . . . . . . . . . . . . . . . previous playlist item
| goto, gotoitem . . . . . . . . . . . . . . . . . goto item at index
| repeat [on|off] . . . . . . . . . . . . . . toggle playlist repeat
| loop [on|off] . . . . . . . . . . . . . . . . toggle playlist loop
| random [on|off] . . . . . . . . . . . . . . toggle playlist random
| clear . . . . . . . . . . . . . . . . . . . . . clear the playlist
| status . . . . . . . . . . . . . . . . . . . current playlist status
| title [X] . . . . . . . . . . . . . . set/get title in current item
| title_n . . . . . . . . . . . . . . . . next title in current item
| title_p . . . . . . . . . . . . . . previous title in current item
| chapter [X] . . . . . . . . . . . . set/get chapter in current item
| chapter_n . . . . . . . . . . . . . . next chapter in current item
| chapter_p . . . . . . . . . . . . previous chapter in current item
|
| seek X . . . . . . . . . . . seek in seconds, for instance `seek 12'
| pause . . . . . . . . . . . . . . . . . . . . . . . . toggle pause
| fastforward . . . . . . . . . . . . . . . . . . set to maximum rate
| rewind . . . . . . . . . . . . . . . . . . . . . set to minimum rate
| faster . . . . . . . . . . . . . . . . . . faster playing of stream
| slower . . . . . . . . . . . . . . . . . . slower playing of stream
| normal . . . . . . . . . . . . . . . . . . normal playing of stream
| rate [playback rate] . . . . . . . . . . set playback rate to value
| frame . . . . . . . . . . . . . . . . . . . . . play frame by frame
| fullscreen, f, F [on|off] . . . . . . . . . . . . toggle fullscreen
| info . . . . . . . . . . . . . information about the current stream
| stats . . . . . . . . . . . . . . . . show statistical information
| get_time . . . . . . . . . seconds elapsed since stream's beginning
| is_playing . . . . . . . . . . . . 1 if a stream plays, 0 otherwise
| get_title . . . . . . . . . . . . . the title of the current stream
| get_length . . . . . . . . . . . . the length of the current stream
|
| volume [X] . . . . . . . . . . . . . . . . . . set/get audio volume
| volup [X] . . . . . . . . . . . . . . . raise audio volume X steps
| voldown [X] . . . . . . . . . . . . . . lower audio volume X steps
| adev [X] . . . . . . . . . . . . . . . . . . . set/get audio device
| achan [X] . . . . . . . . . . . . . . . . . set/get audio channels
| atrack [X] . . . . . . . . . . . . . . . . . . . set/get audio track
| vtrack [X] . . . . . . . . . . . . . . . . . . . set/get video track
| vratio [X] . . . . . . . . . . . . . . . set/get video aspect ratio
| vcrop, crop [X] . . . . . . . . . . . . . . . . set/get video crop
| vzoom, zoom [X] . . . . . . . . . . . . . . . . set/get video zoom
| vdeinterlace [X] . . . . . . . . . . . . . set/get video deintelace
| vdeinterlace_mode [X] . . . . . . . . set/get video deintelace mode
| snapshot . . . . . . . . . . . . . . . . . . . . take video snapshot
| strack [X] . . . . . . . . . . . . . . . . . set/get subtitles track
|
| vlm . . . . . . . . . . . . . . . . . . . . . . . . . load the VLM
| description . . . . . . . . . . . . . . . . . describe this module
| help, ? [pattern] . . . . . . . . . . . . . . . . . a help message
| longhelp [pattern] . . . . . . . . . . . . . . a longer help message
| lock . . . . . . . . . . . . . . . . . . . . lock the telnet prompt
| logout . . . . . . . . . . . . . . exit (if in a socket connection)
| quit . . . . . . . . quit VLC (or logout if in a socket connection)
| shutdown . . . . . . . . . . . . . . . . . . . . . . . shutdown VLC
+----[ end of help ]

如何訪問:

實際上,在一些討論中看到,如果只是需要本機訪問,更高效的辦法是借助一個unix socket文件,從而通過socket文件來訪問,簡單說就是將上面提到的-rc-host選項變成-rc-unix然后用一個文件作為輸入;

這樣可以用類似于形如下面指令的方法來模擬socket訪問 (作者以OSX為例,應當是沒有問題的):

echo normal | nc -U /Users/vlc.sock

我的例子,Debian中,nc似乎是不適用,更多人推薦用socat (需要安裝)

echo “pause” | socat – UNIX-CONNECT:/Users/vlc.sock

上面可以理解成將管道化之后的ECHO字符向這個Unix的Socket發送。可是在我這里死活都會遇到文件connection refused,作為菜鳥,實在無力解決,特別是看到有強者遇到類似問題交叉比較得出可能和特定版本的kernel stock存在bug有關之后,不管看不看得懂,我都理直氣壯的放棄了——直接轉向了上面提到的host:port的方案。

所以:

OSX

如果是互動式,直接 nc <host> <port>,就可以進入這個接口界面了:

$ nc 192.168.199.105 8888
VLC media player 2.0.3 Twoflower
Command Line Interface initialized. Type `help' for help.
$ status
( new input: file:///home/user/test.mp3 )
( audio volume: 256 )
( state playing )
$ info
+----[ Stream 0 ]
|
| Bitrate: 160 kb/s
| Type: Audio
| Channels: Stereo
| Sample rate: 44100 Hz
| Codec: MPEG Audio layer 1/2/3 (mpga)
|
+----[ end of stream info ]

如果不需要互動:

參考上節:

echo status | nc -w 0 <ip> <port>

-w是指示說該命令之后,立刻退出,實際上OSX下并不一定需要,只是為了和下面Linux對照一下。

LINUX

大致相同,區別只是在非互動式下,

echo status | nc -q 0 <ip> <port>

兩者區別只是一個用了' wait'之意,另外一個是' quit'罷了,想想還挺風趣....風趣個妹啊,這不是折騰跨平臺用戶嗎?!

結論

至此,該問題解決,不論是想在遠程控制無輸入(顯示)設備上的播放,還是想用腳本對其他線程的VLC播放進行自動化的干預控制,都不成問題了。

——怪不得有那么多人喜歡用VLC啊,我恍然。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容