使用 AWS SDK for PHP 版本 3 来在 AWS Elemental MediaConvert 中创建和管理转码作业 - AWS SDK for PHP

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 AWS SDK for PHP 版本 3 来在 AWS Elemental MediaConvert 中创建和管理转码作业

在此示例中,您使用AWS SDK for PHP版本 3 调用 AWS Elemental MediaConvert 并创建转码任务。在开始之前,您需要将输入视频上传到为输入存储预配置的 Amazon S3 存储桶。有关支持的输入视频编解码器和容器的列表,请参阅 AWS Elemental MediaConvert 用户指南中的支持的输入编解码器和容器

以下示例演示如何:

  • 在 AWS Elemental MediaConvert 中创建转码任务。CreateJob

  • 取消AWS Elemental MediaConvert队列中的转码作业。 CancelJob

  • 检索已完成的转码任务的 JSON。 GetJob

  • 检索最多 20 个最近创建的任务的 JSON 数组。 ListJobs

的所有示例代码都可以在此AWS SDK for PHP处找到 GitHub

凭证

运行示例代码之前,请配置您的 AWS 凭证,如 凭证 中所述。然后导入 AWS SDK for PHP,如 基本用法 中所述。

要访问 MediaConvert 客户端,请创建一个 IAM 角色来AWS Elemental MediaConvert访问您的输入文件和存储输出文件的 Amazon S3 存储桶。有关更多信息,请参阅 AWS Elemental MediaConvert 用户指南中的设置 IAM 权限

创建客户端

AWS SDK for PHP通过创建 MediaConvert 客户端进行配置,其中包含您的代码所在的区域。在本示例中,区域设置为 us-west-2。由于 AWS Elemental MediaConvert 对每个账户使用自定义端点,因此,请配置 AWS.MediaConvert client class 以使用特定于账户的端点。要执行此操作,请将端点参数设置为特定于您的账户的端点

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\MediaConvert\MediaConvertClient;

示例代码

$mediaConvertClient = new MediaConvertClient([ 'version' => '2017-08-29', 'region' => 'us-east-2', 'profile' => 'default', 'endpoint' => 'ACCOUNT_ENDPOINT' ]);

定义简单的转码任务

创建定义转码任务参数的 JSON。

这些参数有详细说明。您可以使用 AWS Elemental MediaConvert 控制台生成 JSON 任务参数,方法是在控制台中选择您的任务设置,然后选择任务部分底部的显示任务 JSON。本示例说明了简单作业的 JSON。

示例代码

$jobSetting = [ "OutputGroups" => [ [ "Name" => "File Group", "OutputGroupSettings" => [ "Type" => "FILE_GROUP_SETTINGS", "FileGroupSettings" => [ "Destination" => "s3://OUTPUT_BUCKET_NAME/" ] ], "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" => 5000000, "FramerateControl" => "SPECIFIED", "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" => "SPECIFIED", "NumberBFramesBetweenReferenceFrames" => 2, "RepeatPps" => "DISABLED", "FramerateNumerator" => 30, "FramerateDenominator" => 1, "ParNumerator" => 1, "ParDenominator" => 1 ] ], "AfdSignaling" => "NONE", "DropFrameTimecode" => "ENABLED", "RespondToAfd" => "NONE", "ColorMetadata" => "INSERT" ], "AudioDescriptions" => [ [ "AudioTypeControl" => "FOLLOW_INPUT", "CodecSettings" => [ "Codec" => "AAC", "AacSettings" => [ "AudioDescriptionBroadcasterMix" => "NORMAL", "RateControlMode" => "CBR", "CodecProfile" => "LC", "CodingMode" => "CODING_MODE_2_0", "RawFormat" => "NONE", "SampleRate" => 48000, "Specification" => "MPEG4", "Bitrate" => 64000 ] ], "LanguageCodeControl" => "FOLLOW_INPUT", "AudioSourceName" => "Audio Selector 1" ] ], "ContainerSettings" => [ "Container" => "MP4", "Mp4Settings" => [ "CslgAtom" => "INCLUDE", "FreeSpaceBox" => "EXCLUDE", "MoovPlacement" => "PROGRESSIVE_DOWNLOAD" ] ], "NameModifier" => "_1" ] ] ] ], "AdAvailOffset" => 0, "Inputs" => [ [ "AudioSelectors" => [ "Audio Selector 1" => [ "Offset" => 0, "DefaultSelection" => "NOT_DEFAULT", "ProgramSelection" => 1, "SelectorType" => "TRACK", "Tracks" => [ 1 ] ] ], "VideoSelector" => [ "ColorSpace" => "FOLLOW" ], "FilterEnable" => "AUTO", "PsiControl" => "USE_PSI", "FilterStrength" => 0, "DeblockFilter" => "DISABLED", "DenoiseFilter" => "DISABLED", "TimecodeSource" => "EMBEDDED", "FileInput" => "s3://INPUT_BUCKET_AND_FILE_NAME" ] ], "TimecodeConfig" => [ "Source" => "EMBEDDED" ] ];

创建作业

在创建任务参数 JSON 后,通过调用 AWS.MediaConvert service object 并传递参数,来调用 createJob 方法。所创建任务的 ID 在响应数据中返回。

示例代码

try { $result = $mediaConvertClient->createJob([ "Role" => "IAM_ROLE_ARN", "Settings" => $jobSetting, //JobSettings structure "Queue" => "JOB_QUEUE_ARN", "UserMetadata" => [ "Customer" => "Amazon" ], ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

检索任务

使用在调用 createjob 时返回的 JobID,您可以获取最近任务的 JSON 格式的详细描述。

示例代码

$mediaConvertClient = new MediaConvertClient([ 'version' => '2017-08-29', 'region' => 'us-east-2', 'profile' => 'default', 'endpoint' => 'ACCOUNT_ENDPOINT' ]); try { $result = $mediaConvertClient->getJob([ 'Id' => 'JOB_ID', ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

取消任务

使用在调用 createjob 时返回的 JobID,您可以取消仍在队列中的任务。您无法取消已开始转码的任务。

示例代码

$mediaConvertClient = new MediaConvertClient([ 'version' => '2017-08-29', 'region' => 'us-east-2', 'profile' => 'default', 'endpoint' => 'ACCOUNT_ENDPOINT' ]); try { $result = $mediaConvertClient->cancelJob([ 'Id' => 'JOB_ID', // REQUIRED The Job ID of the job to be cancelled. ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出最近的转码任务

创建包括值的参数 JSON,以指定是按升序还是降序对列表排序,要检查的任务队列的 ARN,以及要包含的任务的状态。这将返回最多 20 个任务。要检索后面的 20 个最近任务,请使用随结果返回的 nextToken 字符串。

示例代码

$mediaConvertClient = new MediaConvertClient([ 'version' => '2017-08-29', 'region' => 'us-east-2', 'profile' => 'default', 'endpoint' => 'ACCOUNT_ENDPOINT' ]); try { $result = $mediaConvertClient->listJobs([ 'MaxResults' => 20, 'Order' => 'ASCENDING', 'Queue' => 'QUEUE_ARN', 'Status' => 'SUBMITTED', // 'NextToken' => '<string>', //OPTIONAL To retrieve the twenty next most recent jobs ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }