无法在使用Mutagen添加封面后播放MP3歌曲。

huangapple go评论87阅读模式
英文:

Unable to Play MP3 Song After Adding Artwork Using Mutagen

问题

我正在使用pytube库成功从YouTube下载音频内容。然而,当尝试使用mutagen库将缩略图添加到已下载的MP3文件时,文件变得无法播放。在添加缩略图之前,音频部分仍然正常工作,但在添加缩略图后,文件似乎已损坏或无法访问。我也尝试过eyed3,但结果相同。

以下是您提供的代码部分:

from os import rename
from os.path import exists, expanduser, splitext
from pytube import YouTube, Search
from requests import get
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC

yt = YouTube(link)
print(f'Downloading...')
audio_stream = yt.streams.filter(only_audio=True).first()
download_file = audio_stream.download(output_path=download_path)
base, _ = splitext(download_file)
out_file = base + '.mp3'
rename(download_file, out_file)

thumbnail = get(yt.thumbnail_url).content
audio = MP3(out_file, ID3=ID3)

if audio.tags is None:
    audio.add_tags()

audio.tags.add(
    APIC(
        encoding=3,
        mime='image/jpeg',
        type=3,
        desc=u'Cover',
        data=thumbnail
    )
)
audio.save()

希望这对您有所帮助。

英文:

I am utilizing the pytube library to download audio content from YouTube successfully. However, when attempting to add a thumbnail to the downloaded MP3 file using the mutagen library, the file becomes unplayable. The audio portion remains functional until the thumbnail is added, at which point the file appears to be corrupted or inaccessible. I also tried with eyed3 but the same thing.

Code:

from os import rename
from os.path import exists, expanduser, splitext
from pytube import YouTube, Search
from requests import get
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC

yt = YouTube(link)
print(f'Downloading...')
audio_stream = yt.streams.filter(only_audio=True).first()
download_file = audio_stream.download(output_path=download_path)
base, _ = splitext(download_file)
out_file = base + '.mp3'
rename(download_file, out_file)

thumbnail = get(yt.thumbnail_url).content
audio = MP3(out_file, ID3=ID3)

if audio.tags is None:
    audio.add_tags()
    
audio.tags.add(
    APIC(
        encoding=3,
        mime='image/jpeg',
        type=3,
        desc=u'Cover',
        data=thumbnail
    )
)
audio.save()

答案1

得分: 0

需要将文件转换为`mp3`格式而不仅仅是更改文件扩展名您可以使用[pydub](http://pydub.com)库进行转换还请确保已安装[ffmpeg](http://www.ffmpeg.org)

```python
import os
import pydub
import pytube
import requests
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC

yt = pytube.YouTube(link)
download_file = yt.streams.filter(only_audio=True).first().download(output_path=download_path)
out_file = os.path.splitext(download_file)[0] + '.mp3'
pydub.AudioSegment.from_file(download_file).export(out_file, format='mp3')
audio = MP3(out_file, ID3=ID3)
if not audio.tags:
    audio.add_tags()
audio.tags.add(APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=requests.get(yt.thumbnail_url).content))
audio.save()
os.remove(download_file)

<details>
<summary>英文:</summary>

You need to convert file to `mp3` format, not just rename file extension. You can use [pydub](http://pydub.com) lib for converting. Also make sure you have [ffmpeg](http://www.ffmpeg.org) installed.

    import os
    import pydub
    import pytube
    import requests
    from mutagen.mp3 import MP3
    from mutagen.id3 import ID3, APIC
    
    yt = pytube.YouTube(link)
    download_file = yt.streams.filter(only_audio=True).first().download(output_path=download_path)
    out_file = os.path.splitext(download_file)[0] + &#39;.mp3&#39;
    pydub.AudioSegment.from_file(download_file).export(out_file, format=&#39;mp3&#39;)
    audio = MP3(out_file, ID3=ID3)
    if not audio.tags:
        audio.add_tags()
    audio.tags.add(APIC(encoding=3, mime=&#39;image/jpeg&#39;, type=3, desc=u&#39;Cover&#39;, data=requests.get(yt.thumbnail_url).content))
    audio.save()
    os.remove(download_file)

</details>



huangapple
  • 本文由 发表于 2023年8月4日 23:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定