From 48473b2a90fe00746794f2d8d5bb8853932e48e5 Mon Sep 17 00:00:00 2001 From: Bastiaan Welmers Date: Wed, 6 Apr 2022 19:52:44 +0200 Subject: [PATCH 1/5] Give priorities to transcoding types --- ...test_get_short_name_of_transcoding_type.py | 2 +- videodinges/models.py | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/videodinges/unit/models/test_get_short_name_of_transcoding_type.py b/tests/videodinges/unit/models/test_get_short_name_of_transcoding_type.py index 1831ae4..80964ff 100644 --- a/tests/videodinges/unit/models/test_get_short_name_of_transcoding_type.py +++ b/tests/videodinges/unit/models/test_get_short_name_of_transcoding_type.py @@ -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') diff --git a/videodinges/models.py b/videodinges/models.py index 885bb70..7b71324 100644 --- a/videodinges/models.py +++ b/videodinges/models.py @@ -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): From 60accdc6185ea2a4eb8268c4e1ea2f996cd0e7a2 Mon Sep 17 00:00:00 2001 From: Bastiaan Welmers Date: Wed, 6 Apr 2022 19:51:41 +0200 Subject: [PATCH 2/5] Add get_transcoding_type_by_name() --- .../models/test_get_transcoding_type_by_name.py | 15 +++++++++++++++ videodinges/models.py | 5 +++++ 2 files changed, 20 insertions(+) create mode 100644 tests/videodinges/unit/models/test_get_transcoding_type_by_name.py diff --git a/tests/videodinges/unit/models/test_get_transcoding_type_by_name.py b/tests/videodinges/unit/models/test_get_transcoding_type_by_name.py new file mode 100644 index 0000000..180fa6c --- /dev/null +++ b/tests/videodinges/unit/models/test_get_transcoding_type_by_name.py @@ -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) diff --git a/videodinges/models.py b/videodinges/models.py index 7b71324..a788574 100644 --- a/videodinges/models.py +++ b/videodinges/models.py @@ -143,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: From aa8f4195c7d096fc16db48bc4ad86def99d3a7f7 Mon Sep 17 00:00:00 2001 From: Bastiaan Welmers Date: Wed, 6 Apr 2022 19:53:29 +0200 Subject: [PATCH 3/5] Order source tags by transcoding type prio --- videodinges/views.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/videodinges/views.py b/videodinges/views.py index 0e2a938..840f0c5 100644 --- a/videodinges/views.py +++ b/videodinges/views.py @@ -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) From 93843fdcad30d65b490499c81a03a88ca8e99d9f Mon Sep 17 00:00:00 2001 From: Bastiaan Welmers Date: Wed, 6 Apr 2022 20:03:17 +0200 Subject: [PATCH 4/5] Fix tests for transcoding type order --- tests/videodinges/views/test_video.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/videodinges/views/test_video.py b/tests/videodinges/views/test_video.py index 4804f36..253a834 100644 --- a/tests/videodinges/views/test_video.py +++ b/tests/videodinges/views/test_video.py @@ -59,8 +59,8 @@ class VideoTestCase(UploadMixin, TestCase): self.assertInHTML( """""", content, @@ -129,8 +129,8 @@ class VideoTestCase(UploadMixin, TestCase): self.assertInHTML( """""", content, @@ -190,8 +190,8 @@ class VideoTestCase(UploadMixin, TestCase): self.assertInHTML( """""", content, @@ -287,8 +287,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase): self.assertInHTML( """""", @@ -317,8 +317,8 @@ class VideoWithTrackTestCase(UploadMixin, TestCase): self.assertInHTML( f"""