-
Notifications
You must be signed in to change notification settings - Fork 0
/
step-transcode-mobile.py
162 lines (142 loc) · 6.1 KB
/
step-transcode-mobile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#
# This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
# Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
#
# The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
#
import boto3
import tempfile
import os
from module.logger import get_logger
from media_tools import (
get_file_mime,
get_video_metadata,
video_encode_for_mobile,
ffmpeg_barebones_transcode,
get_video_encoding_type,
)
from module.api import (
UpdateTaskStatusRequest,
AnswerUpdateRequest,
upload_task_status_update,
upload_answer_and_task_status_update,
)
from module.utils import s3_bucket, load_sentry, fetch_from_graphql
from module.constants import Supported_Video_Type, MP4, WEBM_VP9
load_sentry()
log = get_logger("answer-transcode-mobile-handler")
s3 = boto3.client("s3")
def transcode_mobile(
video_file,
video_file_type: Supported_Video_Type,
s3_path,
maintain_original_aspect_ratio,
):
work_dir = os.path.dirname(video_file)
target_file = f"mobile.{video_file_type.extension}"
target_file_path = os.path.join(work_dir, target_file)
video_encode_for_mobile(
video_file,
target_file_path,
video_file_type.mime,
maintain_original_aspect_ratio=maintain_original_aspect_ratio,
)
log.info("uploading %s to %s/%s", target_file_path, s3_bucket, s3_path)
s3.upload_file(
target_file_path,
s3_bucket,
f"{s3_path}/{target_file}",
ExtraArgs={"ContentType": video_file_type.mime},
)
# webm are also transcoded to mp4 for browsers that do not support webm
if video_file_type.mime == "video/webm":
mp4_target_file = "mobile.mp4"
mp4_target_file_path = os.path.join(work_dir, mp4_target_file)
ffmpeg_barebones_transcode(target_file_path, mp4_target_file_path)
log.info("uploading %s to %s/%s", mp4_target_file_path, s3_bucket, s3_path)
s3.upload_file(
mp4_target_file_path,
s3_bucket,
f"{s3_path}/{mp4_target_file}",
ExtraArgs={"ContentType": "video/mp4"},
)
def process_task(request):
log.info("video to process %s", request["video"])
auth_headers = request["authHeaders"]
maintain_original_aspect_ratio = request["maintain_original_aspect_ratio"]
stored_task = fetch_from_graphql(
request["mentor"], request["question"], "transcodeMobileTask", auth_headers
)
if not stored_task:
log.warning("task not found, skipping transcode")
return
if stored_task["status"].startswith("CANCEL"):
log.info("task cancelled, skipping transcription")
return
with tempfile.TemporaryDirectory() as work_dir:
work_file = os.path.join(work_dir, "original_video")
s3.download_file(s3_bucket, request["video"], work_file)
is_vbg_video = request["isVbgVideo"] if "isVbgVideo" in request else False
if is_vbg_video:
try:
file_mime_type = get_file_mime(work_file)
file_encoding = get_video_encoding_type(work_file)
if file_mime_type == "video/webm" and file_encoding == "vp9":
desired_video_file_type = WEBM_VP9
else:
desired_video_file_type = MP4
except Exception as e:
log.info(
f"Failed to determine mime and encoding type for {work_file}, defaulting to mp4"
)
log.info(e)
desired_video_file_type = MP4
else:
desired_video_file_type = MP4
s3_path = os.path.dirname(request["video"]) # same 'folder' as original file
log.info("%s downloaded to %s", request["video"], work_dir)
upload_task_status_update(
UpdateTaskStatusRequest(
mentor=request["mentor"],
question=request["question"],
transcode_mobile_task={"status": "IN_PROGRESS"},
),
auth_headers,
)
transcode_mobile(
work_file, desired_video_file_type, s3_path, maintain_original_aspect_ratio
)
video_metadata_string, duration, video_hash = get_video_metadata(work_file)
mobile_media = {
"duration": duration,
"hash": video_hash,
"stringMetadata": video_metadata_string,
"type": "video",
"tag": "mobile",
"url": f"{s3_path}/mobile.mp4", # mp4's are always created
"transparentVideoUrl": f"{s3_path}/mobile.webm"
if desired_video_file_type.mime == "video/webm"
else "", # webms are also created if the mime type is webm
}
upload_answer_and_task_status_update(
AnswerUpdateRequest(
mentor=request["mentor"],
question=request["question"],
mobile_media=mobile_media,
),
UpdateTaskStatusRequest(
mentor=request["mentor"],
question=request["question"],
transcode_mobile_task={"status": "DONE"},
mobile_media=mobile_media,
),
auth_headers,
)
def handler(event, context):
log.info(event)
request = event["request"]
task = request["transcodeMobileTask"] if "transcodeMobileTask" in request else None
if not task:
log.warning("no transcoding-mobile task requested")
return
process_task(request)