Skip to content

Commit 23be416

Browse files
authored
feat(media): support audio and video tag with multi sources (#1618)
1 parent 01076cb commit 23be416

File tree

5 files changed

+132
-6
lines changed

5 files changed

+132
-6
lines changed

_data/media.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- extension: mp3
2+
mime_type: mpeg
3+
- extension: mov
4+
mime_type: quicktime
5+
- extension: avi
6+
mime_type: x-msvideo
7+
- extension: mkv
8+
mime_type: x-matroska
9+
- extension: ogv
10+
mime_type: ogg
11+
- extension: weba
12+
mime_type: webm
13+
- extension: 3gp
14+
mime_type: 3gpp
15+
- extension: 3g2
16+
mime_type: 3gpp2
17+
- extension: mid
18+
mime_type: midi

_includes/embed/audio.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% assign src = include.src | strip %}
2+
{% assign title = include.title | strip %}
3+
{% assign types = include.types | default: '' | strip | split: '|' %}
4+
5+
{% unless src contains '://' %}
6+
{%- capture src -%}
7+
{% include img-url.html src=src %}
8+
{%- endcapture -%}
9+
{% endunless %}
10+
11+
<p>
12+
<audio class="embed-audio" controls>
13+
{% assign extension = src | split: '.' | last %}
14+
{% assign types = extension | concat: types %}
15+
16+
{% assign ext_size = extension | size %}
17+
{% assign src_size = src | size %}
18+
{% assign slice_size = src_size | minus: ext_size %}
19+
20+
{% assign filepath = src | slice: 0, slice_size %}
21+
22+
{% for type in types %}
23+
{% assign src = filepath | append: type %}
24+
{% assign media_item = site.data.media | find: 'extension', type %}
25+
{% assign mime_type = media_item.mime_type | default: type %}
26+
<source src="{{ src }}" type="audio/{{ mime_type }}">
27+
{% endfor %}
28+
29+
Your browser does not support the audio tag. Here is a
30+
<a href="{{ src | strip }}">link to the audio file</a> instead.
31+
</audio>
32+
{% if title %}
33+
<em>{{ title }}</em>
34+
{% endif %}
35+
</p>

_includes/embed/video.html

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{% assign video_url = include.src %}
2+
{% assign title = include.title %}
23
{% assign poster_url = include.poster %}
4+
{% assign types = include.types | default: '' | strip | split: '|' %}
35

46
{% unless video_url contains '://' %}
57
{%- capture video_url -%}
6-
{% include img-url.html src=video_url img_path=page.img_path %}
8+
{% include img-url.html src=video_url %}
79
{%- endcapture -%}
810
{% endunless %}
911

@@ -31,8 +33,27 @@
3133
{% endif %}
3234

3335
<p>
34-
<video class="embed-video file" src="{{ video_url }}" {{ poster }} {{ attributes }}>
35-
Your browser doesn't support HTML video. Here is a <a href="{{ video_url }}">link to the video</a> instead.
36+
<video class="embed-video file" {{ poster }} {{ attributes }}>
37+
{% assign extension = video_url | split: '.' | last %}
38+
{% assign types = extension | concat: types %}
39+
40+
{% assign ext_size = extension | size %}
41+
{% assign src_size = video_url | size %}
42+
{% assign slice_size = src_size | minus: ext_size %}
43+
44+
{% assign filepath = video_url | slice: 0, slice_size %}
45+
46+
{% for type in types %}
47+
{% assign src = filepath | append: type %}
48+
{% assign media_item = site.data.media | find: 'extension', type %}
49+
{% assign mime_type = media_item.mime_type | default: type %}
50+
<source src="{{ src }}" type="video/{{ mime_type }}">
51+
{% endfor %}
52+
53+
Your browser does not support the video tag. Here is a
54+
<a href="{{ video_url | strip }}">link to the video file</a> instead.
3655
</video>
37-
<em>{{ include.title }}</em>
56+
{% if title %}
57+
<em>{{ title }}</em>
58+
{% endif %}
3859
</p>

_posts/2019-08-08-write-a-new-post.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,59 @@ You can also specify additional attributes for the embedded video file. Here is
472472
- `autoplay=true` - video automatically begins to play back as soon as it can
473473
- `loop=true` - automatically seek back to the start upon reaching the end of the video
474474
- `muted=true` - audio will be initially silenced
475+
- `types` - specify the extensions of additional video formats separated by `|`. Ensure these files exist in the same directory as your primary video file.
475476

476477
Consider an example utilizing all of the above:
477478

478479
```liquid
479-
{% include embed/video.html src='video.mp4' poster='poster.png' title='Demo video'
480-
autoplay=true loop=true muted=true %}
480+
{%
481+
include embed/video.html
482+
src='/path/to/video/video.mp4'
483+
types='ogg|mov'
484+
poster='poster.png'
485+
title='Demo video'
486+
autoplay=true
487+
loop=true
488+
muted=true
489+
%}
490+
```
491+
492+
> It's not recommended to host video files in `assets` folder as they cannot be cached by PWA and may cause issues.
493+
> Instead, use CDN to host video files. Alternatively, use a separate folder that is excluded from PWA (see `pwa.deny_paths` setting in `_config.yml`).
494+
{: .prompt-warning }
495+
496+
## Audios
497+
498+
### Audio File
499+
500+
If you want to embed an audio file directly, use the following syntax:
501+
502+
```liquid
503+
{% include embed/audio.html src='{URL}' %}
504+
```
505+
506+
Where `URL` is an URL to an audio file e.g. `/assets/img/sample/audio.mp3`.
507+
508+
You can also specify additional attributes for the embedded audio file. Here is a full list of attributes allowed.
509+
510+
- `title='Text'` - title for an audio that appears below the audio and looks same as for images
511+
- `types` - specify the extensions of additional audio formats separated by `|`. Ensure these files exist in the same directory as your primary audio file.
512+
513+
Consider an example utilizing all of the above:
514+
515+
```liquid
516+
{%
517+
include embed/audio.html
518+
src='/path/to/audio/audio.mp3'
519+
types='ogg|wav|aac'
520+
title='Demo audio'
521+
%}
481522
```
482523

524+
> It's not recommended to host audio files in `assets` folder as they cannot be cached by PWA and may cause issues.
525+
> Instead, use CDN to host audio files. Alternatively, use a separate folder that is excluded from PWA (see `pwa.deny_paths` setting in `_config.yml`).
526+
{: .prompt-warning }
527+
483528
## Learn More
484529

485530
For more knowledge about Jekyll posts, visit the [Jekyll Docs: Posts](https://jekyllrb.com/docs/posts/).

_sass/addon/commons.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ main {
575575
@extend %img-caption;
576576
}
577577

578+
.embed-audio {
579+
width: 100%;
580+
display: block;
581+
582+
@extend %img-caption;
583+
}
584+
578585
/* --- buttons --- */
579586
.btn-lang {
580587
border: 1px solid !important;

0 commit comments

Comments
 (0)