python播放音乐同步歌词_python 根据歌词的时间(LRC文件),生成H5 audio按句播放器...
1、效果图
2、Python代码:
# coding=UTF-8
import codecs
import os
import re
# 正则校验是否为浮点数字
def is_number(num):
pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$')
result = pattern.match(num)
if result:
return True
else:
return False
# 解析歌词文件为结构化
def lrcParser(file_path):
new = []
f = codecs.open(file_path, 'r', encoding='utf-8')
lines = f.readlines()
f.close()
for i in xrange(len(lines)):
if i==0 :
continue
startTimes=lines[i-1].split("[")[1].split("]")[0].split(":")
endTimes=lines[i].split("[")[1].split("]")[0].split(":")
sentenceTxts =str(i-1)+":["+startTimes[1]+"] "+ lines[i-1].split("]")[1]
if is_number(startTimes[1]) and is_number(endTimes[1]) :
startPoint=-1;
if is_number(startTimes[0]):
startPoint=float(startTimes[0])*60+float(startTimes[1])
else:
startPoint = float(startTimes[1])
endPoint=float(endTimes[0])*60+float(endTimes[1])+0.5
new.append(str(startPoint )+","+str(endPoint)+","+sentenceTxts)
if (i + 1) == len(lines):
new.append(str(endPoint) + ",document.getElementById('audio1').duration-1," + (str(i)+":["+str(startTimes[1])+"] "+ lines[i].split("]")[1]))
else:
new.append("0,0,"+sentenceTxts)
return new;
# 生成每个歌词控制语句
def generateSentenceControl(lrcs):
controlList=[]
for i in xrange(len(lrcs)):
lines=lrcs[i].split(",")
control=''+lines[2]+'\n'
controlList.append(control)
return controlList;
def generateHtml(lrcs_arry,mp3):
html=[]
html.append(u"\n");
html.append(u"\n");
html.append(u"\n
");
html.append(u"\n ");
html.append(u"\n ");
html.append(u"\n
"+mp3+"");
html.append(u"\n
html.append(u"\n button {");
html.append(u"\n margin: 5px;");
html.append(u"\n }");
html.append(u"\n ");
html.append(u"\n");
html.append(u"\n
");
html.append(u"\n
");
html.append(u"\n\n\n")
for i in xrange(len(lrcs_arry)):
html.append(lrcs_arry[i])
html.append(u"\n
html.append(u"\n// https://stackoverflow.com/questions/19355952/make-html5-video-stop-at-indicated-time");
html.append(u"\nvar myAud=document.getElementById(\'audio1\');");
html.append(u"\nfunction setCurEndTimeAndPlay(startTime,endTime){");
html.append(u"\n// 设置当前时间");
html.append(u"\nmyAud.currentTime=startTime;");
html.append(u"\nvar pausing_function = function(){");
html.append(u"\n// 播放");
html.append(u"\nmyAud.play();");
html.append(u"\n if(myAud.currentTime >= endTime) {");
html.append(u"\n myAud.pause();");
html.append(u"\n // remove the event listener after you paused the playback");
html.append(u"\n myAud.removeEventListener(\'timeupdate\',pausing_function);");
html.append(u"\n }");
html.append(u"\n};");
html.append(u"\nmyAud.addEventListener(\'timeupdate\', pausing_function); ");
html.append(u"\n } ");
html.append(u"\n ");
html.append(u"\n");
html.append(u"\n");
return html
def save(html,file):
f = codecs.open(file, 'w', encoding='utf-8')
f.writelines(html)
f.close()
if __name__ == '__main__':
path = u'/Users/jifeng/Downloads/学习音乐/';
mp3=u'红玫瑰.mp3';
lrc_file = path + u'红玫瑰.lrc' # 放原txt文件的目录,注意有的字符需要转义
result = path + u'红玫瑰.lrc.html' # 结果文件名
lrcs_arry = lrcParser(lrc_file) # 解析歌词文件
controlList = generateSentenceControl(lrcs_arry) # 生产行控制语句
html=generateHtml(controlList,mp3)
save(html,result)
3、获取歌词LRC
4、代码下载地址
python播放音乐同步歌词_python 根据歌词的时间(LRC文件),生成H5 audio按句播放器...相关推荐
- 用JS实现歌词与播放音乐同步
用JS实现歌词与播放音乐同步 第一步:把歌词解析成JS对象 参看我的上一篇文章:用JS解析LRC格式的歌词 解析后的歌词写到页面的一个列表中,效果如下: 主要HTML代码: <!-- 播放器 - ...
- Python先生,你好!(4)——npz、npy文件生成与读取
Python先生,你好!(4)--npz.npy文件生成与读取 (一)前 言 (二)np.save()函数 (1)生成一个数组 (2)储存数组 (三)np.savez()函数 (1)生成两个数组 (2 ...
- python播放音乐同步歌词_使用Python下载歌词并嵌入歌曲文件中的实现代码
使用python扫描本地音乐并下载歌词 这次这个真的是干货哦,昨晚弄了半晚上,,,,从8点吃完饭就开始写,一直到了快12点才弄好,,,新手,伤不起呀.... 先简单的说下吧,百度提供了一个音乐搜索的a ...
- python程序多次运行_Python内怎么使同一个.py文件多次运行?
原博文 2020-08-03 15:57 − 当在多个客户端同时连接一个服务端时需要同时运行多个客户端所在的.py文件,但默认上我们运行了一次后如果再想运行这个.py文件,则被要求停下当前运行的.py ...
- python中bttext什么意思_Python实现解析Bit Torrent种子文件内容的方法
有人会 python语言 对BT种子解析 吗bt的客户端,本来就有纯python编写的. 你找一个下载就可以了. 我印象中bittorrent就是python编写的. python开发很方便,也很快. ...
- Python基础教程(八):日期和时间、文件I/O、异常处理
Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...
- python播放音乐同步歌词_python终端播放音乐同定制步显示本地或网络歌词
文章来源:淘论文网 发布者:毕业设计 浏览量: #!/usr/bin/python # -*- encoding:utf-8 -*- #============================== ...
- python播放音乐同步歌词范晓萱_范晓萱同步的歌词
你是不是在找范玮琪的这首同步的歌词?下面是学习啦!小编帮你整理了这首歌词的相关的资料希望对你有用! 同步的歌词 同步-(电影<陪安东尼度过漫长岁月>主题曲)-范晓萱 相隔两地的时空 默契对 ...
- python播放音乐同步歌词_linux 下 python 调用 mplayer 解析歌词同步播放显示
标签: 加载同目录同名歌词同步显示 #!/usr/bin/python # -*- coding: utf-8 -*- import sys, os, time, subprocess, re, ch ...
最新文章
- arch linux引导不启动_Linux 内核源代码的目录结构
- python什么时候进入中国-python什么时候发明的
- Linux系统入门之如何安装Linux系统
- c++动态绑定和动态绑定
- SAP Spartacus CMS Component的lazy loading懒加载方式
- Bellman_Ford算法(求一个点到任意一点的最短距离)
- 输入法候选词排列的两种新方式
- 归并排序算法(java实现)
- 刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
- jemalloc疑似内存泄漏分析
- 史上最新最全的ADB命令行
- Oracle中动态SQL详解(EXECUTE IMMEDIATE)
- 基于51单片机的LED彩灯控制器设计
- 汇编语言里 eax ebx ecx edx esi edi ebp esp
- arcgis的炸开多边形功能
- 如何找到一个应用的源代码
- Cholesky分解(Matlab代码实现)
- “世界杰出女科学家成就奖”组委会宣布2021年度五位获奖者
- 编写简单的在线考试系统
- 探秘野三坡红叶大峡谷