Merge branch transcoding-order

Apparantly the order of <source> tags in the <video> element
determines what transcoding a browser chooses. Until now this
was quite random.

Now show transcodings (<source> elements) in the order:
vp9, vp8, h264
This commit is contained in:
2022-04-06 20:11:09 +02:00
5 changed files with 96 additions and 20 deletions

View File

@@ -9,5 +9,5 @@ class GetShortNameOfTranscodingTypeTestCase(SimpleTestCase):
def test_gets_transcoding_by_transcoding_object(self):
result = get_short_name_of_transcoding_type(TranscodingType(name='Looooong naaaaame', short_name='shrt nm',
description='Some Description'))
description='Some Description', priority=1))
self.assertEqual(result, 'shrt nm')

View File

@@ -0,0 +1,15 @@
from django.test import SimpleTestCase
from videodinges.models import get_transcoding_type_by_name
class GetTranscodingTypeByNameTestCase(SimpleTestCase):
def test_returns_transcoding_type_if_listed(self):
result = get_transcoding_type_by_name('video/webm; codecs="vp9, opus"')
self.assertEqual(result.name, 'video/webm; codecs="vp9, opus"')
self.assertEqual(result.short_name, 'vp9')
self.assertEqual(result.description, 'WebM with VP9 and Opus')
self.assertEqual(result.priority, 100)
def test_returns_none_if_not_listed(self):
result = get_transcoding_type_by_name('non-existent')
self.assertIsNone(result)

View File

@@ -59,8 +59,8 @@ class VideoTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
</video>""",
content,
@@ -129,8 +129,8 @@ class VideoTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="1280" height="720" controls="controls">
<source src="http://720p.mp4" type='video/mp4' />
<source src="http://720p.webm" type='video/webm; codecs="vp8, vorbis"' />
<source src="http://720p.mp4" type='video/mp4' />
You need a browser that understands HTML5 video and supports h.264 or vp8 codecs.
</video>""",
content,
@@ -190,8 +190,8 @@ class VideoTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="1280" height="720" controls="controls">
<source src="http://720p.mp4" type='video/mp4' />
<source src="http://720p.webm" type='video/webm; codecs="vp8, vorbis"' />
<source src="http://720p.mp4" type='video/mp4' />
You need a browser that understands HTML5 video and supports h.264 or vp8 codecs.
</video>""",
content,
@@ -245,6 +245,52 @@ class VideoTestCase(UploadMixin, TestCase):
content,
)
def test_video_view_renders_transcoding_types_in_correct_order(self):
video = factories.create(
models.Video,
title='Vid 1',
slug='vid-1',
default_quality='480p',
)
factories.create(
models.Transcoding,
video=video,
quality='480p',
type='video/mp4; codecs="avc1.64001e,mp4a.40.2"',
url='http://480p.mp4',
)
factories.create(
models.Transcoding,
video=video,
quality='480p',
type='video/webm; codecs="vp9, opus"',
url='http://480p.vp9.webm',
)
factories.create(
models.Transcoding,
video=video,
quality='480p',
type='video/webm; codecs="vp8, vorbis"',
url='http://480p.vp8.webm',
)
resp:HttpResponse = self.client.get(reverse('video', args=['vid-1']))
self.assertEqual(resp.status_code, 200)
content:str = resp.content.decode(resp.charset)
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.vp9.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.vp8.webm" type='video/webm; codecs="vp8, vorbis"' />
<source src="http://480p.mp4" type='video/mp4; codecs="avc1.64001e,mp4a.40.2"' />
You need a browser that understands HTML5 video and supports h.264 or vp8 or vp9 codecs.
</video>""",
content,
)
class VideoWithTrackTestCase(UploadMixin, TestCase):
def setUp(self):
@@ -287,8 +333,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="/uploads/some_file.txt" srclang="en" kind="subtitles" label="en" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
</video>""",
@@ -317,8 +363,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
f"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="{ track_en.upload.file.url }" srclang="en" kind="subtitles" label="en" />
<track src="{ track_nl.upload.file.url }" srclang="nl" kind="subtitles" label="nl" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
@@ -344,8 +390,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="/uploads/some_file.txt" default="default" srclang="en" kind="subtitles" label="en" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
</video>""",
@@ -375,8 +421,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
f"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="{ track_en.upload.file.url }" default="default" srclang="en" kind="subtitles" label="en" />
<track src="{ track_nl.upload.file.url }" srclang="nl" kind="subtitles" label="nl" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
@@ -401,8 +447,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="/uploads/some_file.txt" srclang="en" kind="subtitles" label="Some Label" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
</video>""",
@@ -427,8 +473,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase):
self.assertInHTML(
"""<video width="853" height="480" controls="controls">
<source src="http://480p.mp4" type='video/mp4' />
<source src="http://480p.webm" type='video/webm; codecs="vp9, opus"' />
<source src="http://480p.mp4" type='video/mp4' />
<track src="/uploads/some_file.txt" srclang="en" kind="captions" label="en" />
You need a browser that understands HTML5 video and supports h.264 or vp9 codecs.
</video>""",

View File

@@ -16,6 +16,7 @@ class TranscodingType(NamedTuple):
name: str
short_name: str
description: str
priority: int
def __str__(self):
return self.name
@@ -28,18 +29,20 @@ qualities = (
)
transcoding_types = (
TranscodingType(name='video/webm', short_name='webm', description='Generic WebM'),
TranscodingType(name='video/webm; codecs="vp8, vorbis"', short_name='vp8', description='WebM with VP8 and Vorbis'),
TranscodingType(name='video/webm; codecs="vp9, opus"', short_name='vp9', description='WebM with VP9 and Opus'),
TranscodingType(name='video/mp4', short_name='h.264', description='Generic MP4 with H.264'),
TranscodingType(name='video/webm', short_name='webm', description='Generic WebM', priority=1),
TranscodingType(name='video/webm; codecs="vp8, vorbis"', short_name='vp8', description='WebM with VP8 and Vorbis',
priority=80),
TranscodingType(name='video/webm; codecs="vp9, opus"', short_name='vp9', description='WebM with VP9 and Opus',
priority=100),
TranscodingType(name='video/mp4', short_name='h.264', description='Generic MP4 with H.264', priority=1),
TranscodingType(name='video/mp4; codecs="avc1.64001e,mp4a.40.2"', short_name='h.264',
description='MP4 with H.264 (AVC1 profile High, Level 3.0) and AAC-LC'),
description='MP4 with H.264 (AVC1 profile High, Level 3.0) and AAC-LC', priority=50),
TranscodingType(name='video/mp4; codecs="avc1.64001f,mp4a.40.2"', short_name='h.264',
description='MP4 with H.264 (AVC1 profile High, Level 3.1) and AAC-LC'),
description='MP4 with H.264 (AVC1 profile High, Level 3.1) and AAC-LC', priority=50),
TranscodingType(name='video/mp4; codecs="avc1.640028,mp4a.40.2"', short_name='h.264',
description='MP4 with H.264 (AVC1 profile High, Level 4.0) and AAC-LC'),
description='MP4 with H.264 (AVC1 profile High, Level 4.0) and AAC-LC', priority=50),
TranscodingType(name='video/mp4; codecs="avc1.640032,mp4a.40.2"', short_name='h.264',
description='MP4 with H.264 (AVC1 profile High, Level 5.0) and AAC-LC'),
description='MP4 with H.264 (AVC1 profile High, Level 5.0) and AAC-LC', priority=50),
)
class Upload(models.Model):
@@ -140,6 +143,11 @@ def get_quality_by_name(name: str) -> Optional[Quality]:
if quality.name == name:
return quality
def get_transcoding_type_by_name(name: str) -> Optional[TranscodingType]:
for t in transcoding_types:
if t.name == name:
return t
def get_short_name_of_transcoding_type(transcoding_type: Union[str, TranscodingType]) -> str:
if isinstance(transcoding_type, str):
for type_ in transcoding_types:

View File

@@ -37,12 +37,19 @@ def video(request: HttpRequest, slug: str) -> HttpResponse:
current_quality=quality[0].quality_obj.name
)
template_data['sources'] = [
sources = [
{
'src': _url_for(transcoding),
'type': transcoding.type,
}
for transcoding in quality ]
for transcoding in quality
]
# sort by transcoding type priority
sources.sort(
key=lambda i: models.get_transcoding_type_by_name(i['type']).priority,
reverse=True
)
template_data['sources'] = sources
template_data['used_codecs'] = [
models.get_short_name_of_transcoding_type(transcoding.type)