Shazamio (python) 无法识别音频格式

问题描述 投票:0回答:1

我正在编写一个脚本,该脚本将从 YouTube 下载音乐并编辑其元数据(添加封面、名称、专辑等) 所以这是完整的代码:

from blessings import Terminal
from shazamio import Shazam, Serialize
import asyncio
import yt_dlp 
import warnings
import eyed3
import requests
import urllib
import os

#temporrary
warnings.filterwarnings("ignore")

t = Terminal()

def download_audio(url):
    """
    Downloads an audio from youtube
    """
    
    ydl_opts = {
        'format': 'bestaudio/beyst',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'outtmpl': '%(title)s.%(ext)s',
    }
    filename = None
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
        video_title = info_dict.get('title', None)
        print(video_title)
        filename = f"{video_title}.mp3"
        edit_metadata(filename)
        print(t.green+"Successfully Downloaded - check the local folder")
    return filename

async def recognize_audio(audio):
    shazam = Shazam()
    out = await shazam.recognize(audio)
    song_info = {}
    track_info = out["track"]
    song_info["title"] = track_info["title"]
    song_info["artist"] = track_info["subtitle"]
    song_info["img"] = track_info["images"]["coverart"]
    return song_info

def get_image(url):

    response = urllib.request.urlopen(url)
    imagedata = response.read()
    return imagedata

def edit_metadata(audio):
    songinfo = asyncio.run(recognize_audio(audio))
    img_data = get_image(songinfo["img"])
    

    audiofile = eyed3.load(audio)
    audiofile.tag.artist = songinfo["artist"]
    audiofile.tag.title = songinfo["title"]
    audiofile.tag.images.set(type_=3, img_data=img_data, mime_type="images/jpeg", description=u"Cover art", img_url= u""+songinfo["img"])

    #renaming the file
    os.rename(audio, f'{songinfo["title"]}.mp3')

    audiofile.tag.save()

def main():
    url = input("Paste a link from YouTube to the song you want to download: ")
    #getting filename of the download
    f = download_audio(url)
    print(f)
    edit_metadata(f)

if __name__ == "__main__":
    main()

如果我单独使用

download_audio
函数和
edit_metadata
函数就可以了。我的意思是,当我下载音频时,我会停止脚本并对音频名称进行硬编码。然后
shazamio
将完美地完成其工作,并且
eyed3
将编辑其元数据。 这是它抛出的错误

Traceback (most recent call last):
  File "/Users/user/Desktop/programming/musicdownloader/main.py", line 83, in <module>
    main()
  File "/Users/user/Desktop/programming/musicdownloader/main.py", line 80, in main
    edit_metadata(f)
  File "/Users/user/Desktop/programming/musicdownloader/main.py", line 61, in edit_metadata
    songinfo = asyncio.run(recognize_audio(audio))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/user/Desktop/programming/musicdownloader/main.py", line 44, in recognize_audio
    out = await shazam.recognize(audio)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/Desktop/programming/musicdownloader/env/lib/python3.11/site-packages/shazamio/api.py", line 578, in recognize
    signature = await self.core_recognizer.recognize_path(data)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SignatureError: Unrecognized format

首先它下载 mp4,然后将其转换为 webm,但随后它会转换为 mp3。下载完成后我已经打印了它的文件名,并且脚本将其保存在 mp3 中。

python eyed3 shazam
1个回答
0
投票

所以问题是我在这个过程中更改了文件名,正如您在

edit_metadata
函数中看到的那样。

def edit_metadata(audio):
    songinfo = asyncio.run(recognize_audio(audio))
    img_data = get_image(songinfo["img"])
    

    audiofile = eyed3.load(audio)
    audiofile.tag.artist = songinfo["artist"]
    audiofile.tag.title = songinfo["title"]
    audiofile.tag.images.set(type_=3, img_data=img_data, mime_type="images/jpeg", description=u"Cover art", img_url= u""+songinfo["img"])

    #we gotta remove this line. we should do it in the end of the main function
    os.rename(audio, f'{songinfo["title"]}.mp3')

    audiofile.tag.save()
© www.soinside.com 2019 - 2024. All rights reserved.