英文:
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] + '.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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论