본문 바로가기
프로그램

[파이썬] 문제 : mp3 정보 찾기

by 오디세이99 2023. 12. 14.
728x90
반응형
import eyed3
import os

def get_mp3_tags(file_path):
    audio_file = eyed3.load(file_path)            # MP3 파일 정보 로드

    if audio_file is None:
        return "Invalid file or unsupported format"

    # 파일 정보 추출
    title = audio_file.tag.title
    artist = audio_file.tag.artist
    album = audio_file.tag.album
    genre = audio_file.tag.genre
    year = audio_file.tag.getBestDate()
    length = audio_file.info.time_secs

    file_size = os.path.getsize(file_path)         # 파일 용량 계산

    # 정보 출력
    return {
        "Title": title,
        "Artist": artist,
        "Album": album,
        "Genre": genre,
        "Year": year.year if year else "Unknown",
        "Length (seconds)": length,
        "File Size (bytes)": file_size
    }

# 예시 파일 경로
file_path = "E:/tmp/musicBank/최신/여행스케치-그녀석들과 여행.mp3"

# 태그 정보 출력
tags = get_mp3_tags(file_path)

for key, value in tags.items():
    print(f"{key} : {value}")

 

폴더내 모든 mp3 파일 정보 읽어서 파일로 저장하기

import eyed3
import os

def get_mp3_tags(file_path):
    audio_file = eyed3.load(file_path)            # MP3 파일 정보 로드

    if audio_file is None:
        return "Invalid file or unsupported format"

    audio_file.tag.encoding = 'utf-8'
    
    # 파일 정보 추출
    title = audio_file.tag.title
    artist = audio_file.tag.artist
    album = audio_file.tag.album
    genre = audio_file.tag.genre
    year = audio_file.tag.getBestDate()
    length = audio_file.info.time_secs

    file_size = os.path.getsize(file_path)         # 파일 용량 계산

    # 정보 출력
    return {
        "Title": title,
        "Artist": artist,
        "Album": album,
        "Genre": genre,
        "Year": year.year if year else "Unknown",
        "Length (seconds)": length,
        "File Size (bytes)": file_size
    }

path = 'E:/tmp/musicBank/최신'
mp3_files = [f for f in os.listdir(path) if f.endswith('.mp3')]

f = open('mp3_info.csv','w', encoding='UTF-8')
for mp3 in mp3_files:
    file_path = path + '/' + mp3

    tags = get_mp3_tags(file_path)           # 태그 정보 출력
    # print(tags)
    f.write(file_path + ',')
    for i, value in enumerate(tags.values()):
        if value == None:
            value = ''
        # if type(value) == float or type(value) == int or type(value) == Genre:
        if type(value) != str:
            value = str(value)
        # print(f"{i} : {value}")
        f.write(value)
        if i < len(tags.values())-1:
            f.write(',')
    f.write('\n')
        
f.close()

 

 

728x90
반응형

댓글