Skip to content

Commit db8b203

Browse files
authored
added handling for multiple transcodig profiles (#223)
* added handling for multiple transcodig profiles * updated env variable name * added changelog files * added a param for excluding file group * added changelog * added common settings and removed version specs for mitol common
1 parent 8b3a3ca commit db8b203

File tree

8 files changed

+199
-16
lines changed

8 files changed

+199
-16
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Removed
10+
11+
- A bullet item for the Removed category.
12+
13+
-->
14+
<!--
15+
### Added
16+
17+
- A bullet item for the Added category.
18+
19+
-->
20+
### Changed
21+
22+
- Updated handling for multiple profiles in transcoding job
23+
24+
<!--
25+
### Deprecated
26+
27+
- A bullet item for the Deprecated category.
28+
29+
-->
30+
<!--
31+
### Fixed
32+
33+
- A bullet item for the Fixed category.
34+
35+
-->
36+
<!--
37+
### Security
38+
39+
- A bullet item for the Security category.
40+
41+
-->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Removed
10+
11+
- A bullet item for the Removed category.
12+
13+
-->
14+
<!--
15+
### Added
16+
17+
- A bullet item for the Added category.
18+
19+
-->
20+
### Changed
21+
22+
- Added handling to exclude file group with hls groups
23+
24+
<!--
25+
### Deprecated
26+
27+
- A bullet item for the Deprecated category.
28+
29+
-->
30+
<!--
31+
### Fixed
32+
33+
- A bullet item for the Fixed category.
34+
35+
-->
36+
<!--
37+
### Security
38+
39+
- A bullet item for the Security category.
40+
41+
-->

src/transcoding/mitol/transcoding/api.py

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@
22

33
import json
44
from pathlib import Path
5+
from typing import Optional
56

67
import boto3
78
from django.conf import settings
89

10+
from mitol.transcoding.constants import GroupSettings
911

10-
def media_convert_job(video_source_key: str) -> dict:
12+
13+
def media_convert_job( # noqa: PLR0913
14+
video_source_key: str,
15+
source_prefix: str = settings.VIDEO_S3_UPLOAD_PREFIX,
16+
source_bucket: str = settings.AWS_STORAGE_BUCKET_NAME,
17+
destination_prefix: str = settings.VIDEO_S3_TRANSCODE_PREFIX,
18+
destination_bucket: str = (
19+
settings.VIDEO_S3_TRANSCODE_BUCKET or settings.AWS_STORAGE_BUCKET_NAME
20+
),
21+
group_settings: Optional[dict] = None,
22+
) -> dict:
1123
"""
1224
Create a MediaConvert job for a Video
1325
1426
Args:
1527
video_source_key (str): S3 key for the video source.
28+
source_prefix (str): Prefix for the source video.
29+
source_bucket (str, optional): S3 bucket for the source video.
30+
destination_prefix (str): Prefix for the destination video.
31+
destination_bucket (str): S3 bucket for the transcoded output
32+
group_settings (dict, optional): Settings for output groups. Defaults to None.
1633
1734
Returns:
1835
dict: MediaConvert job details.
1936
"""
20-
source_prefix = settings.DRIVE_S3_UPLOAD_PREFIX
37+
38+
if group_settings is None:
39+
group_settings = {}
40+
2141
client = boto3.client(
2242
"mediaconvert",
2343
region_name=settings.AWS_REGION,
@@ -35,17 +55,72 @@ def media_convert_job(video_source_key: str) -> dict:
3555
job_dict["Role"] = (
3656
f"arn:aws:iam::{settings.AWS_ACCOUNT_ID}:role/{settings.AWS_ROLE_NAME}"
3757
)
38-
source_path = Path(
39-
video_source_key.replace(
40-
source_prefix,
41-
settings.VIDEO_S3_TRANSCODE_PREFIX,
58+
59+
if source_prefix:
60+
destination_path = Path(
61+
video_source_key.replace(
62+
source_prefix,
63+
destination_prefix,
64+
)
4265
)
43-
)
44-
destination = str(source_path.parent / source_path.stem)
45-
job_dict["Settings"]["OutputGroups"][0]["OutputGroupSettings"][
46-
"FileGroupSettings"
47-
]["Destination"] = f"s3://{settings.AWS_STORAGE_BUCKET_NAME}/{destination}"
66+
else:
67+
destination_path = Path(destination_prefix, video_source_key)
68+
destination = str(destination_path.parent / destination_path.stem)
69+
4870
job_dict["Settings"]["Inputs"][0]["FileInput"] = (
49-
f"s3://{settings.AWS_STORAGE_BUCKET_NAME}/{video_source_key}"
71+
f"s3://{source_bucket}/{video_source_key}"
5072
)
73+
74+
add_group_settings(job_dict, destination, destination_bucket, group_settings)
75+
5176
return client.create_job(**job_dict)
77+
78+
79+
def add_group_settings(
80+
job_dict: dict,
81+
destination: str,
82+
destination_bucket: str,
83+
group_settings: dict,
84+
) -> None:
85+
"""
86+
Add group settings to the MediaConvert job dictionary.
87+
88+
Args:
89+
job_dict (dict): MediaConvert job dictionary.
90+
destination (str): Destination path for the output files.
91+
group_settings (dict): Group settings for the job.
92+
destination_bucket (str, optional): S3 bucket for the transcoded output.
93+
"""
94+
95+
exclude_mp4 = group_settings.get("exclude_mp4", False)
96+
output_groups = job_dict["Settings"]["OutputGroups"]
97+
98+
if exclude_mp4:
99+
# Remove the MP4 output group if not needed
100+
output_groups = [
101+
group
102+
for group in output_groups
103+
if group["OutputGroupSettings"]["Type"] != GroupSettings.FILE_GROUP_SETTINGS
104+
]
105+
106+
for group in output_groups:
107+
output_group_settings = group["OutputGroupSettings"]
108+
group_settings_type = group["OutputGroupSettings"]["Type"]
109+
if group_settings_type == GroupSettings.HLS_GROUP_SETTINGS:
110+
group_settings_key = GroupSettings.HLS_GROUP_SETTINGS_KEY
111+
output_group_settings[group_settings_key]["SegmentLength"] = (
112+
group_settings.get("SegmentLength", 10)
113+
)
114+
output_group_settings[group_settings_key]["AdditionalManifests"][0][
115+
"ManifestNameModifier"
116+
] = group_settings.get("ManifestNameModifier", "__index")
117+
elif group_settings_type == GroupSettings.FILE_GROUP_SETTINGS:
118+
group_settings_key = GroupSettings.FILE_GROUP_SETTINGS_KEY
119+
else:
120+
group_type = group_settings_type
121+
error_msg = f"Unsupported group settings type: {group_type}"
122+
raise ValueError(error_msg)
123+
124+
output_group_settings[group_settings_key]["Destination"] = (
125+
f"s3://{destination_bucket}/{destination}"
126+
)

src/transcoding/mitol/transcoding/apps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class Transcoding(AppConfig):
1616
"VIDEO_TRANSCODE_QUEUE",
1717
"VIDEO_S3_TRANSCODE_ENDPOINT",
1818
"VIDEO_S3_TRANSCODE_PREFIX",
19-
"DRIVE_S3_UPLOAD_PREFIX",
19+
"VIDEO_S3_UPLOAD_PREFIX",
2020
"AWS_STORAGE_BUCKET_NAME",
21+
"VIDEO_S3_TRANSCODE_BUCKET",
2122
"AWS_REGION",
2223
"AWS_ACCOUNT_ID",
2324
"AWS_ROLE_NAME",

src/transcoding/mitol/transcoding/constants.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@
66
"TopicArn=arn:aws:sns:{AWS_REGION}:{AWS_ACCOUNT_ID}:"
77
"MediaConvertJobAlert&Token={TOKEN}"
88
)
9+
10+
11+
class GroupSettings:
12+
"""
13+
Constants for AWS MediaConvert group settings types used in job configuration.
14+
"""
15+
16+
HLS_GROUP_SETTINGS = "HLS_GROUP_SETTINGS"
17+
HLS_GROUP_SETTINGS_KEY = "HlsGroupSettings"
18+
19+
FILE_GROUP_SETTINGS = "FILE_GROUP_SETTINGS"
20+
FILE_GROUP_SETTINGS_KEY = "FileGroupSettings"

src/transcoding/mitol/transcoding/settings/job.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@
1212
default="Default",
1313
description=("Name of MediaConvert queue to use for transcoding"),
1414
)
15+
VIDEO_S3_TRANSCODE_BUCKET = get_string(
16+
name="VIDEO_S3_TRANSCODE_BUCKET",
17+
default="",
18+
description=("Bucket to be used for transcoding"),
19+
)
20+
VIDEO_S3_TRANSCODE_PREFIX = get_string(
21+
name="VIDEO_S3_TRANSCODE_PREFIX",
22+
default="",
23+
description=("Prefix for the transcoded video"),
24+
)
25+
VIDEO_S3_UPLOAD_PREFIX = get_string(
26+
name="VIDEO_S3_UPLOAD_PREFIX",
27+
default="",
28+
description=("Prefix for the source video"),
29+
)
1530
POST_TRANSCODE_ACTIONS = get_delimited_list(
1631
name="POST_TRANSCODE_ACTIONS",
1732
default=[],

src/transcoding/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "mitol-django-transcoding"
33
version = "2025.3.17"
44
description = "MIT Open Learning Django app extension for Transcoding jobs"
55
dependencies = [
6-
"mitol-django-common==2023.12.19",
6+
"mitol-django-common",
77
"django-stubs>=1.13.1",
88
"django>=3.0",
99
"boto3==1.37.11",

uv.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)