适用于 PHP 的 AWS 开发工具包
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

使用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

GitHub 上的此处提供了AWS SDK for PHP版本 3 的所有示例代码。

凭证

在运行示例代码之前,您需要配置 AWS 凭证,如适用于 PHP 的 AWS 开发工具包版本 3 的凭证中所述。然后导入AWS SDK for PHP,如适用于 PHP 的 AWS 开发工具包版本 3 的基本使用模式中所述。

要访问 MediaConvert 客户端,请创建一个 IAM 角色,该角色使 AWS Elemental MediaConvert 能够访问输入文件以及存储输出文件的 Amazon S3 存储桶。有关详细信息,请参阅 AWS Elemental MediaConvert 用户指南中的设置 IAM 权限

创建客户端

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

导入

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

示例代码

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

定义简单的转码任务

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

这些参数有详细说明。您可以使用 AWS Elemental MediaConvert 控制台生成 JSON 任务参数,方法是在控制台中选择您的任务设置,然后选择 Job (任务) 部分底部的 Show job 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"; }