Skip to content

Commit

Permalink
refactor: better video support (#2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Dec 4, 2024
1 parent 870d7b5 commit fbc4fe6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions resources/layouts/_default/page.iframe.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
{%- if page.videos is defined ~%}
<video id="player" playsinline controls preload="auto"{% if page.poster|default(page.image|default) %} poster="{{ asset(page.poster|default(page.image|default)) }}"{% endif %}>
{%- for video in page.videos ~%}
{%- set asset = asset(video) ~%}
<source src="{{ url(asset) }}" type="{{ asset.subtype }}"/>
{%- set asset_video = asset(video) ~%}
<source src="{{ url(asset_video) }}" type="{{ asset_video.subtype }}"/>
{%- endfor ~%}
</video>
{%- else ~%}
Expand Down
4 changes: 4 additions & 0 deletions resources/layouts/_default/page.oembed.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
{%- endif ~%}
"provider_name": "{{ site.title }}",
"provider_url": "{{ url('/', {canonical: true}) }}",
{%- if author.name is defined ~%}
"author_name": "{{ author.name }}",
{%- endif ~%}
{%- if author.url is defined ~%}
"author_url": "{{ author.url }}",
{%- endif ~%}
"cache_age": 3600,
{%- if 'iframe' in page.output ~%}
"embed_url": "{{ url(page, {canonical: true, format: 'iframe'}) }}",
{%- endif ~%}
{%- if opengraph.description is defined ~%}
"description": "{{ opengraph.description }}"
{%- endif ~%}
}
{% endblock %}
8 changes: 5 additions & 3 deletions resources/layouts/partials/jsonld.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@
"@context": "https://schema.org",
"@type": "VideoObject",
"name": "{{ title|default(page.title)|e }}",
{%- if page.description is defined ~%}
"description": "{{ page.description|e }}",
{%- if page.poster is defined ~%}
{%- endif ~%}
"thumbnailUrl": [
"{{ url(asset(page.poster), {canonical: true}) }}"
],
{%- endif ~%}
"uploadDate": "{{ page.date|date('Y-m-dTH:i:sP') }}",
"duration": "PT1M54S",
{%- if page.duration is defined ~%}
"duration": "{{ page.duration|duration_to_iso8601 }}",
{%- endif ~%}
"contentUrl": "{{ url(opengraph.video, {canonical: true}) }}",
"embedUrl": "{{ url(page, {canonical: true, format: 'iframe'}) }}"
}
Expand Down
2 changes: 2 additions & 0 deletions src/Renderer/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public function getFilters(): array
new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']),
new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']),
new \Twig\TwigFilter('splitline', [$this, 'splitLine']),
// date
new \Twig\TwigFilter('duration_to_iso8601', ['\Cecil\Util\Date', 'durationToIso8601']),
];
}

Expand Down
14 changes: 14 additions & 0 deletions src/Util/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ public static function toDatetime($date): \DateTime

return new \DateTime($date);
}

/**
* Duration in seconds to ISO 8601.
* e.g.: '00:00:46.70' -> 'T0M46S'
*/
public static function durationToIso8601(string $duration): string
{
$time = new \DateTime($duration);
$midnight = new \DateTime();
$midnight->setTime(0, 0);
$period = $midnight->diff($time);

return $period->format('T%iM%SS');
}
}
10 changes: 10 additions & 0 deletions tests/fixtures/website/layouts/video.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends '_default/page.html.twig' %}

{% block content %}
<video controls width="100%">
{%- for video in page.videos ~%}
{%- set asset_video = asset(video) ~%}
<source src="{{ url(asset_video) }}" type="{{ asset_video.subtype }}" />
{%- endfor ~%}
</video>
{% endblock %}
12 changes: 12 additions & 0 deletions tests/fixtures/website/pages/Assets/video.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Video
image: video/poster.png

duration: 00:00:13
videos:
- video/test.mp4
poster: video/poster.png

output: [html, iframe, oembed]
layout: video
---
Binary file added tests/fixtures/website/static/video/poster.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fbc4fe6

Please sign in to comment.