"""Logic for transcoding job.""" import uuid from typing import Any from owsresponse import response from transcoding.constants import field_const, transcoding from transcoding.logic import messaging from transcoding.models import ( transcoding_job as transcoding_job_model, ) def _generate_unique_filename(container: str) -> str: """Create unique filename for any purpose. Args: container (str): Format container name that is used to generate the extension. Returns: str: Unique filename. """ filename = str(uuid.uuid4()).replace("-", "_") # container value has been already validated extension = field_const.EXTENSION_MAPPING[container] return transcoding.OUTPUT_KEY_PATTERN.format(filename=filename, extension=extension) def create( transcoding_order: dict[str, Any], output_bucket: str, transcoding_rule: dict[str, Any], ) -> response.Response: """Create transcoding job. Args: transcoding_order (dict): Parent transcoding order info. output_bucket (str): Output S3 bucket for transcoded file. transcoding_rule (dict): Transcoding rule info. Returns: response.Response: Transcoding job info or error. """ job_data = { "output_bucket": output_bucket, "output_key": _generate_unique_filename(transcoding_rule["container"]), "container": transcoding_rule.get("container"), "channels": transcoding_rule.get("channels"), "codec": transcoding_rule.get("codec"), "sample_rate": transcoding_rule.get("sample_rate"), "bit_rate": transcoding_rule.get("bit_rate"), "bit_depth": transcoding_rule.get("bit_depth"), } job_response = transcoding_job_model.create_transcoding_job( transcoding_order["transcoding_order_id"], job_data ) if not job_response: return job_response return messaging.send_transcoding_job_message( transcoding_order, job_response.message ) def get_transcoding_job_by_id(transcoding_job_id: int) -> response.Response: """Get transcoding job entry for given transcoding_job_id. Args: transcoding_job_id (int): Transcoding job id. Returns: response.Response: Success or error message. """ return transcoding_job_model.get_transcoding_job_by_id( transcoding_job_id=transcoding_job_id )