{"id":889,"date":"2012-09-12T14:58:14","date_gmt":"2012-09-12T12:58:14","guid":{"rendered":"http:\/\/www.alkannoide.com\/?p=889"},"modified":"2012-12-13T11:49:55","modified_gmt":"2012-12-13T10:49:55","slug":"microsoft-azure-media-services-how-to-encode-in-the-azure-cloud-part-2","status":"publish","type":"post","link":"https:\/\/www.alkannoide.com\/2012\/09\/12\/microsoft-azure-media-services-how-to-encode-in-the-azure-cloud-part-2\/","title":{"rendered":"Microsoft Azure Media Services \u2013 How to encode in the Azure Cloud \u2013 Part 2"},"content":{"rendered":"

After the first post which give an overview and explain how set-up a WAMS environment, I continue the discovery of WAMS World<\/a> (sorry for the pun ;-)). In this post, we will see how we can transform a video from a input format to another format. It’s possible to generate multiple output format or to encrypt the video. But we will see it in a next posts.<\/p>\n

<\/p>\n

First of all, we need to understand the different terms we will use in WAMS. In my example, I want to obtain this workflow :<\/p>\n

    \n
  1. create an asset<\/a>\u00a0: an asset is an entity which contains all informations on the video (metadata, …)<\/li>\n
  2. upload a file in blob storage and associate to the asset<\/li>\n
  3. apply a job<\/a> on the asset. A job is composed by one or more tasks<\/a>. A task is an action : eg: \u00a0encoding with protection. To run the task, we use a MediaProcessor<\/a>.<\/li>\n
  4. after the job completed, deliver the video via the Azure CDN.<\/li>\n<\/ol>\n

    Create an asset<\/h2>\n

    As I said previously, the AMS is based on REST, so you use HTTP verbs to make an action. Here is the PHP class, I used to make API call (I completed the code given in previous post<\/a>):<\/p>\n

    class Rest{\r\n \tprotected $strUrl;\r\n \tprotected $strToken;\r\n \tprotected $strBody;\r\n \tpublic function __construct(){\r\n \t \t$this->requestToken();\r\n\t}\r\n\r\n\tprivate function generateData($arrData){\r\n\t\treturn implode('&', $arrData);\r\n\t}\r\n\r\n\tpublic function requestToken(){\r\n\t\tif (file_exists(TOKEN_STORAGE)){\r\n\t\t\techo 'Get Token from storage'."\\n";\r\n\t\t\t$data = file_get_contents(TOKEN_STORAGE);\r\n\t\t\t$arrToken = json_decode($data);\r\n\t\t\tif ((filemtime(TOKEN_STORAGE) + $arrToken->expires_in) > time()) {\r\n\t\t\t\t$this->strToken = $arrToken->access_token;\r\n\t\t\t} else {\r\n\t\t\t\techo 'Token expired'."\\n";\r\n\t\t\t\tunlink(TOKEN_STORAGE);\r\n\t\t\t\t$this->requestToken();\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\techo 'Get new token from API'."\\n";\r\n\t\t\t$arrData = array(\r\n\t\t\t\t'grant_type=client_credentials',\r\n\t\t\t\t'client_id='.CLIENT_ID,\r\n\t\t\t\t'client_secret='.urlencode(ACCESS_KEY),\r\n\t\t\t\t'scope=urn%3aWindowsAzureMediaServices'\r\n\t\t\t);\r\n\t\t\t$arrHeader = array(\r\n\t\t\t\t'Content-length:'.strlen($this->generateData($arrData))\r\n\t\t\t);\r\n\r\n\t\t\t$ch = curl_init();\r\n\t\t\tcurl_setopt($ch, CURLOPT_URL, TOKEN_URL);\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateData($arrData));\r\n\t\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);\r\n\t\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\t\t\t$data = curl_exec($ch);\r\n\t\t\tcurl_close($ch);\r\n\r\n\t\t\t$arrToken = json_decode($data);\r\n\t\t\tif (isset($arrToken->error)){\r\n\t\t\t\tprint_r($arrToken);\r\n\t\t\t\tdie();\r\n\t\t\t}\r\n\t\t\t$this->strToken = $arrToken->access_token;\r\n\t\t\tfile_put_contents(TOKEN_STORAGE, $data);\r\n\t\t\techo 'Token save in storage'."\\n";\r\n\t\t}\r\n\t\treturn $this->strToken;\r\n\t}\r\n\r\n\tpublic function request($arrData = array()){\r\n\t\t$ch = curl_init();\r\n\t\t$arrHeader = array(\r\n\t\t\t'x-ms-version:1.0',\r\n\t\t\t'DataServiceVersion:3.0',\r\n\t\t\t'MaxDataServiceVersion:3.0',\r\n\t\t\t'Authorization: Bearer '.$this->strToken,\r\n\t\t\t'Content-Type: application\/json;odata=verbose',\r\n\t\t\t'Accept: application\/json;odata=verbose'\r\n\t\t);\r\n\t\techo 'Call API:'.$this->strUrl."\\n";\r\n\t\tcurl_setopt($ch, CURLOPT_URL, $this->strUrl);\r\n\t\tcurl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);\r\n\t\tcurl_setopt($ch, CURLOPT_HEADER, true);\r\n\t\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\t\tif (!empty($arrData)){\r\n\t\t\tcurl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrData));\r\n\t\t}\r\n\t\t$data = curl_exec($ch);\r\n\t\t$arrInfo = curl_getinfo($ch);\r\n\t\tcurl_close($ch);\r\n\r\n\t\tlist($strHeader, $strBody) = explode("\\r\\n\\r\\n", $data, 2);\r\n\r\n\t\tif ($arrInfo['http_code'] == 301){\r\n\t\t\tif(preg_match('`\\s*Location:\\s(.+)`i', $data, $arrTmp)){\r\n\t\t\t\t$arrTmp[1] = trim($arrTmp[1]);\r\n\t\t\t\tRegister::getInstance()->setIndex('base_url', $arrTmp[1]);\r\n\t\t\t\t$this->strUrl = $arrTmp[1].$this->getVerb();\r\n\t\t\t\techo 'Redirection to '.$this->strUrl."\\n";\r\n\t\t\t\t$this->request($arrData);\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\t$this->strBody = $strBody;\r\n\t\t}\r\n\t}\r\n\r\n\tprivate function getVerb(){\r\n\t\t$arrUrl = explode('\/', $this->strUrl);\r\n\t\treturn array_pop($arrUrl);\r\n\t}\r\n}<\/pre>\n

    You can look, the code manages the token, so you only need to make the API call.<\/p>\n

    Now, we will create an asset. To do that, we need<\/p>\n