Zum Hauptinhalt springen
info
Dieser Teil ist für Entwickler und nur auf Englisch verfügbar.

Thumbnails

We process media files with different libraries to convert them to a widely compatible format and to speed up previewing files.

Thumbnail engines​

libvips (images)​

By leveraging libvips' lightning-fast image processing capabilities and memory-efficient design, the conversion process becomes remarkably fast, ensuring optimal performance even with numerous images. Moreover, the utilization of pyvips as a Python interface facilitates seamless integration with the project

libvips comes bundled with pyvips-binary; what that build cannot decode (HEIC, JPEG XL, BMP, JPEG 2000) is decoded by Pillow with pillow-heif and pillow-jxl-plugin and handed back to pyvips (api/thumbnails/decoding.py).

Neu in 1.7.0 libvips' operation cache is off (pyvips.cache_set_max(0)) in every process that renders: api/thumbnails/decoding.py sets it on import, and every module that renders with pyvips (api.thumbnails.render, api.editing.source, the metadata-free copies of api.sharing.copies) imports it first, and so does the thumbnail service. Noch nicht erschienen (kommt nach 1.7.0) django.setup() no longer imports pyvips at all (see Memory of the Django processes). The cache keeps up to 100 recent operations with their pixels so that an identical operation can be answered again, but a scan decodes every photo once, so it only held about 100 MB of decoded photos per worker and per thumbnail service until the process ended. Measured on a scan of 159 files with four workers: the container's peak went from 2.26 GB to 1.69 GB and the workers from 891 MB to 308 MB at that moment, and the scan took 97 s instead of 110 s.

The original is decoded once, at the big thumbnail's size; the two smaller thumbnails are resized from that image (create_static_thumbnails in api/thumbnails/render.py). Thumbnails are WebP at quality 95 and encoder effort 2, which encodes twice as fast as libwebp's default effort 4 at the same file size.

LibRaw via rawpy (RAW images)​

Almost every camera stores a JPEG preview it rendered itself inside the RAW file. When that preview shows the whole picture (the same aspect ratio as the sensor image) and is at least as tall as the big thumbnail, the thumbnail is made from it, in the worker (image_decoding.raw_preview). This takes a fraction of a full render, and the thumbnail looks like the camera's own JPEG.

Otherwise the RAW is rendered with LibRaw through rawpy, which ships LibRaw in its wheel, and the result is resized and saved as WebP by pyvips. This runs on a separate microservice so that a decoder crash on an exotic file stays away from the API workers.

FFmpeg (video)​

As a powerful and versatile multimedia framework, FFmpeg equips users with a comprehensive suite of tools to seamlessly convert videos in various formats. The open-source nature of FFmpeg results in continuous updates and improvements that keep pace with evolving industry standards.

Neu in 1.7.0 A video's thumbnail, its animated preview and the frames Scan Faces reads run with -threads from api.video.ffmpeg_budget.thumbnail_threads(): half the cores, at most 4 (THUMBNAIL_THREADS), for the decoder and, for the preview, the x264 encoder too. ffmpeg otherwise starts a thread per core and a frame buffer for each, so on 12 cores one 1080p preview took 465 MB and four scan workers making previews at once took 880 MB; with 4 threads a preview takes 275 MB (in 1.6 times the time of one alone, about the same when the workers share the cores) and a frame 80 MB instead of 130 MB in the same time. Up to 1.6.0 these ran with ffmpeg's defaults; the live and cached conversions keep their own budget (cpu_share).

Source of truth for processing​

Big thumbnails act as a source of truth for all subsequent processing like finding faces, calculating similarity, finding objects etc. The reason for that is twofold: First, we know that all files will be compatible with the machine learning pipelines, because it's only one format, second all file dimensions are limited to a certain format, which will prevent a possible explosion of resource use.

Other thumbnails​

We also create thumbnails for previewing faces: during face extraction the face region is cropped out of the big thumbnail and stored as a JPEG under protected_media/faces, so a lot of them can be displayed quickly. User avatars are not thumbnailed by the backend — the frontend scales the chosen image to a 150x150 PNG in the browser before upload, and the backend stores that file as-is under protected_media/avatars.

Endpoints:​

Authentication

These /media/... endpoints are all served by UnifiedMediaAccessView, which is declared AllowAny and authenticates with JWTCookieAuthentication plus Basic auth: an Authorization: Bearer <token> header when there is one, otherwise the jwt cookie. When both are sent, the header wins. POST /api/auth/token/obtain/ and POST /api/auth/token/refresh/ set the jwt cookie automatically, which is how <img> and <video> requests authenticate. A request without a usable credential for non-public media is rejected with 403 Forbidden and an X-Media-Error: authentication header; an invalid bearer header answers 401.

The exception is media belonging to an active public album share, which is served without any authentication. See API authentication for the full flow.

GET /media/thumbnails_big/<hash>​

Gives you a large preview of the actual file. It is also an image for videos. If you want to display the video file use media/photos/<hash> instead.

GET /media/square_thumbnails/<hash>​

Gives you a normal preview of the actual file. Can be an image or a video.

GET /media/square_thumbnails_small/<hash>​

Gives you a small preview. Usually only usable with a blur to indicate loading. Could be replaced by blur hash.

GET /media/photos/<hash>​

Return the actual image or video from the server.

GET /media/faces/<filename>​

Returns the cropped face image for a photo. The file is created as <hash>_<face_number>.jpg, but Django's default storage appends a random uniqueness suffix when that name is already taken (for example bb6685821c52c994cf7bbe9ebfd5eb7e1_2_fpZqB0S.jpg), so do not construct the name yourself — use the face_url value returned by the faces or persons endpoints.

GET /media/avatars/<filename>​

Returns the avatar for a given user. The frontend uploads it as <first_name>avatar.png (there is no hash component on the first upload); the same random uniqueness suffix is appended on re-upload. Use the avatar_url field from the user endpoints rather than building the name.

GET /media/embedded_media/<hash>​

Returns the embedded video track of a motion photo (Samsung or Google) as video/mp4. The path segment accepts either the photo UUID (36 characters with 4 hyphens) or the legacy image hash, and returns 404 if the photo has no embedded media file. Used by the frontend lightbox to play the motion-photo clip. Unlike the other endpoints this one does not hard-require the cookie: an unauthenticated request is limited to public photos, and it narrows to the owner's photos when the request is authenticated (bearer header, jwt cookie or Basic auth).

GET /media/zip/<prefix>​

Serves a bulk-download archive as application/x-zip-compressed. The path segment is a filename prefix, not the full filename — the backend appends the requesting user's id and .zip, so a user can only retrieve their own archive. Requires authentication (bearer header or jwt cookie); returns 403 Forbidden otherwise.