从MusicBrainz获取音轨列表

时间:2013-07-04 12:13:57

标签: python musicbrainz

我正在努力学习Python,并且认为我会通过写一些我实际使用的东西来学习。所以我正在尝试编写一个小脚本来翻录一些音乐CD。

我正在使用musicbrainzngs包。我想获得CD的跟踪列表。我的代码目前:

#! /usr/bin/env python

import argparse
import musicbrainzngs
import discid

musicbrainzngs.set_useragent("Audacious", "0.1", "https://github.com/jonnybarnes/audacious")

parser = argparse.ArgumentParser()
parser.add_argument("--cdrom", help="provide the source of the cd", default="/dev/cdrom")
args = parser.parse_args()

device = args.cdrom

print("device: %s" % device)
disc = discid.read(device)
print("id: %s" % disc.id)

try:
    result = musicbrainzngs.get_releases_by_discid(disc.id, includes=["artists"])
except musicbrainzngs.ResponseError:
    print("disc not found or bad response")
else:
    if result.get("disc"):
        print("artist:\t%s" %
            result["disc"]["release-list"][0]["artist-credit-phrase"])
        print("title:\t%s" % result["disc"]["release-list"][0]["title"])
    elif result.get("cdstub"):
        print("artist:\t" % result["cdstub"]["artist"])
        print("title:\t" % result["cdstub"]["title"])

如何获取跟踪列表,查看返回的完整结果有track-list属性,但无论我尝试使用什么CD,结果始终为空

2 个答案:

答案 0 :(得分:3)

通过discid is a lookup获取版本及其支持的“inc ='参数与发布的查找请求相同”列出earlier on that page。要获得非空的跟踪列表,您只需添加“录音”包括:

result = musicbrainzngs.get_releases_by_discid(disc.id, includes=["artists", "recordings"])

答案 1 :(得分:1)

这是一个示例脚本,用于使用musicbrainzngs获取专辑的曲目列表

#!/usr/bin/python3
from __future__ import print_function
from __future__ import unicode_literals
import musicbrainzngs
import sys

musicbrainzngs.set_useragent(
    "python-musicbrainzngs-example",
    "0.1",
    "https://github.com/alastair/python-musicbrainzngs/",
)

def get_tracklist(artist, album):
    result = musicbrainzngs.search_releases(artist=artist, release=album, limit=1)
    id = result["release-list"][0]["id"]
    
    #### get tracklist
    new_result = musicbrainzngs.get_release_by_id(id, includes=["recordings"])
    t = (new_result["release"]["medium-list"][0]["track-list"])
    for x in range(len(t)):
        line = (t[x])
        print(f'{line["number"]}. {line["recording"]["title"]}')

if __name__ == '__main__':
    ### get first release
    if len(sys.argv) > 1:
        artist, album = [sys.argv[1], sys.argv[2]]
        get_tracklist(artist, album)
    else:
        artist = input("Artist: ")
        album = input("Album: ")
        if not artist == "" and not album == "":
            get_tracklist(artist, album)
        else:
            print("Artist or Album missing")
    

用法:

python3 album_get_tracklist.py“滚石”“乞g宴会”

python3 album_get_tracklist.py

它将询问艺术家和专辑