{"id":2831,"date":"2017-07-04T14:05:36","date_gmt":"2017-07-04T14:05:36","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2831"},"modified":"2017-07-04T14:05:36","modified_gmt":"2017-07-04T14:05:36","slug":"13-youtube-data-api-captions-download-function","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2017\/07\/04\/13-youtube-data-api-captions-download-function\/","title":{"rendered":"13 &#8211; YouTube Data API &#8211; Captions &#8211; download function"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p style=\"text-align: center;\"><strong>This is article 13 of the YouTube API With PHP series.<\/strong><\/p>\n<p class=\"NormalTextDroid\">Download a caption track of a Video. The caption track is returned in its original format unless the request specifies a value for the tfmt parameter and in its original language unless the request specifies a value for the tlang parameter. Since this call requires user authentication, it can only download Caption tracks of videos which belong to you.<\/p>\n<p class=\"NormalTextDroid\">\u00a0The Request URL is<\/p>\n<pre class=\"PHPCode\"><span style=\"color: #808080;\">\u00a0GET https:\/\/www.googleapis.com\/youtube\/v3\/captions\/(id)\r\n\r\n<\/span><\/pre>\n<p><strong>Parameters<br \/>\n<\/strong><\/p>\n<ul>\n<li><strong>key<\/strong> (string) required. Your API key<\/li>\n<li><strong>id<\/strong> (string) required This is added as a suffix to the GET url. This has to be a valid id for an existing Caption track.<\/li>\n<li><strong>onBehalfOfContentOwner<\/strong> (string) optional. This is relevant only for YouTube Channel Partners. For this parameter, the API request URL should have user authentication.We will not be exploring this option.<\/li>\n<li><strong>tfmt<\/strong> (string) optional. The tfmt parameter specifies that the caption track should be returned in a specific format. If the parameter is not included in the request, the track is returned in its original format. Possible values are :\n<ul>\n<li>sbv \u2013 SubViewer subtitle<\/li>\n<li>scc \u2013 Scenarist Closed Caption format<\/li>\n<li>srt \u2013 SubRip subtitle<\/li>\n<li>ttml \u2013 Timed Text Markup Language caption<\/li>\n<li>vtt \u2013 Web Video Text Tracks caption<\/li>\n<\/ul>\n<\/li>\n<li><strong>tlang<\/strong> (string) optional. The tlang parameter specifies that the API response should return a translation of the specified caption track. The parameter value is the international two letter language code that identifies the desired caption language. The translation is generated by using machine translation, such as Google Translate.<\/li>\n<\/ul>\n<p><strong>\u00a0Response<\/strong><\/p>\n<p><strong>\u00a0<\/strong>On a successful call, a binary file is returned which should be saved onto local disk,<\/p>\n<p>Here is sample code which downloads a Caption track:<br \/>\n<pre class=\"brush: php\">&lt;?php\r\n    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);\r\n    set_time_limit(60 * 3);\r\n    session_start();\r\n\r\n    $clientId = &quot;**&quot;;\r\n    $clientSecret = &quot;**-&quot;;\r\n    $g_youtubeDataAPIKey = &quot;**&quot;;\r\n\r\n    $captionId =  &quot;STDMK4mG9ONQRc2kPO88VeQN1mlD15SHfV5I8hY1acQ=&quot;;\r\n    \r\n    $_SESSION[&quot;code_id&quot;] = $_SERVER[&quot;PHP_SELF&quot;];\r\n\r\n    if ($_SESSION[&quot;access_token&quot;] == null || $_SESSION[&quot;access_token&quot;] == &quot;&quot;) {\r\n   \t \/\/ check for oauth response\r\n   \t header(&quot;Location: ..\/..\/init-login.php&quot;);\r\n   \t exit;\r\n    }\r\n\r\n   \t $accessToken = $_SESSION[&quot;access_token&quot;];\r\n   \t \r\n   \t \/\/ make api request\r\n   \t $url = &quot;https:\/\/www.googleapis.com\/youtube\/v3\/captions\/&quot; . $captionId . &quot;?key=&quot; .\r\n   \t\t\t $g_youtubeDataAPIKey;\r\n   \t $curl = curl_init();\r\n   \t curl_setopt_array($curl, array(\r\n   \t\t\t\t CURLOPT_HTTPHEADER=&gt;array(&#039;Authorization: OAuth &#039; . $accessToken),\r\n   \t\t\t\t CURLOPT_RETURNTRANSFER =&gt; 1,\r\n   \t\t\t\t CURLOPT_URL =&gt; $url,\r\n   \t\t\t\t CURLOPT_USERAGENT =&gt; &#039;YouTube API Tester&#039;,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYPEER =&gt; 1,\r\n   \t\t\t\t CURLOPT_SSL_VERIFYHOST=&gt; 0,\r\n   \t\t\t\t CURLOPT_CAINFO =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_CAPATH =&gt; &quot;..\/..\/cert\/cacert.pem&quot;,\r\n   \t\t\t\t CURLOPT_FOLLOWLOCATION =&gt; TRUE\r\n   \t\t\t\t ));\r\n   \t $resp = curl_exec($curl);\r\n\r\n   \t curl_close($curl);\r\n   \t var_dump($resp);\r\n    \r\n    \r\n?&gt;\r\n<\/pre><\/p>\n<p class=\"NormalTextDroid\">Here is the output:<\/p>\n<pre><span style=\"color: #999999;\">string(3265) \"0:00:00.000,0:00:08.220 a few months back sometime in 2016 \r\nI've 0:00:06.120,0:00:10.800 made a video which showcased the 0:00:08.220,0:00:13.080 features\r\n of the web speech API the West 0:00:10.800,0:00:15.020 peach API is a technology made by google \r\n0:00:13.080,0:00:18.600 which lets you do speech recognition 0:00:15.020,0:00:21.359 within your\r\n browser unfortunately even 0:00:18.600,0:00:23.490 as of now the only browser which fully\r\n 0:00:21.359,0:00:26.160 supports that specification is google 0:00:23.490,0:00:28.769 chrome \r\nthe other browsers haven't really 0:00:26.160,0:00:30.449 got support for it so i suggest you \r\nhave 0:00:28.769,0:00:32.489 a look at that video first in order to 0:00:30.449,0:00:34.590 \r\nget an idea of the capabilities of web 0:00:32.489,0:00:37.350 speech API the link is right \r\nbelow this 0:00:34.590,0:00:39.030 video so this time round i decided to 0:00:37.350,0:00:42.120 \r\ntake that experiment a little further 0:00:39.030,0:00:44.940 and what i have here \r\nis a single web 0:00:42.120,0:00:48.000 page application which does real-time 0:00:44.940,0:00:50.280 \r\ntranslation so the processing pipeline 0:00:48.000,0:00:53.399 is very simple the first thing\r\n it does 0:00:50.280,0:00:56.160 is that it accepts speech using a 0:00:53.399,0:00:59.489 \r\nmicrophone and then translates the 0:00:56.160,0:01:01.699 speech into written text that text\r\n is 0:00:59.489,0:01:03.899 them fed into a translation api which 0:01:01.699,0:01:07.140 \r\ntranslates that text into another 0:01:03.899,0:01:09.479 language and then that translated\r\n takes 0:01:07.140,0:01:11.670 to spread into a text-to-speech engine 0:01:09.479,0:01:14.159 which \r\nthen plays back that text as 0:01:11.670,0:01:15.390 reporters audio so you can speak in one \r\n0:01:14.159,0:01:17.009 language and you can hear the 0:01:15.390,0:01:19.860 translation \r\nof the same thing in a 0:01:17.009,0:01:22.799 different language now since what\r\n i'm 0:01:19.860,0:01:25.439 using your motif c-suite engines and AP 0:01:22.799,0:01:27.000 is the\r\n machine translation is not really 0:01:25.439,0:01:29.930 hundred percent perfect and sometimes\r\n 0:01:27.000,0:01:33.270 the results can be quite funny but this 0:01:29.930,0:01:35.549 still forms \r\nthe base for any kind of 0:01:33.270,0:01:38.820 translation application of \r\nsoftware 0:01:35.549,0:01:40.020 which one might make and this can use as 0:01:38.820,0:01:41.880 a\r\n base to make something more 0:01:40.020,0:01:44.990 sophisticated so i'm just going \r\nto show 0:01:41.880,0:01:44.990 you a few examples here 0:01:47.100,0:01:54.240 what is your \r\nname and where you live you 0:01:51.930,0:01:57.409 will have a callous me why I shouldn't 0:01:54\r\n.240,0:02:02.909 even care where I usually say material 0:01:57.409,0:02:07.289 assiyah\r\n me here for this table do you 0:02:02.909,0:02:13.920 believe them do you have any money \r\nwith 0:02:07.289,0:02:19.470 you muscle to kill every one \r\ndegree to 0:02:13.920,0:02:21.750 be happy imma get\r\n between the phenomena 0:02:19.470,0:02:28.850 Tomas famous me your hundred your\r\n 0:02:21.750,0:02:28.850 amateur thank you for the kind words 0:02:29.780,0:02:39.080 necessary \r\nmaster Anahata name is a photo 0:02:33.330,0:02:39.080 that dress PS different people around \"\r\n\r\n\r\n<\/span><\/pre>\n<p class=\"NormalTextDroid\">We pass the Caption track id as part of the API call. We also pass the OAuth token as part of the headers instead of the URL.<\/p>\n<p class=\"NormalTextDroid\">\u00a0What the call returns are the contents of the Captions file. In this example we are dumping the contents . Ideally you would save it to a file. Since we have not specified what format we want the contents to be in via the tfmt parameter, the contents are returned in the original format as it was uploaded in.<\/p>\n<p class=\"NormalTextDroid\">\u00a0We can set the tfmt parameter as part of the API URL as tfmt=xxx where xxx is a valid string as mentioned in the tfmt specs above.<\/p>\n<p class=\"NormalTextDroid\">\u00a0Here is how the previous output looks if we put tfmt=vtt<\/p>\n<pre class=\"NormalTextDroid\"><span style=\"color: #999999;\">string(12175) \"WEBVTT Kind: captions Language: en Style: ::cue(c.colorCCCCCC) { color: rgb(204,204,204); }\r\n ::cue(c.colorE5E5E5) { color: rgb(229,229,229); } ## 00:00:00.000 --&gt; 00:00:08.220 \r\nalign:start position:19% a&lt;00:00:01.280&gt; few&lt;00:00:02.280&gt; months&lt;00:00:02.429&gt; back&lt;00:00:02.790&gt; \r\nsometime&lt;00:00:03.780&gt; in&lt;00:00:04.160&gt; 2016&lt;00:00:05.160&gt; I've 00:00:06.120 --&gt; 00:00:10.800 \r\nalign:start position:19% made&lt;00:00:06.509&gt; a&lt;00:00:06.540&gt; video&lt;00:00:06.839&gt; which&lt;00:00:07.200&gt; \r\nshowcased&lt;00:00:08.040&gt; the 00:00:08.220 --&gt; 00:00:13.080 align:start position:19% features&lt;00:00:08.580&gt;\r\n of&lt;00:00:08.610&gt; the&lt;00:00:08.910&gt; web&lt;00:00:09.090&gt; speech&lt;00:00:09.389&gt; API&lt;00:00:09.590&gt; \r\nthe&lt;00:00:10.590&gt; West 00:00:10.800 --&gt; 00:00:15.020 align:start position:19% peach&lt;00:00:11.070&gt; \r\nAPI&lt;00:00:11.460&gt; is&lt;00:00:11.580&gt; a&lt;00:00:11.820&gt; technology&lt;00:00:12.210&gt; made&lt;00:00:12.690&gt; \r\nby&lt;00:00:12.870&gt; google 00:00:13.080 --&gt; 00:00:18.600 align:start position:19% which&lt;00:00:13.650&gt;\r\n lets&lt;00:00:13.950&gt; you&lt;00:00:14.099&gt; do&lt;00:00:14.280&gt; speech&lt;00:00:14.849&gt; recognition\r\n 00:00:15.020 --&gt; 00:00:21.359 align:start position:19% within&lt;00:00:16.020&gt; \r\nyour&lt;00:00:16.260&gt; browser&lt;00:00:17.420&gt; unfortunately\r\n\r\n\r\n<\/span><\/pre>\n<p class=\"NormalTextDroid\">Note that it is not necessary that specifying a tfmt value will always return content in that format. If the format is invalid or it cannot be converted then no content is returned for that format.<\/p>\n<p class=\"NormalTextDroid\">\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is article 13 of the YouTube API With PHP series. Download a caption track of a Video. The caption track is returned in its <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2017\/07\/04\/13-youtube-data-api-captions-download-function\/\" title=\"13 &#8211; YouTube Data API &#8211; Captions &#8211; download function\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2727,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[318],"tags":[],"class_list":["post-2831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-youtube-api-with-php"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2831"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2831\/revisions"}],"predecessor-version":[{"id":2833,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2831\/revisions\/2833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2727"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}