linux终端播放音乐歌词同步显示
- 作者: 隔壁老栗
- 来源: 51数据库
- 2022-08-12
#!/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 '\033[4;0H\033[K\tNo 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'(\d\d)\s*:\s*(\d\d)',tm.strip(' [')).groups()
pos = 60*int(time[0]) + int(time[1])
lrc[pos] = line[-1]
except Exception, e:
#print '\033[4;0H\033[K\tLrc fromat error'
#lrc_fp.close()
#return {}
pass
#lrc_fp.close()
return lrc
def main(song):
print '\033[2J\033[2;0H\tPlaying %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 '\033[9;0H\tFailed 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 '\033[15;0H\ttotal time: %d'%tot_time
break
print '\033[?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 '\033[6;0H\033[K\tCurrent %d / Total %d'%(cur_time, tot_time)
print '\033[1;0H', '-'*(tot_time/jump)
print '\033[1;0H', '='*(cur_time/jump)
print '\033[7;0H', '-'*(tot_time/jump)
print '\033[7;0H', '='*(cur_time/jump)
if tot_time <= cur_time:
print '\033[8;0H\033[K\tquit'
break
print '\033[4;0H\033[K\t%s'%lrc[cur_time]
except Exception, e:
#print '\033[17;0H\tERROR:', e
pass
elif 'E' == cur_char:
print '\033[8;0H\033[K\tquit'
break
p.wait()
print '\033[10;0H\033[?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])
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
