How to generate download link from any Youtube videos in PHP?

967

I'm working on a web application that allow users to post any Youtube videos to their profile or pages they manages. This is the code that I use to post Youtube video to my profile

 $videoUpload = $fb->post('/me/videos', ['description' => 'example description', 'file_url' => 'https://r2---sn-npoeene7.googlevideo.com/videoplayback?pl=24&lmt=1513082002389285&id=o-AIPnl4v8svvYBTFPYwJOsRanE_5O8YSK0F3bMkTqN6QR&itag=22&signature=8051FCC4C243296224CD1D7ADB4E5C0A90125C57.66FF9FF2063C3A8F00F143FB51BB3E98F7AEBB7B&requiressl=yes&ip=203.189.156.104&sparams=dur,ei,expire,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source&key=cms1&mime=video%2Fmp4&dur=301.952&fvip=2&ipbits=0&c=WEB&expire=1519648901&ratebypass=yes&ei=JayTWtSaDcWqoQORionoCg&source=youtube&redirect_counter=1&cm2rm=sn-jvbxuxavoapox-2oil7e&fexp=23712580&req_id=21384f45d53ba3ee&cms_redirect=yes&mm=29&mn=sn-npoeene7&ms=rdu&mt=1519627218&mv=m'], $access_token);
 $videoUpload = $videoUpload->getGraphNode()->asArray();

The problem is that Facebook not allow to post Youtube link directly, so I need to generate a downloadable link from that video in order to post to Facebook. Here is the PHP code that use to generate a download link from a Youtube video

   <?php

$id = 'RllJtOw0USI'; 

if (isset($_GET["id"]))
    $id = $_GET["id"];

parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id), $video_data);

$streams = $video_data['url_encoded_fmt_stream_map'];
$streams = explode(',',$streams);
$counter = 1;

foreach ($streams as $streamdata) {
    printf("Stream %d:<br/>

It's only work with RllJtOw0USI if I change the $id to dMK_npDG12Q it's not working anymore, and I got the errors as follow:

{-code-3}

How can I get the link from this kind of video?

685

Answer

------<br/><br/>", $counter); parse_str($streamdata,$streamdata); foreach ($streamdata as $key => $value) { if ($key == "url") { $value = urldecode($value); printf("<strong>%s:</strong> <a href='%s'>video link</a><br/>", $key, $value); } else { printf("<strong>%s:</strong> %s<br/>", $key, $value); } } $counter = $counter+1; printf("<br/><br/>"); } ?>|||status=fail&errorcode=150&errordetail=0&reason=This+video+contains+content+from+Vevo.+It+is+restricted+from+playback+on+certain+sites+or+applications.%0A%3Ca+href%3D%27http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdMK_npDG12Q%26feature%3Dplayer_embedded%27+target%3D%27_blank%27%3EWatch+on+YouTube%3C%2Fa%3E
383

Answer

Solution:

If you look at reason in json, you can see that

This video contains content from Vevo. It is restricted from playback on certain sites or applications

So you should obey their restrictions, or find any other link which is not restricted yet.

People are also looking for solutions to the problem: php - Changing the location of the User Table in Laravel 5.6

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.