44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import mimetypes
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import http_pool
|
|
|
|
|
|
def download_from_yt(url: str, album_path: Path) -> None:
|
|
'''YouTube-specific cover art downloader.
|
|
See https://git.dc09.ru/DarkCat09/musicdlp/issues/1#issuecomment-202'''
|
|
|
|
path = album_path / 'cover.webp'
|
|
if path.exists():
|
|
return
|
|
|
|
ret = subprocess.call(
|
|
[
|
|
'ffmpeg',
|
|
'-nostdin',
|
|
'-i', url,
|
|
'-vf', 'crop=ih:ih',
|
|
str(album_path / 'cover.webp'),
|
|
],
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
|
|
if ret != 0:
|
|
raise RuntimeError(f'FFmpeg returned {ret} exit code')
|
|
|
|
|
|
def download(url: str, album_path: Path) -> None:
|
|
'''General cover art downloader'''
|
|
|
|
resp = http_pool.get().request('GET', url)
|
|
ext = mimetypes.guess_extension(resp.headers['content-type']) or '.jpg'
|
|
|
|
path = album_path / ('cover' + ext)
|
|
if path.exists():
|
|
return
|
|
|
|
with (album_path / ('cover' + ext)).open('wb') as f:
|
|
for chunk in resp.read_chunked():
|
|
f.write(chunk)
|