-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
124 lines (95 loc) · 3.5 KB
/
worker.py
File metadata and controls
124 lines (95 loc) · 3.5 KB
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
import os
from utils import Log
from models import VideoStatus
import traceback
from exceptions import TranscodingException
from tasks import (
VideoStatusHandler,
VideoDownloader,
VideoTranscoder,
S3Uploader
)
# Prevent module imports
if __name__ != "__main__":
print("Module not meant to be imported. Please run as a script.")
exit(1)
# Load environment variables
DEBUG = os.getenv("DEBUG", "false") == "true"
VIDEO_S3_KEY: str = os.getenv("VIDEO_S3_KEY").strip('"')
VIDEO_ID: str = VIDEO_S3_KEY.split("/", maxsplit=3)[1]
# Initialize logger
log = Log()
# Initialize video status handler
status_handler = VideoStatusHandler(debug=DEBUG, video_id=VIDEO_ID)
try:
# Initialize tasks
downloader = VideoDownloader(log=log)
transcoder = VideoTranscoder(log=log)
uploader = S3Uploader(log=log)
"""
Start executing tasks...
"""
# Mark video status as 'processing'
status_handler.update_status(VideoStatus.PROCESSING)
# Download video to local storage
log.info("Downloading video from S3...")
source_video_path: str = downloader.download(key=VIDEO_S3_KEY)
log.info(f"Video downloaded to: {source_video_path}")
# Delete original video from S3
log.info("Deleting original video from S3...")
downloader.delete_original_object(key=VIDEO_S3_KEY)
# Transcode video to HLS format
log.info("Transcoding video to HLS format...")
try: video_duration: float = transcoder.transcode(source_video_path)
except TranscodingException as e: # Handle custom transcoding exception
log.error(f"Failed to transcode video: {e}")
status_handler.update_status(
VideoStatus.FAILED,
processing_error=e.error_code
)
exit(1)
log.info("Video transcoding completed successfully")
# Delete source video from local storage
os.remove(source_video_path)
# Upload segments and manifest to S3
log.info("Uploading segment files to S3...")
try:
s3_upload_root_key: str = VIDEO_S3_KEY.rsplit("/", maxsplit=1)[0] + "/"
uploader.upload_hls_dir(
hls_dir=transcoder.OUTPUT_DIR,
root_object_key=s3_upload_root_key
)
except Exception as upload_error:
log.error(f"Failed to upload hls files to S3: {upload_error}")
# Reflect failure in video status
status_handler.update_status(
VideoStatus.FAILED,
processing_error="processing_error"
)
# Rollback any uploaded files in S3
log.info("Rolling back uploaded files in S3...")
uploader.rollback_uploads(
root_object_key=s3_upload_root_key
)
exit(1)
log.info("HLS files uploaded to S3 successfully")
# Update video status to 'ready'
status_handler.update_status(
status=VideoStatus.READY,
manifest_s3_key=s3_upload_root_key + transcoder.MANIFEST_FILENAME,
video_duration=video_duration
)
log.info("Video processing complete. Exiting...")
# Handle unexpected exceptions
except Exception:
log.error(f"An unexpected error occurred")
traceback.print_exc()
# Attempt to update status to 'failed' if possible
try:
status_handler.update_status(
VideoStatus.FAILED,
processing_error="processing_error"
)
except Exception as status_update_error:
log.error(f"Failed to update video status: {status_update_error}")
exit(1)