fix: keep GIFs moving in reply contexts (v2.20.1)

Mastodon turns GIFs into silent looping MP4s (gifv); only the first-frame
preview was kept. The MP4 is now saved locally and played muted and
looping with the still as poster. Animated GIFs from other sites are saved
as animated WebP. Migration 025 adds reply_contexts.video and has
fediverse contexts with an image fetched again once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-13 20:28:20 -07:00
1 parent 03a1c8fcea
commit 952ca300c0
9 files changed
+193 -19

No files matched your search

+11
View File
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [2.20.1] - 2026-11-13
### Fixed
- A reply to a Mastodon post with a GIF showed a still image. Mastodon turns GIFs into short silent looping MP4s ("gifv"), and only their first frame was kept. The MP4 is now saved on this site and plays muted and looping, as on Mastodon, with the still image as its poster
- Animated GIFs (and animated WebP/PNG) from other sites stay animated in reply contexts instead of being cut to their first frame
### Changed
- Migration 025 adds `video` to `reply_contexts`, and has fediverse contexts with an image fetched again once, so existing replies pick up their gifv
## [2.20.0] - 2026-11-13
### Added
@@ -0,0 +1,27 @@
# v2.20.1: Moving pictures in reply contexts
## Why
The reply https://starpunk.thesatelliteoflove.com/note/20261114032356 answers
an infosec.exchange post with a GIF, but the card showed a still. Mastodon
converts uploaded GIFs to a silent looping MP4 (`media_attachments[].type ==
"gifv"`, here 320×240, 5.3 s, ~140 KB). Its `preview_url` is a PNG of the
first frame, which is all v2.19.0 kept.
## Changes
| Area | Change |
|---|---|
| `reply_context._from_status` | A `gifv` attachment's `url` goes into the new `video` field (the preview stays in `photo`, used as the poster) |
| Migration 025 | `reply_contexts.video`; fediverse rows with a photo get `source = NULL`, so the worker's `refresh_stale` fetches them once more |
| `avatars.fetch_video` | Saves the MP4 as `DATA_PATH/previews/<sha256>.mp4` (8 MB cap, must have an `ftyp` box); `fill_missing`/`remove_unused` cover it |
| `avatars._save` | Animated GIF/WebP/PNG previews are saved as animated WebP (frames narrowed to 640 px, at most 300 frames) instead of their first frame |
| `/previews/<name>` | Also serves `<sha256>.mp4` as `video/mp4` |
| Template | `<video autoplay loop muted playsinline poster=…>` with the alt text as `aria-label`, when there is a saved MP4 |
The MP4 is stored as it is: re-encoding would need ffmpeg in the image, and
gifvs are small. Like the images, it has to be served from this site
(`default-src 'self'` covers media).
Real videos (`type == "video"`) and Bluesky videos still show their
thumbnail: they can be long and have sound.
+6
View File
@@ -0,0 +1,6 @@
-- Migration 025: Looping videos in reply contexts (v2.20.1)
-- Mastodon turns GIFs into silent looping MP4s ("gifv"); video holds its URL,
-- shown instead of the still preview. Fediverse contexts with an image are
-- fetched again once (no source) so their gifv is picked up.
ALTER TABLE reply_contexts ADD COLUMN video TEXT;
UPDATE reply_contexts SET source = NULL WHERE source = 'fediverse' AND photo IS NOT NULL;
+10 -2
View File
@@ -233,6 +233,14 @@ def create_app(config=None):
name = cached_preview(app.config.get('DATA_PATH', 'data'), url)
return url_for('public.preview', name=name) if name else None
# Template filter - local copy of a replied-to post's gifv, or None (v2.20.1)
@app.template_filter('local_video')
def local_video_filter(url):
from flask import url_for
from starpunk.avatars import cached_video
name = cached_video(app.config.get('DATA_PATH', 'data'), url)
return url_for('public.preview', name=name) if name else None
# Template filter - URL to color for facepile (v1.6.0)
@app.template_filter('url_to_color')
def url_to_color_filter(url):
@@ -476,5 +484,5 @@ def create_app(config=None):
# Package version (Semantic Versioning 2.0.0)
# See docs/standards/versioning-strategy.md for details
__version__ = "2.20.0"
__version_info__ = (2, 20, 0)
__version__ = "2.20.1"
__version_info__ = (2, 20, 1)
+67 -11
View File
@@ -29,6 +29,10 @@ SIZE = 96
MAX_BYTES = 1024 * 1024
PREVIEW_WIDTH = 640
PREVIEW_MAX_BYTES = 8 * 1024 * 1024
MAX_FRAMES = 300
# Mastodon's "GIFs" are silent looping MP4s (gifv); kept as they are (v2.20.1)
VIDEO_MAX_BYTES = 8 * 1024 * 1024
VIDEO_NAME_RE = re.compile(r"^[0-9a-f]{64}\.mp4$")
# Bluesky avatar on its CDN: .../img/<kind>/plain/<did>/<cid>[@jpeg]
BSKY_CDN_RE = re.compile(
r"^https://cdn\.bsky\.app/img/[a-z_]+/plain/(did:(?:plc|web):[A-Za-z0-9._:%-]+)/([a-z2-7]+)(?:@\w+)?$")
@@ -65,6 +69,39 @@ def cached_preview(data_path, url: Optional[str]) -> Optional[str]:
return _cached(preview_folder(data_path), url)
def video_name_for(url: str) -> str:
return hashlib.sha256(url.encode()).hexdigest() + ".mp4"
def cached_video(data_path, url: Optional[str]) -> Optional[str]:
"""File name of the saved copy of a replied-to post's looping video, if there is one"""
if not url or not url.startswith(("http://", "https://")):
return None
name = video_name_for(url)
return name if (preview_folder(data_path) / name).is_file() else None
def fetch_video(data_path, url: Optional[str], site_url: str) -> bool:
"""Save a copy of a replied-to post's gifv (a short silent MP4) unless there is one"""
if not url or not url.startswith(("http://", "https://")):
return False
path = preview_folder(data_path) / video_name_for(url)
if path.is_file():
return True
try:
data = _download(url, site_url, VIDEO_MAX_BYTES)
if data[4:8] != b"ftyp":
raise ValueError("not an MP4")
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(".tmp")
tmp.write_bytes(data)
tmp.replace(path)
return True
except Exception as e:
logger.info(f"Could not save video {url}: {type(e).__name__}: {e}")
return False
def _square(image):
from PIL import Image
side = min(image.size)
@@ -81,6 +118,19 @@ def _narrow(image):
return image.resize((PREVIEW_WIDTH, height), Image.LANCZOS)
def _save_animated(image, tmp: Path) -> None:
"""An animated GIF/WebP/PNG as an animated WebP, frames narrowed (v2.20.1)"""
from PIL import ImageSequence
frames, durations = [], []
for frame in ImageSequence.Iterator(image):
if len(frames) >= MAX_FRAMES:
break
frames.append(_narrow(frame.convert("RGBA")))
durations.append(frame.info.get("duration") or image.info.get("duration") or 100)
frames[0].save(tmp, "WEBP", save_all=True, append_images=frames[1:], duration=durations,
loop=0, quality=80)
def _save(path: Path, url: Optional[str], site_url: str, shape, max_bytes: int,
what: str) -> bool:
if not url or not url.startswith(("http://", "https://")):
@@ -91,12 +141,15 @@ def _save(path: Path, url: Optional[str], site_url: str, shape, max_bytes: int,
try:
with Image.open(io.BytesIO(_download(url, site_url, max_bytes))) as image:
image.load()
image = image.convert("RGBA" if "A" in image.getbands() else "RGB")
image = shape(image)
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(".tmp")
image.save(tmp, "WEBP", quality=80)
if shape is _narrow and getattr(image, "n_frames", 1) > 1:
_save_animated(image, tmp)
else:
image.load()
image = image.convert("RGBA" if "A" in image.getbands() else "RGB")
image = shape(image)
image.save(tmp, "WEBP", quality=80)
tmp.replace(path)
return True
except Exception as e:
@@ -199,18 +252,19 @@ def _download(url: str, site_url: str, max_bytes: int = MAX_BYTES) -> bytes:
return _get(url, headers, max_bytes)
def _context_images(conn) -> tuple[list, list]:
"""Avatars and post images of reply contexts that notes use (v2.19.0)"""
def _context_images(conn) -> tuple[list, list, list]:
"""Avatars, post images and gifvs of reply contexts that notes use (v2.19.0)"""
rows = conn.execute(
"""
SELECT rc.author_photo, rc.photo FROM reply_contexts rc
SELECT rc.author_photo, rc.photo, rc.video FROM reply_contexts rc
WHERE rc.fetch_status = 'fetched' AND EXISTS (
SELECT 1 FROM notes n WHERE n.deleted_at IS NULL AND rc.url IN
(n.in_reply_to, n.like_of, n.repost_of, n.bookmark_of))
ORDER BY rc.id DESC
"""
).fetchall()
return [r[0] for r in rows if r[0]], [r[1] for r in rows if r[1]]
return ([r[0] for r in rows if r[0]], [r[1] for r in rows if r[1]],
[r[2] for r in rows if r[2]])
def fill_missing(db_path, data_path, site_url: str, limit: int = 20) -> int:
@@ -226,11 +280,12 @@ def fill_missing(db_path, data_path, site_url: str, limit: int = 20) -> int:
ORDER BY id DESC
"""
)]
context_avatars, context_photos = _context_images(conn)
context_avatars, context_photos, context_videos = _context_images(conn)
finally:
conn.close()
wanted = [(url, cached_name, fetch) for url in dict.fromkeys(urls + context_avatars)]
wanted += [(url, cached_preview, fetch_preview) for url in dict.fromkeys(context_photos)]
wanted += [(url, cached_video, fetch_video) for url in dict.fromkeys(context_videos)]
tried = 0
for url, cached, save in wanted:
if tried >= limit:
@@ -248,17 +303,18 @@ def remove_unused(db_path, data_path) -> int:
try:
used = {name_for(row[0]) for row in conn.execute(
"SELECT DISTINCT author_photo FROM webmentions WHERE author_photo IS NOT NULL")}
context_avatars, context_photos = _context_images(conn)
context_avatars, context_photos, context_videos = _context_images(conn)
finally:
conn.close()
used |= {name_for(url) for url in context_avatars}
used_previews = {name_for(url) for url in context_photos}
used_previews |= {video_name_for(url) for url in context_videos}
removed = 0
for directory, keep in ((folder(data_path), used), (preview_folder(data_path), used_previews)):
if not directory.is_dir():
continue
for path in directory.iterdir():
if NAME_RE.match(path.name) and path.name not in keep:
if (NAME_RE.match(path.name) or VIDEO_NAME_RE.match(path.name)) and path.name not in keep:
path.unlink(missing_ok=True)
removed += 1
return removed
+7 -2
View File
@@ -65,7 +65,7 @@ FEDIVERSE_PATH_RE = re.compile(
FEDI_TITLE_RE = re.compile(r"^(.*?)\s*\((@[^)\s]+)\)\s*$")
FIELDS = ('title', 'content_snippet', 'published_at', 'author_name', 'author_handle',
'author_url', 'author_photo', 'site_name', 'photo', 'photo_alt')
'author_url', 'author_photo', 'site_name', 'photo', 'photo_alt', 'video')
def fetch_reply_context(
@@ -300,11 +300,14 @@ def _from_status(status: dict, url: str) -> Dict[str, Any]:
snippet = _text_snippet(f"CW: {spoiler}")
else:
snippet = _snippet(status.get('content') or '') or None
photo = photo_alt = None
photo = photo_alt = video = None
if not status.get('sensitive'):
for media in status.get('media_attachments') or []:
if media.get('type') in ('image', 'gifv', 'video') and _is_http_url(media.get('preview_url')):
photo, photo_alt = media['preview_url'], media.get('description') or None
# A "GIF" on Mastodon is a silent looping MP4; the preview is its first frame
if media['type'] == 'gifv' and _is_http_url(media.get('url')):
video = media['url']
break
avatar = account.get('avatar_static') or account.get('avatar')
return {
@@ -319,6 +322,7 @@ def _from_status(status: dict, url: str) -> Dict[str, Any]:
'site_name': urlparse(url).hostname,
'photo': photo,
'photo_alt': photo_alt,
'video': video,
}
@@ -482,6 +486,7 @@ def _save_images(context: Dict[str, Any], site_url: str) -> None:
data_path = current_app.config['DATA_PATH']
avatars.fetch(data_path, context.get('author_photo'), site_url)
avatars.fetch_preview(data_path, context.get('photo'), site_url)
avatars.fetch_video(data_path, context.get('video'), site_url)
except Exception as e:
logger.info(f"Could not save reply context images: {e}")
+4 -3
View File
@@ -268,12 +268,13 @@ def avatar(name: str):
def preview(name: str):
"""A saved copy of a replied-to post's image (v2.19.0)"""
from pathlib import Path
from starpunk.avatars import NAME_RE, preview_folder
from starpunk.avatars import NAME_RE, VIDEO_NAME_RE, preview_folder
if not NAME_RE.match(name):
if not (NAME_RE.match(name) or VIDEO_NAME_RE.match(name)):
abort(404)
response = send_from_directory(
preview_folder(Path(current_app.config.get('DATA_PATH', 'data'))), name)
preview_folder(Path(current_app.config.get('DATA_PATH', 'data'))), name,
mimetype='video/mp4' if name.endswith('.mp4') else None)
response.headers['Cache-Control'] = 'public, max-age=604800'
return response
+5 -1
View File
@@ -34,7 +34,11 @@
{% if ctx.title %}<p class="p-name reply-title">{{ ctx.title }}</p>{% endif %}
{% if ctx.content_snippet %}<div class="e-content reply-content">{{ ctx.content_snippet|safe }}</div>{% endif %}
{% set image = ctx.photo|local_preview %}
{% if image %}<img class="reply-photo" src="{{ image }}" alt="{{ ctx.photo_alt or '' }}" loading="lazy">{% endif %}
{% set video = ctx.video|local_video if ctx.video is defined else None %}
{% if video %}
{# Mastodon "GIFs" are silent looping MP4s: shown as Mastodon shows them (v2.20.1) #}
<video class="reply-photo" src="{{ video }}"{% if image %} poster="{{ image }}"{% endif %} autoplay loop muted playsinline{% if ctx.photo_alt %} aria-label="{{ ctx.photo_alt }}" title="{{ ctx.photo_alt }}"{% endif %}></video>
{% elif image %}<img class="reply-photo" src="{{ image }}" alt="{{ ctx.photo_alt or '' }}" loading="lazy">{% endif %}
<footer class="reply-source">
<a class="u-url" href="{{ url }}">{{ _when(ctx, '%B %-d, %Y · %-I:%M %p') }}{% if ctx.published_at and ctx.published_at.strftime is defined %} · {% endif %}{{ ctx.site_name or url.split('/')[2] }} ↗</a>
</footer>
+56
View File
@@ -268,3 +268,59 @@ def test_reply_page_and_home_page_show_the_context(app, web):
reply_to = entry["properties"]["in-reply-to"][0]
assert (reply_to if isinstance(reply_to, str) else reply_to["properties"]["url"][0]) == TOOT
assert "Ada Lovelace" not in json.dumps(entry["properties"].get("author", ""))
# --- Moving pictures (v2.20.1) --------------------------------------------------
MP4 = b"\x00\x00\x00\x18ftypmp42" + b"\x00" * 64
def animated_gif():
buffer = io.BytesIO()
frames = [Image.new("RGB", (800, 400), color) for color in ("red", "green", "blue")]
frames[0].save(buffer, "GIF", save_all=True, append_images=frames[1:], duration=80, loop=0)
return buffer.getvalue()
def test_mastodon_gifv_is_kept_as_a_looping_video(app, web, monkeypatch):
status = {**STATUS, "media_attachments": [{
"type": "gifv", "url": "https://masto.example/media/original.mp4",
"preview_url": "https://masto.example/media/small.png", "description": "Engineering report"}]}
web.routes["https://masto.example/api/v1/statuses/117611186265622833"] = \
lambda r: httpx.Response(200, json=status)
web.routes["https://masto.example/media/original.mp4"] = \
lambda r: httpx.Response(200, content=MP4, headers={"Content-Type": "video/mp4"})
note = _reply(app, TOOT, "Same here")
context = fetch(app, TOOT)
assert context["video"] == "https://masto.example/media/original.mp4"
assert avatars.cached_video(app.config["DATA_PATH"], context["video"])
client = app.test_client()
page = client.get(f"/note/{note.slug}").data.decode()
assert "<video" in page and "autoplay loop muted playsinline" in page
assert 'aria-label="Engineering report"' in page
src = page.split('<video class="reply-photo" src="', 1)[1].split('"', 1)[0]
response = client.get(src)
assert response.status_code == 200 and response.mimetype == "video/mp4"
def test_animated_gif_stays_animated(app, web):
web.routes[ARTICLE] = lambda r: httpx.Response(200, html=ARTICLE_PAGE.replace("/img/lead.png", "/img/lead.gif"))
web.routes["https://news.example/img/lead.gif"] = \
lambda r: httpx.Response(200, content=animated_gif(), headers={"Content-Type": "image/gif"})
context = fetch(app, ARTICLE)
data = app.config["DATA_PATH"]
with Image.open(avatars.preview_folder(data) / avatars.cached_preview(data, context["photo"])) as image:
assert image.n_frames == 3
assert image.width == avatars.PREVIEW_WIDTH
def test_unused_videos_are_removed(app, web):
data = app.config["DATA_PATH"]
folder = avatars.preview_folder(data)
folder.mkdir(parents=True, exist_ok=True)
stray = folder / avatars.video_name_for("https://gone.example/x.mp4")
stray.write_bytes(MP4)
avatars.remove_unused(app.config["DATABASE_PATH"], data)
assert not stray.exists()