linux终端播放音乐歌词同步显示
发布时间:2020-05-25 00:49:38 所属栏目:Python 来源:互联网
导读:linux终端播放音乐歌词同步显示
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #!/usr/bin/python
# -*- coding: utf-8 -*-
#=========================================
# Filename : music.py
# Filetype : Python
# Author : Colben
# Create : 2015-06-30 22:17:15
#=========================================
import sys,os,time,subprocess,re,chardet
def load_lrc(lrc_file):
try:
lrc_contains = open(lrc_file,'rb').read()
encoding = chardet.detect(lrc_contains)['encoding']
if 'utf-8' != encoding:
lrc_contains = lrc_contains.decode(encoding).encode('utf-8')
except Exception,e:
print ' 33[4;0H 33[KtNo lrc or in encoding found'
return {}
lrc = {}
for eachline in re.split(r'n',lrc_contains):
line = re.split(r']',eachline)
if 1 < len(line):
for tm in line[0:-1]:
try:
time = re.match(r'(dd)s*:s*(dd)',tm.strip(' [')).groups()
pos = 60*int(time[0]) + int(time[1])
lrc[pos] = line[-1]
except Exception,e:
#print ' 33[4;0H 33[KtLrc fromat error'
#lrc_fp.close()
#return {}
pass
#lrc_fp.close()
return lrc
def main(song):
print ' 33[2J 33[2;0HtPlaying %s ...'%song[:60]
p = subprocess.Popen('mplayer %s 2>/dev/null'%song,stdout = subprocess.PIPE,shell = True)
while True:
match = re.match(r'A:.*[d:.()]* of (d+)',p.stdout.read(30))
if None != p.poll():
print ' 33[9;0HtFailed to recognize file format .'
return 1
if not match:
output = p.stdout.readline()
else:
tot_time = int(match.group(1)) - 1
for jump in range(1,10):
if 70 >= tot_time/jump:
break
#print ' 33[15;0Httotal time: %d'%tot_time
break
print ' 33[?25l'
lrc = load_lrc(os.path.splitext(song)[0] + '.lrc')
while True:
cur_char = p.stdout.read(1)
if 'A' == cur_char:
try:
cur_time = int(p.stdout.read(5)[1:])
print ' 33[6;0H 33[KtCurrent %d / Total %d'%(cur_time,tot_time)
print ' 33[1;0H','-'*(tot_time/jump)
print ' 33[1;0H','='*(cur_time/jump)
print ' 33[7;0H','-'*(tot_time/jump)
print ' 33[7;0H','='*(cur_time/jump)
if tot_time <= cur_time:
print ' 33[8;0H 33[Ktquit'
break
print ' 33[4;0H 33[Kt%s'%lrc[cur_time]
except Exception,e:
#print ' 33[17;0HtERROR:',e
pass
elif 'E' == cur_char:
print ' 33[8;0H 33[Ktquit'
break
p.wait()
print ' 33[10;0H 33[?25h'
return 0
if '__main__' == __name__:
if 2 != len(sys.argv) or not os.path.isfile(sys.argv[1]):
print 'nUsage:',sys.argv[0],'{exist music filename}n'
else:
main(sys.argv[1])
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
