animated preview

This commit is contained in:
j 2025-02-01 12:38:46 +05:30
parent 3656a6f34c
commit 5ad72d2faf
2 changed files with 51 additions and 3 deletions

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import html
import os
import subprocess
from django.conf import settings
from django.core.validators import MaxLengthValidator
@ -162,6 +164,53 @@ class Content(models.Model):
if src:
return mark_safe('<a href="{}"><img src="{}"></a>'.format(self.get_absolute_url(), src))
def og_meta_headers(self):
headers = []
values = [
('og:title', self.title),
]
if self.photo and self.photo.image.url.endswith('.gif'):
video_path = c.photo.image.path.replace('.gif', '.mp4')
image_path = c.photo.image.path.replace('.gif', '.jpg')
if not os.path.exists(video_path):
cmd = [
'ffmpeg',
'-hide_banner', '-nostats', '-loglevel', 'error',
'-i', c.photo.image.path, video_path
]
subprocess.call(cmd)
if not os.path.exists(image_path):
cmd = [
'ffmpeg',
'-hide_banner', '-nostats', '-loglevel', 'error',
'-i', c.photo.image.path, '-frames:v', '1', image_path
]
subprocess.call(cmd)
if os.path.exists(video_path):
video_url = self.photo.image.url.replace('.gif', '.mp4')
if video_url.startswith('/'):
video_url = 'https://studio.camp' + video_url
image_url = video_url.replace('.mp4', '.jpg')
width = self.photo.image.width
height = self.photo.image.height
values = [
('og:video', video_url),
('og:video:type', "video/mp4"),
('og:video:width', str(width)),
('og:video:height', str(height)),
('og:image', image_url),
] + values
else:
values += [
('og:image', 'https://studio.camp%s' % self.image_url),
]
for key, value in values:
headers.append(
'<meta property="%s" content="%s"/>' % (html.escape(key, True), html.escape(value, True))
)
return mark_safe("\n".join(headers))
@cached_property
def image_url(self):
if self.photo:

View File

@ -1,7 +1,6 @@
<title>{{ content.title }} - CAMP</title>
<meta name="title" content="{{ content.title }}"/>
<meta property="og:title" content="{{ content.title }}"/>
{{ content.og_meta_headers }}
<meta content="https://studio.camp{{ content.image_url }}" name="thumbnail"/>
<meta content="https://studio.camp{{ content.image_url }}" name="image_src"/>
<meta property="og:image" content="https://studio.camp{{ content.image_url }}"/>