mypy-boto3-mediaconvert Type Annotations
mypy-boto3-mediaconvert provides type annotations for the boto3 MediaConvert service, enhancing static analysis and IDE support for Python developers working with AWS. It ensures early error detection and improved code readability. This package is part of the larger `mypy-boto3-builder` ecosystem, which frequently releases updates in sync with `boto3` versions.
Warnings
- breaking Python 3.8 support was removed with mypy-boto3-builder 8.12.0. Projects requiring type annotations must use Python 3.9 or newer.
- breaking TypeDef names for packed method arguments and conflicting TypeDef `Extra` postfixes were changed in mypy-boto3-builder 8.9.0. This might break existing type annotations that used the old naming conventions.
- gotcha `mypy-boto3-mediaconvert` (and other `mypy-boto3` packages) are solely for type-checking and are not runtime dependencies. To avoid unnecessary runtime overhead, always guard type-only imports with `if TYPE_CHECKING:` or ensure `mypy-boto3` packages are only installed in development environments.
- gotcha `mypy-boto3-mediaconvert` does not install `boto3`. You must install `boto3` separately for your application to function at runtime.
- gotcha PyCharm users might experience slow performance with `Literal` overloads. Consider using `boto3-stubs-lite` if performance issues occur.
- gotcha Historically, `boto3-stubs` and `mypy-boto3` were somewhat distinct. `mypy-boto3` (and its per-service packages like `mypy-boto3-mediaconvert`) is the actively maintained and recommended solution. For broad coverage and easier management of `session.client`/`resource` overloads, using `boto3-stubs[service_name]` is often preferred.
Install
-
pip install mypy-boto3-mediaconvert boto3 mypy -
pip install 'boto3-stubs[mediaconvert]' boto3 mypy
Imports
- MediaConvertClient
from mypy_boto3_mediaconvert.client import MediaConvertClient
- CreateJobRequestTypeDef
from mypy_boto3_mediaconvert.type_defs import CreateJobRequestTypeDef
- CreateJobResponseTypeDef
from mypy_boto3_mediaconvert.type_defs import CreateJobResponseTypeDef
- AacAudioDescriptionBroadcasterMixType
from mypy_boto3_mediaconvert.literals import AacAudioDescriptionBroadcasterMixType
- TYPE_CHECKING
from typing import TYPE_CHECKING
- client
from boto3 import client
Quickstart
import os
from typing import TYPE_CHECKING, Dict, Any
import boto3
# These imports are only for type checking, not for runtime execution
if TYPE_CHECKING:
from mypy_boto3_mediaconvert.client import MediaConvertClient
from mypy_boto3_mediaconvert.type_defs import CreateJobRequestTypeDef, CreateJobResponseTypeDef
def create_sample_job(role_arn: str, s3_input_url: str, s3_output_bucket: str) -> Dict[str, Any]:
# In a real application, consider using AWS credentials from environment variables
# or ~/.aws/credentials. For quickstart, we assume they are configured.
# The client variable is explicitly typed for static analysis
client: 'MediaConvertClient' = boto3.client("mediaconvert", region_name=os.environ.get('AWS_REGION', 'us-east-1'))
job_settings: Dict[str, Any] = {
"Inputs": [
{
"AudioSelectors": {"Audio Selector 1": {"DefaultSelection": "DEFAULT"}},
"FileInput": s3_input_url,
}
],
"OutputGroups": [
{
"Name": "File Group",
"OutputGroupSettings": {
"Type": "FILE_GROUP_SETTINGS",
"FileGroupSettings": {"Destination": f"s3://{s3_output_bucket}/"},
},
"Outputs": [
{
"ContainerSettings": {"Container":"MP4","Mp4Settings":{}},
"VideoDescription": {"CodecSettings": {"Codec": "H_264"}},
"AudioDescriptions": [
{"CodecSettings": {"Codec": "AAC"}}
],
}
],
}
],
}
request_params: 'CreateJobRequestTypeDef' = {
"Role": role_arn,
"Settings": job_settings,
"Queue": os.environ.get('MEDIACONVERT_QUEUE_ARN', 'arn:aws:mediaconvert:us-east-1:123456789012:queues/Default')
}
try:
response: 'CreateJobResponseTypeDef' = client.create_job(**request_params)
print("Successfully created MediaConvert job.")
print(f"Job ID: {response['Job']['Id']}")
return response
except client.exceptions.BadRequestException as e:
print(f"Bad request: {e}")
raise
except Exception as e:
print(f"An unexpected error occurred: {e}")
raise
if __name__ == "__main__":
# Example usage: Replace with actual values and ensure AWS credentials/config are set.
# For a runnable example, you need a valid AWS account, MediaConvert permissions,
# an S3 input file, an S3 output bucket, and a MediaConvert queue.
# Using placeholder environment variables for demonstration.
ROLE_ARN = os.environ.get('MEDIACONVERT_ROLE_ARN', 'arn:aws:iam::123456789012:role/MediaConvertRole')
S3_INPUT_URL = os.environ.get('S3_INPUT_VIDEO_URL', 's3://your-input-bucket/input.mp4')
S3_OUTPUT_BUCKET = os.environ.get('S3_OUTPUT_BUCKET', 'your-output-bucket')
if not all([ROLE_ARN, S3_INPUT_URL, S3_OUTPUT_BUCKET, os.environ.get('AWS_REGION'), os.environ.get('MEDIACONVERT_QUEUE_ARN')]):
print("Warning: Please set MEDIACONVERT_ROLE_ARN, S3_INPUT_VIDEO_URL, S3_OUTPUT_BUCKET, AWS_REGION, and MEDIACONVERT_QUEUE_ARN environment variables for a real test.")
print("Using dummy values for demonstration.")
# Proceed with dummy values for type checking demonstration even if not runnable
_ = create_sample_job(ROLE_ARN, S3_INPUT_URL, S3_OUTPUT_BUCKET)
else:
create_sample_job(ROLE_ARN, S3_INPUT_URL, S3_OUTPUT_BUCKET)