AWS REST API mediaconvert: example convert mp4 to HLS


Step 1: Create IAM user

Create IAM user for AWS

We get access Key Id and secret Access Key

Step 2: create role

AWS IAM: create role for mediaconvert

We get ARN role

Step 3: Get Endpoint

Step 4: Calculating a Signature

https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html

We get headers Authorization and X-Amz-Date

Example for Nodejs:

Nodejs: create signature request header for AWS API

Step 5:

Example JSON convert MP4 to M3U8 with single file TS:

{
	  "UserMetadata": {},
	  "Role": "arn:aws:iam::395784617274:role/mediaconvert-role-1",
	  "Settings": {
		"OutputGroups": [
		  {
			"Name": "Apple HLS",
			"Outputs": [           
			  {
				"VideoDescription": {
				  "ScalingBehavior": "DEFAULT",
				  "TimecodeInsertion": "DISABLED",
				  "AntiAlias": "ENABLED",
				  "Sharpness": 50,
				  "CodecSettings": {
					"Codec": "H_264",
					"H264Settings": {
					  "InterlaceMode": "PROGRESSIVE",
					  "NumberReferenceFrames": 3,
					  "Syntax": "DEFAULT",
					  "Softness": 0,
					  "GopClosedCadence": 1,
					  "GopSize": 90,
					  "Slices": 1,
					  "GopBReference": "DISABLED",
					  "SlowPal": "DISABLED",
					  "SpatialAdaptiveQuantization": "ENABLED",
					  "TemporalAdaptiveQuantization": "ENABLED",
					  "FlickerAdaptiveQuantization": "DISABLED",
					  "EntropyEncoding": "CABAC",
					  "Bitrate": 1093000,
					  "FramerateControl": "INITIALIZE_FROM_SOURCE",
					  "RateControlMode": "CBR",
					  "CodecProfile": "MAIN",
					  "Telecine": "NONE",
					  "MinIInterval": 0,
					  "AdaptiveQuantization": "HIGH",
					  "CodecLevel": "AUTO",
					  "FieldEncoding": "PAFF",
					  "SceneChangeDetect": "ENABLED",
					  "QualityTuningLevel": "SINGLE_PASS",
					  "FramerateConversionAlgorithm": "DUPLICATE_DROP",
					  "UnregisteredSeiTimecode": "DISABLED",
					  "GopSizeUnits": "FRAMES",
					  "ParControl": "INITIALIZE_FROM_SOURCE",
					  "NumberBFramesBetweenReferenceFrames": 2,
					  "RepeatPps": "DISABLED"
					}
				  },
				  "AfdSignaling": "NONE",
				  "DropFrameTimecode": "ENABLED",
				  "RespondToAfd": "NONE",
				  "ColorMetadata": "INSERT",
				  "Width": 1080,
				  "Height": 720
				},
				"AudioDescriptions": [
				  {
					"AudioTypeControl": "FOLLOW_INPUT",
					"CodecSettings": {
					  "Codec": "AAC",
					  "AacSettings": {
						"AudioDescriptionBroadcasterMix": "NORMAL",
						"Bitrate": 96000,
						"RateControlMode": "CBR",
						"CodecProfile": "LC",
						"CodingMode": "CODING_MODE_2_0",
						"RawFormat": "NONE",
						"SampleRate": 48000,
						"Specification": "MPEG4"
					  }
					},
					"LanguageCodeControl": "FOLLOW_INPUT",
					"AudioSourceName": "Audio Selector 1"
				  }
				],
				"OutputSettings": {
				  "HlsSettings": {
					"AudioGroupId": "program_audio",
					"AudioRenditionSets": "program_audio",
					"IFrameOnlyManifest": "EXCLUDE"
				  }
				},
				"ContainerSettings": {
				  "Container": "M3U8",
				  "M3u8Settings": {
					"AudioFramesPerPes": 4,
					"PcrControl": "PCR_EVERY_PES_PACKET"					
				  }
				},
				"NameModifier": "_hd"
			  },
			  {
				"VideoDescription": {
				  "ScalingBehavior": "DEFAULT",
				  "TimecodeInsertion": "DISABLED",
				  "AntiAlias": "ENABLED",
				  "Sharpness": 100,
				  "CodecSettings": {
					"Codec": "H_264",
					"H264Settings": {
					  "InterlaceMode": "PROGRESSIVE",
					  "NumberReferenceFrames": 3,
					  "Syntax": "DEFAULT",
					  "Softness": 0,
					  "GopClosedCadence": 1,
					  "GopSize": 90,
					  "Slices": 1,
					  "GopBReference": "DISABLED",
					  "SlowPal": "DISABLED",
					  "SpatialAdaptiveQuantization": "ENABLED",
					  "TemporalAdaptiveQuantization": "ENABLED",
					  "FlickerAdaptiveQuantization": "DISABLED",
					  "EntropyEncoding": "CABAC",
					  "Bitrate": 418000,
					  "FramerateControl": "INITIALIZE_FROM_SOURCE",
					  "RateControlMode": "CBR",
					  "CodecProfile": "MAIN",
					  "Telecine": "NONE",
					  "MinIInterval": 0,
					  "AdaptiveQuantization": "HIGH",
					  "CodecLevel": "LEVEL_3_1",
					  "FieldEncoding": "PAFF",
					  "SceneChangeDetect": "ENABLED",
					  "QualityTuningLevel": "SINGLE_PASS_HQ",
					  "FramerateConversionAlgorithm": "DUPLICATE_DROP",
					  "UnregisteredSeiTimecode": "DISABLED",
					  "GopSizeUnits": "FRAMES",
					  "ParControl": "INITIALIZE_FROM_SOURCE",
					  "NumberBFramesBetweenReferenceFrames": 2,
					  "RepeatPps": "DISABLED"
					}
				  },
				  "AfdSignaling": "NONE",
				  "DropFrameTimecode": "ENABLED",
				  "RespondToAfd": "NONE",
				  "ColorMetadata": "INSERT",
				  "Width": 720,
				  "Height": 480
				},
				"AudioDescriptions": [
				  {
					"AudioTypeControl": "FOLLOW_INPUT",
					"CodecSettings": {
					  "Codec": "AAC",
					  "AacSettings": {
						"AudioDescriptionBroadcasterMix": "NORMAL",
						"Bitrate": 96000,
						"RateControlMode": "CBR",
						"CodecProfile": "LC",
						"CodingMode": "CODING_MODE_2_0",
						"RawFormat": "NONE",
						"SampleRate": 48000,
						"Specification": "MPEG4"
					  }
					},
					"LanguageCodeControl": "FOLLOW_INPUT",
					"AudioSourceName": "Audio Selector 1"
				  }
				],
				"OutputSettings": {
				  "HlsSettings": {
					"AudioGroupId": "program_audio",
					"AudioRenditionSets": "program_audio",
					"IFrameOnlyManifest": "EXCLUDE"
				  }
				},
				"ContainerSettings": {
				  "Container": "M3U8",
				  "M3u8Settings": {
					"AudioFramesPerPes": 4,
					"PcrControl": "PCR_EVERY_PES_PACKET"					
				  }
				},
				"NameModifier": "_sd"
			  }
			],
			"OutputGroupSettings": {
			  "Type": "HLS_GROUP_SETTINGS",
			  "HlsGroupSettings": {
				"ManifestDurationFormat": "INTEGER",
				"SegmentLength": 15,
				"TimedMetadataId3Period": 15,
				"CaptionLanguageSetting": "OMIT",
				"Destination": "s3://my-new-bucket/output_hls/master",
				"TimedMetadataId3Frame": "PRIV",
				"CodecSpecification": "RFC_4281",
				"OutputSelection": "MANIFESTS_AND_SEGMENTS",
				"ProgramDateTimePeriod": 600,
				"MinSegmentLength": 0,
				"DirectoryStructure": "SINGLE_DIRECTORY",
				"ProgramDateTime": "EXCLUDE",
				"SegmentControl": "SINGLE_FILE",
				"ManifestCompression": "NONE",
				"ClientCache": "ENABLED",
				"StreamInfResolution": "INCLUDE"
			  }
			}
		  }
		],
		"AdAvailOffset": 0,
		"Inputs": [
		  {
			"AudioSelectors": {
			   "Audio Selector 1": {
				"DefaultSelection": "DEFAULT",
				"SelectorType": "PID"
			  }
			},
			"VideoSelector": {
			  "ColorSpace": "FOLLOW"
			},
			"FilterEnable": "AUTO",
			"PsiControl": "USE_PSI",
			"FilterStrength": 0,
			"DeblockFilter": "DISABLED",
			"DenoiseFilter": "DISABLED",
			"TimecodeSource": "ZEROBASED",
			"FileInput": "s3://my-new-bucket/input.mp4"
		  }
		]
	  }
	}

Example CURL:

curl -X POST -H "Content-Type: application/json" \
-H "Authorization: AWS4-HMAC-SHA256 Credential=AKIAVYJUZ7U5D4R5QZ74/20220620/us-east-1/mediaconvert/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=89fc69bf089e87497df01f0a184450284804fcf1842d53dd1899619cdfbe839b" \
-H "X-Amz-Date: 20220620T035259Z" \
-d '{"UserMetadata":{},"Role":"arn:aws:iam::395784617274:role/mediaconvert-role-1","Settings":{"OutputGroups":[{"Name":"Apple HLS","Outputs":[{"VideoDescription":{"ScalingBehavior":"DEFAULT","TimecodeInsertion":"DISABLED","AntiAlias":"ENABLED","Sharpness":50,"CodecSettings":{"Codec":"H_264","H264Settings":{"InterlaceMode":"PROGRESSIVE","NumberReferenceFrames":3,"Syntax":"DEFAULT","Softness":0,"GopClosedCadence":1,"GopSize":90,"Slices":1,"GopBReference":"DISABLED","SlowPal":"DISABLED","SpatialAdaptiveQuantization":"ENABLED","TemporalAdaptiveQuantization":"ENABLED","FlickerAdaptiveQuantization":"DISABLED","EntropyEncoding":"CABAC","Bitrate":1093000,"FramerateControl":"INITIALIZE_FROM_SOURCE","RateControlMode":"CBR","CodecProfile":"MAIN","Telecine":"NONE","MinIInterval":0,"AdaptiveQuantization":"HIGH","CodecLevel":"AUTO","FieldEncoding":"PAFF","SceneChangeDetect":"ENABLED","QualityTuningLevel":"SINGLE_PASS","FramerateConversionAlgorithm":"DUPLICATE_DROP","UnregisteredSeiTimecode":"DISABLED","GopSizeUnits":"FRAMES","ParControl":"INITIALIZE_FROM_SOURCE","NumberBFramesBetweenReferenceFrames":2,"RepeatPps":"DISABLED"}},"AfdSignaling":"NONE","DropFrameTimecode":"ENABLED","RespondToAfd":"NONE","ColorMetadata":"INSERT","Width":1080,"Height":720},"AudioDescriptions":[{"AudioTypeControl":"FOLLOW_INPUT","CodecSettings":{"Codec":"AAC","AacSettings":{"AudioDescriptionBroadcasterMix":"NORMAL","Bitrate":96000,"RateControlMode":"CBR","CodecProfile":"LC","CodingMode":"CODING_MODE_2_0","RawFormat":"NONE","SampleRate":48000,"Specification":"MPEG4"}},"LanguageCodeControl":"FOLLOW_INPUT","AudioSourceName":"Audio Selector 1"}],"OutputSettings":{"HlsSettings":{"AudioGroupId":"program_audio","AudioRenditionSets":"program_audio","IFrameOnlyManifest":"EXCLUDE"}},"ContainerSettings":{"Container":"M3U8","M3u8Settings":{"AudioFramesPerPes":4,"PcrControl":"PCR_EVERY_PES_PACKET"}},"NameModifier":"_hd"},{"VideoDescription":{"ScalingBehavior":"DEFAULT","TimecodeInsertion":"DISABLED","AntiAlias":"ENABLED","Sharpness":100,"CodecSettings":{"Codec":"H_264","H264Settings":{"InterlaceMode":"PROGRESSIVE","NumberReferenceFrames":3,"Syntax":"DEFAULT","Softness":0,"GopClosedCadence":1,"GopSize":90,"Slices":1,"GopBReference":"DISABLED","SlowPal":"DISABLED","SpatialAdaptiveQuantization":"ENABLED","TemporalAdaptiveQuantization":"ENABLED","FlickerAdaptiveQuantization":"DISABLED","EntropyEncoding":"CABAC","Bitrate":418000,"FramerateControl":"INITIALIZE_FROM_SOURCE","RateControlMode":"CBR","CodecProfile":"MAIN","Telecine":"NONE","MinIInterval":0,"AdaptiveQuantization":"HIGH","CodecLevel":"LEVEL_3_1","FieldEncoding":"PAFF","SceneChangeDetect":"ENABLED","QualityTuningLevel":"SINGLE_PASS_HQ","FramerateConversionAlgorithm":"DUPLICATE_DROP","UnregisteredSeiTimecode":"DISABLED","GopSizeUnits":"FRAMES","ParControl":"INITIALIZE_FROM_SOURCE","NumberBFramesBetweenReferenceFrames":2,"RepeatPps":"DISABLED"}},"AfdSignaling":"NONE","DropFrameTimecode":"ENABLED","RespondToAfd":"NONE","ColorMetadata":"INSERT","Width":720,"Height":480},"AudioDescriptions":[{"AudioTypeControl":"FOLLOW_INPUT","CodecSettings":{"Codec":"AAC","AacSettings":{"AudioDescriptionBroadcasterMix":"NORMAL","Bitrate":96000,"RateControlMode":"CBR","CodecProfile":"LC","CodingMode":"CODING_MODE_2_0","RawFormat":"NONE","SampleRate":48000,"Specification":"MPEG4"}},"LanguageCodeControl":"FOLLOW_INPUT","AudioSourceName":"Audio Selector 1"}],"OutputSettings":{"HlsSettings":{"AudioGroupId":"program_audio","AudioRenditionSets":"program_audio","IFrameOnlyManifest":"EXCLUDE"}},"ContainerSettings":{"Container":"M3U8","M3u8Settings":{"AudioFramesPerPes":4,"PcrControl":"PCR_EVERY_PES_PACKET"}},"NameModifier":"_sd"}],"OutputGroupSettings":{"Type":"HLS_GROUP_SETTINGS","HlsGroupSettings":{"ManifestDurationFormat":"INTEGER","SegmentLength":15,"TimedMetadataId3Period":15,"CaptionLanguageSetting":"OMIT","Destination":"s3://my-new-bucket/output_hls/master","TimedMetadataId3Frame":"PRIV","CodecSpecification":"RFC_4281","OutputSelection":"MANIFESTS_AND_SEGMENTS","ProgramDateTimePeriod":600,"MinSegmentLength":0,"DirectoryStructure":"SINGLE_DIRECTORY","ProgramDateTime":"EXCLUDE","SegmentControl":"SINGLE_FILE","ManifestCompression":"NONE","ClientCache":"ENABLED","StreamInfResolution":"INCLUDE"}}}],"AdAvailOffset":0,"Inputs":[{"AudioSelectors":{"Audio Selector 1":{"DefaultSelection":"DEFAULT","SelectorType":"PID"}},"VideoSelector":{"ColorSpace":"FOLLOW"},"FilterEnable":"AUTO","PsiControl":"USE_PSI","FilterStrength":0,"DeblockFilter":"DISABLED","DenoiseFilter":"DISABLED","TimecodeSource":"ZEROBASED","FileInput":"s3://my-new-bucket/input.mp4"}]}}' \
https://q25wbt2lc.mediaconvert.us-east-1.amazonaws.com/2017-08-29/jobs

Leave a Reply