Files
ytdl-sub/src/ytdl_sub/utils/thumbnail.py
Maka0 cb31156b42 Added QEMU/buildx to Github Actions to build multiarch containers (#84)
* Updated docker to use buildx Buildkit

* Updated github actions to include QEMU/buildx to build multiarch containers

* Removed packages after build is done

* Updated documentation to set the correct default thumbnail file name

* Parallel jobs (#1)

Implemented parallel docker builds on Github Actions

* Updated default config file in Docker container

* Replaced Pillow with ffmpeg for thumbnail converting (and updated documentation to change the default thumbnail to -thumb.jpg) (#2)

* Swapped Pillow for ffmpeg for thumbnail conversion
* Further changed default thumbname to include "-thumb" (Kodi needs this to pick up the thumbnail image)
* Added ffmpeg bitexact for reproducability

* Removed extra packages needed for Pillow

* Updated ref check (#3)

* Fixed formatting on if statement for deploy job

Co-authored-by: Maka0 <bas@oddens.net>
2022-07-04 01:17:04 -07:00

67 lines
2.1 KiB
Python

import os
import tempfile
from pathlib import Path
from typing import Optional
from urllib.request import urlopen
from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.ffmpeg import FFMPEG
def _get_downloaded_thumbnail_path(entry: Entry) -> Optional[str]:
thumbnails = entry.kwargs("thumbnails") or []
possible_thumbnail_exts = {"jpg", "webp"} # Always check for jpg and webp thumbs
# The source `thumbnail` value and the actual downloaded thumbnail extension sometimes do
# not match. Find all possible extensions by checking all available thumbnails.
for thumbnail in thumbnails:
possible_thumbnail_exts.add(thumbnail["url"].split(".")[-1])
for ext in possible_thumbnail_exts:
possible_thumbnail_path = str(Path(entry.working_directory()) / f"{entry.uid}.{ext}")
if os.path.isfile(possible_thumbnail_path):
return possible_thumbnail_path
return None
def convert_download_thumbnail(entry: Entry):
"""
Converts an entry's downloaded thumbnail into jpg format
Parameters
----------
entry
Entry with the thumbnail
Raises
------
ValueError
Entry thumbnail file not found
"""
download_thumbnail_path = _get_downloaded_thumbnail_path(entry=entry)
download_thumbnail_path_as_jpg = entry.get_download_thumbnail_path()
if not download_thumbnail_path:
raise ValueError("Thumbnail not found")
if not download_thumbnail_path == download_thumbnail_path_as_jpg:
FFMPEG.run(["-bitexact", "-i", download_thumbnail_path, download_thumbnail_path_as_jpg])
def convert_url_thumbnail(thumbnail_url: str, output_thumbnail_path: str):
"""
Downloads and converts a thumbnail from a url into a jpg
Parameters
----------
thumbnail_url
URL of the thumbnail
output_thumbnail_path
Thumbnail file destination after its converted to jpg
"""
with urlopen(thumbnail_url) as file:
with tempfile.NamedTemporaryFile() as thumbnail:
thumbnail.write(file.read())
FFMPEG.run(["-bitexact", "-i", thumbnail.name, output_thumbnail_path, "-bitexact"])