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

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

Unable to Play MP3 Song After Adding Artwork Using Mutagen

问题

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

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

  1. from os import rename
  2. from os.path import exists, expanduser, splitext
  3. from pytube import YouTube, Search
  4. from requests import get
  5. from mutagen.mp3 import MP3
  6. from mutagen.id3 import ID3, APIC
  7. yt = YouTube(link)
  8. print(f'Downloading...')
  9. audio_stream = yt.streams.filter(only_audio=True).first()
  10. download_file = audio_stream.download(output_path=download_path)
  11. base, _ = splitext(download_file)
  12. out_file = base + '.mp3'
  13. rename(download_file, out_file)
  14. thumbnail = get(yt.thumbnail_url).content
  15. audio = MP3(out_file, ID3=ID3)
  16. if audio.tags is None:
  17. audio.add_tags()
  18. audio.tags.add(
  19. APIC(
  20. encoding=3,
  21. mime='image/jpeg',
  22. type=3,
  23. desc=u'Cover',
  24. data=thumbnail
  25. )
  26. )
  27. 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:

  1. from os import rename
  2. from os.path import exists, expanduser, splitext
  3. from pytube import YouTube, Search
  4. from requests import get
  5. from mutagen.mp3 import MP3
  6. from mutagen.id3 import ID3, APIC
  7. yt = YouTube(link)
  8. print(f'Downloading...')
  9. audio_stream = yt.streams.filter(only_audio=True).first()
  10. download_file = audio_stream.download(output_path=download_path)
  11. base, _ = splitext(download_file)
  12. out_file = base + '.mp3'
  13. rename(download_file, out_file)
  14. thumbnail = get(yt.thumbnail_url).content
  15. audio = MP3(out_file, ID3=ID3)
  16. if audio.tags is None:
  17. audio.add_tags()
  18. audio.tags.add(
  19. APIC(
  20. encoding=3,
  21. mime='image/jpeg',
  22. type=3,
  23. desc=u'Cover',
  24. data=thumbnail
  25. )
  26. )
  27. audio.save()

答案1

得分: 0

  1. 需要将文件转换为`mp3`格式而不仅仅是更改文件扩展名您可以使用[pydub](http://pydub.com)库进行转换还请确保已安装[ffmpeg](http://www.ffmpeg.org)
  2. ```python
  3. import os
  4. import pydub
  5. import pytube
  6. import requests
  7. from mutagen.mp3 import MP3
  8. from mutagen.id3 import ID3, APIC
  9. yt = pytube.YouTube(link)
  10. download_file = yt.streams.filter(only_audio=True).first().download(output_path=download_path)
  11. out_file = os.path.splitext(download_file)[0] + '.mp3'
  12. pydub.AudioSegment.from_file(download_file).export(out_file, format='mp3')
  13. audio = MP3(out_file, ID3=ID3)
  14. if not audio.tags:
  15. audio.add_tags()
  16. audio.tags.add(APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=requests.get(yt.thumbnail_url).content))
  17. audio.save()
  18. os.remove(download_file)
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. import os
  5. import pydub
  6. import pytube
  7. import requests
  8. from mutagen.mp3 import MP3
  9. from mutagen.id3 import ID3, APIC
  10. yt = pytube.YouTube(link)
  11. download_file = yt.streams.filter(only_audio=True).first().download(output_path=download_path)
  12. out_file = os.path.splitext(download_file)[0] + &#39;.mp3&#39;
  13. pydub.AudioSegment.from_file(download_file).export(out_file, format=&#39;mp3&#39;)
  14. audio = MP3(out_file, ID3=ID3)
  15. if not audio.tags:
  16. audio.add_tags()
  17. 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))
  18. audio.save()
  19. os.remove(download_file)
  20. </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:

确定