更新魅族M3播放列表脚本文件

来源:互联网 发布:win10需要优化软件吗 编辑:程序博客网 时间:2024/06/05 04:06

喜欢魅族,谈不上粉,前不久淘到一部喜欢的二手M3,用了一段时间发现M3添加修改播放列表非常不方便,添加删除音乐之后播放列表都得重新添加,今天突然觉得可以用python写一个脚本,以后M3中音乐有更新直接用脚本更新播放列表那就方便多了。

先研究播放列表文件“.M3U”,播放列表文件可以直接用文本编辑器打开,每一首音乐由两行组成,而且分别用了不同的字符编码:

1)第一行使用的是UTF-8编码(可通过前部标识获知“#[UTF8FULLPATH]”),后部分为音乐文件在M3中的存放路径;

2)第二行使用的是GBK编码,为音乐文件在M3中存放的路径;

测试发现,上面中两行数据,只存在任意一行都能在M3中识别出播放列表,如果两行都存在则会以第一行为准,即使第二行路径是错误的也不会出现什么问题。这里就感到很奇怪了为什么要在这种播放列表文件中使用两种字符编码呢???(希望大家不吝赐教~)

明白了上面的事,下面要做的事就简单了,只需要把M3的音乐遍历一次,然后把每个音乐都按照相应格式写入文件就搞定;

代码执行流程:

(1)找到M3所在盘符,获取MUSIC、PLAYLISTS文件夹路径;(我的M3设置了盘符标志为‘魅族M3’,使用代码时需要根据直接的名称修改)

      #搜索魅族M3盘符</span>
<span style="white-space:pre"></span>data = os.popen("wmic VOLUME WHERE Label='魅族M3' GET DriveLetter").read()
<span style="white-space:pre"></span>pos = data.index(":")<span style="white-space:pre"></span>pathmusic = data[pos-1:pos+1] + "\MUSIC"<span style="white-space:pre"></span>pathlists = data[pos-1:pos+1] + "\PLAYLISTS"

(2)搜索MUSIC文件夹下的文件夹,为每一个文件夹建立一个播放列表

<span style="white-space:pre"></span>#为MUSIC下的每一个文件夹建立播放列表</span></span>
<span style="white-space:pre"></span></span>listname = i + '.M3U'<span style="white-space:pre"></span>file = open(os.path.join(pathlists,listname),'wb+')

(3)遍历每一个文件夹下的音乐文件(.mp3\.wma\.flac文件),将音乐文件添加到播放列表文件中;

#查询音乐文件
     x = j.index(".",len(j)-5,len(j))
     #判断音乐文件
     if j[x:len(j)]=='.mp3' or j[x:len(j)]=='.wma' or j[x:len(j)]=='.flac':
pos = pathdir2.index(":")
          # print(pathdir2)
pathfile = pathdir2[pos+1:len(pathdir2)] + '\r\n'
          # print(pathfile)
          a = filetop + pathfile
          b = pathfile
          a_utf = a.encode('utf-8')
          b_gbk = b.encode('gbk')
          file.write(a_utf)
          file.write(b_gbk)

(4)关闭文件

file.close()

附完整代码:

#!/usr/bin/env python# -*- coding:utf-8 -*-' 魅族M3播放列表生成脚本 '_author_ = '凡人'import osimport sysfiletop = '#[UTF8FULLPATH]'#搜索魅族M3盘符data = os.popen("wmic VOLUME WHERE Label='魅族M3' GET DriveLetter").read()try:    pos = data.index(":")    pathmusic = data[pos-1:pos+1] + "\MUSIC"    pathlists = data[pos-1:pos+1] + "\PLAYLISTS"except ValueError:    print("\nNO FOUND DIVICE M3!")    sys.exit()#搜索MUSIC文件下存在的文件夹for i in os.listdir(pathmusic):    pathdir = os.path.join(pathmusic,i)       if os.path.isdir(pathdir):        #为MUSIC下的每一个文件夹建立播放列表        listname = i + '.M3U'        file = open(os.path.join(pathlists,listname),'wb+')        for j in os.listdir(pathdir):            #对二层文件夹判断            pathdir2 = os.path.join(pathdir,j)            if os.path.isdir(pathdir2):                listname2 = j + '.M3U'                file2 = open(os.path.join(pathlists,listname2),'wb+')                for k in os.listdir(pathdir2):                    pathdir3 = os.path.join(pathdir2,k)                    #查询音乐文件                    x = k.index(".",len(k)-5,len(k))                    #判断音乐文件                    if k[x:len(k)]=='.mp3' or k[x:len(k)]=='.wma' or k[x:len(k)]=='.flac':                             pos = pathdir3.index(":")                        pathfile = pathdir3[pos+1:len(pathdir3)] + '\r\n'                        a = filetop + pathfile                        b = pathfile                        a_utf = a.encode('utf-8')                        b_gbk = b.encode('gbk')                        file.write(a_utf)                        file.write(b_gbk)                        file2.write(a_utf)                        file2.write(b_gbk)                file2.close()                print('新建播放列表: '+ listname2)            elif os.path.isfile(pathdir2):                #查询音乐文件                x = j.index(".",len(j)-5,len(j))                #判断音乐文件                if j[x:len(j)]=='.mp3' or j[x:len(j)]=='.wma' or j[x:len(j)]=='.flac':                    pos = pathdir2.index(":")                   # print(pathdir2)                    pathfile = pathdir2[pos+1:len(pathdir2)] + '\r\n'                   # print(pathfile)                    a = filetop + pathfile                    b = pathfile                    a_utf = a.encode('utf-8')                    b_gbk = b.encode('gbk')                    file.write(a_utf)                    file.write(b_gbk)        file.close()        print('新建播放列表: '+ listname)
等有时间的时候再写一个软件吧

不早了,睡觉~

http://write.blog.csdn.net/postedit/45124379

0 0
原创粉丝点击