获取歌曲信息歌曲详细信息

# -*- coding: utf-8 -*-
from mutagen.mp3 import MP3
import os

class GetMp3Info():
    '''获取歌曲信息'''
    def __init__(self, path):
        songFile = MP3(path)
        self.getTitle(songFile, path)
        self.getArtist(songFile)
        self.getAlbum(songFile)
        self.getLength(songFile)

    def getTitle(self, songFile, path):
        '''获取歌曲名
        songFile:文件对象
        path:文件地址
        '''
        try:
            self.title = str(songFile.tags['TIT2'])
        except:
            filename = os.path.basename(path)  # 从地址中获取文件名
            self.title = filename.split('.')[0]  # 去掉文件名后缀

    def getArtist(self,songFile):
        '''获取歌手名
        songFile:文件对象
        '''
        try:
            self.artist=str(songFile.tags['TPE1'])
        except:
            self.artist=''

    def getAlbum(self,songFile):
        '''获取专辑名
        songFile:文件对象
        '''
        try:
            self.album=str(songFile.tags['TALB'])
        except:
            self.album=''

    def getLength(self,songFile):
        '''获取文件播放时时长'''
        timeLength = int(songFile.info.length)
        mintime = timeLength//60  #转换为分钟
        sectime = timeLength % 60 #剩余的转换为秒
        if sectime < 10:
            sectime='0'+ str(sectime)
        else:
            sectime=str(sectime)
        self.length=str(mintime)+":"+sectime

if __name__=='__main__':
    path="E:\\KwDownload\\song\\Jarico.mp3"
    song = GetMp3Info(path)
    print(song.title,song.artist,song.album,song.length)

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值