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:
1 parent
03a1c8fcea
commit
952ca300c0
9 files changed
+193
-19
No files matched your search
+10
-2
@@ -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
@@ -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
|
||||
|
||||
@@ -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}")
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user