O
O
OniMoni2018-02-06 12:34:51
Python
OniMoni, 2018-02-06 12:34:51

How to extract tags from MP3?

Good afternoon, I have a question. How to extract artist tags and title from an mp3 file
, I used the following libraries:
1) Eyed3, but troubles occur with it when you need to call the Tag method, it says it does not have such an attribute, and then the necessary methods become inaccessible to me directly
2) Mutagen , not everything is going smoothly with him either, he does not understand keys like ["TIT2"], ["TPE1"], etc.
Here is the code itself:

import numpy as np
import os
import shutil
import re
import random
from mutagen.mp3 import EasyMP3 as MP3
from mutagen.id3 import ID3


path = r'Кэшированная музыка из ВК'
pathVK = r'Сюда перещаю нормальный .мр3 файл, но без названия и исполнителя'
Name = []

#Здесь парсит теги и даёт нормальное имя файлу
def Artist(file):
  os.chdir(pathVK)
  #try:
  trackInfo = MP3(file)
  STitle = trackInfo['title']
  SArtist = trackInfo['performer']
  os.rename(file, SArtist + " - " + SAlbum + '.mp3')
  #except:
    #print ('1')

#Здесь превращает файл в .mp3 
def ReName(file):
  for x in os.listdir(pathVK):
    y = int(x.replace('.mp3', ''))
    Name.append(y)

  os.chdir(path)
  shutil.move(file, pathVK)
  os.chdir(pathVK)
  os.rename(file, str(max(Name) + 1) + '.mp3')
  Artist(file)
  
#Удаляет, если не тот файл
def ReMove(file):
  os.chdir(path)
  os.remove(file)


for FileName in os.listdir(path):
  if FileName.endswith('.covers'):
    ReMove(FileName)
  else:
    ReName(FileName)
    
for audioFile in os.listdir(pathVK):
  Artist(audioFile)

I understand that the code is still UG, but I'm a newbie, the main thing is that it would work, and then I'll refactor) Thank you for understanding))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Matveev, 2018-02-06
@Shlem

Good afternoon.
To begin with, I recommend studying the basics of the python style guide https://pythonworld.ru/osnovy/pep-8-rukovodstvo-po...
There is no point in the remove function. You can write os.remove(filename) right after checking for cover. you are already in this folder
I get the tags through eyed3 as described here https://eyed3.readthedocs.io/en/latest/
Python is a case-sensitive language, Tag and tag are different things for it

audiofile = eyed3.load("song.mp3")
artist = audiofile.tag.artist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question