MediaConvert examples using SDK for Kotlin - Amazon SDK for Kotlin
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

MediaConvert examples using SDK for Kotlin

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Kotlin with MediaConvert.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateJob.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun createMediaJob( mcClient: MediaConvertClient, mcRoleARN: String, fileInput1: String, ): String? { // Step 1: Describe endpoints to get the MediaConvert endpoint URL val describeResponse = mcClient.describeEndpoints( DescribeEndpointsRequest { maxResults = 1 }, ) val endpointUrl = describeResponse.endpoints?.firstOrNull()?.url ?: error("No MediaConvert endpoint found") // Step 2: Create MediaConvert client with resolved endpoint val mediaConvert = MediaConvertClient.fromEnvironment { region = "us-west-2" endpointProvider = MediaConvertEndpointProvider { Endpoint(endpointUrl) } } // Output destination folder in S3 - put in 'output/' folder beside input val outputDestination = fileInput1.substringBeforeLast('/') + "/output/" // Step 3: Create the job request with minimal valid video codec settings val jobRequest = CreateJobRequest { role = mcRoleARN settings = JobSettings { inputs = listOf( Input { fileInput = fileInput1 }, ) outputGroups = listOf( OutputGroup { outputGroupSettings = OutputGroupSettings { type = OutputGroupType.FileGroupSettings fileGroupSettings = FileGroupSettings { destination = outputDestination } } outputs = listOf( Output { containerSettings = ContainerSettings { container = ContainerType.Mp4 } videoDescription = VideoDescription { width = 1280 height = 720 codecSettings = VideoCodecSettings { codec = VideoCodec.H264 h264Settings = H264Settings { rateControlMode = H264RateControlMode.Qvbr qvbrSettings = H264QvbrSettings { qvbrQualityLevel = 7 } maxBitrate = 5_000_000 codecLevel = H264CodecLevel.Auto codecProfile = H264CodecProfile.Main framerateControl = H264FramerateControl.InitializeFromSource } } } }, ) }, ) } } // Step 4: Call MediaConvert to create the job val response = mediaConvert.createJob(jobRequest) // Return the job ID or null if not found return response.job?.id }
  • For API details, see CreateJob in Amazon SDK for Kotlin API reference.

The following code example shows how to use GetJob.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun getSpecificJob(mcClient: MediaConvertClient, jobId: String) { // 1. Discover the correct endpoint val res = mcClient.describeEndpoints(DescribeEndpointsRequest { maxResults = 1 }) var endpointUrl = res.endpoints?.firstOrNull()?.url ?: error(" No MediaConvert endpoint found") // 2. Create a new client using the endpoint val clientWithEndpoint = MediaConvertClient { region = "us-west-2" endpointUrl = endpointUrl } // 3. Get the job details val jobResponse = clientWithEndpoint.getJob(GetJobRequest { id = jobId }) val job = jobResponse.job println("Job status: ${job?.status}") println("Job ARN: ${job?.arn}") println("Output group count: ${job?.settings?.outputGroups?.size}") }
  • For API details, see GetJob in Amazon SDK for Kotlin API reference.

The following code example shows how to use ListJobs.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

suspend fun listCompleteJobs(mcClient: MediaConvertClient) { val describeEndpoints = DescribeEndpointsRequest { maxResults = 20 } val res = mcClient.describeEndpoints(describeEndpoints) if (res.endpoints?.size!! <= 0) { println("Cannot find MediaConvert service endpoint URL!") exitProcess(0) } val endpointURL = res.endpoints!![0].url!! val mediaConvert = MediaConvertClient.fromEnvironment { region = "us-west-2" endpointProvider = MediaConvertEndpointProvider { Endpoint(endpointURL) } } val jobsRequest = ListJobsRequest { maxResults = 10 status = JobStatus.fromValue("COMPLETE") } val jobsResponse = mediaConvert.listJobs(jobsRequest) val jobs = jobsResponse.jobs if (jobs != null) { for (job in jobs) { println("The JOB ARN is ${job.arn}") } } }
  • For API details, see ListJobs in Amazon SDK for Kotlin API reference.