BV1AL 之無所不記

2021-04-25

辨識是否在WSL或正常Linux環境

 例用python來播放網路影音的script, 為了在標準Linux或是在WSL環境都能通用,
首先需要辨識環境,再啟用對應的播放軟體。

我們可以用 os.uname() 或 platform.uname() 來判別是在正常Linux環境或是在WSL
下的Linux環境,再呼叫指定的播放器。

在WSL環境的Linux可以直接執行Windows的播放器,例如VLC, 它在WSL裡的路徑是
/mnt/c/Program\ Files/VideoLAN/VLC/vlc.exe

platform.uname()或是os.uname() 如果是在WSL裡的linux執行會列出:
uname_result(system='Linux', node='MY_LAPTOP', release='5.4.72-microsoft-standard-WSL2', version='xxxxxx', machine='x86_64')

import csv, platform, os
from sys import argv, exit

LIST="""\
Classic Portland,http://allclassical-ice.streamguys.com/ac96kmp3
Classic NL,http://playerservices.streamtheworld.com/pls/CLASSICFM.pls
Clarinet obbo,http://213.141.131.10:8002/clarinet
Caprice cello, http://79.111.14.76:8002/cello
Chamber music,http://chambermusic.stream.publicradio.org/chambermusic.mp3
WMNF,http://stream.wmnf.org:8000/wmnf_hd3
Taiwan PBS,http://stream.pbs.gov.tw:1935/live/mp3:PBS/playlist.m3u8
ICRT,http://live.leanstream.co/ICRTFM-MP3"""

MENU = csv.reader(LIST.split('\n'))
n = 1

if len(argv) == 1:
   for x,y in MENU:
       if n%3 == 0:
          END = '\n'
       else:
          END = '\t'
       print('\033[32;1m'+str(n)+'.\033[m' +x,end=END)
       n+=1
   print('\n')
   exit(1)
else:
   for x,y in MENU:
       if argv[1].isdigit():
           if n == int(argv[1]):
               MusicLink = y
               break
           else:
               n+=1

if 'WSL' in platform.uname().release:
      PLAYER = '/mnt/c/Program\ Files/VideoLAN/VLC/vlc.exe'
else:
      PLAYER = 'mpv'

os.system('%s %s '%(PLAYER,MusicLink))

 

標籤: , , ,

0 Comments:

張貼留言

<< Home