How to Download Instagram Videos using PHP and Javascript?


Instagram contains selfie videos besides photos. If you examine the HTML source code of Instagram video page. You will find that video URLs are actually stored in the meta tag with og:video and og:video:secure_url (using HTTPS), the Open Graph meta data format, like this:

1
2
<meta property="og:video:secure_url" content="https://scontent-lht6-1.cdninstagram.com/t50.2886-16/11106824_381325112074272_2126120473_n.mp4" />
<meta property="og:video:type" content="video/mp4" />
<meta property="og:video:secure_url" content="https://scontent-lht6-1.cdninstagram.com/t50.2886-16/11106824_381325112074272_2126120473_n.mp4" />
<meta property="og:video:type" content="video/mp4" />

PHP Function to Download Instagram Videos

The idea is to parse the HTML string and extract these two fields if applicable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require('phpQuery.php');
 
function GetInstagramVideo($url) {    
    $doc = phpQuery::newDocumentFile($url);      
    $tag = trim(strip_tags($doc->find("meta[property='og:video:secure_url']")->attr('content')));
    if (is_good_url($tag)) {
      return $tag;
    }
    $tag = trim(strip_tags($doc->find("meta[property='og:video']")->attr('content')));
    if (is_good_url($tag)) {
      return $tag;
    }    
    return "";
}
require('phpQuery.php');
 
function GetInstagramVideo($url) {    
    $doc = phpQuery::newDocumentFile($url);      
    $tag = trim(strip_tags($doc->find("meta[property='og:video:secure_url']")->attr('content')));
    if (is_good_url($tag)) {
      return $tag;
    }
    $tag = trim(strip_tags($doc->find("meta[property='og:video']")->attr('content')));
    if (is_good_url($tag)) {
      return $tag;
    }    
    return "";
}

The above PHP Function i.e. GetInstagramVideo relies on the phpQuery library to parse the HTML string in the similar jQuery fashion. The function is_good_url checks if the string is a valid URL, which can be easily defined using regular expression or:

1
2
3
4
5
6
7
function is_good_url($url) {
  if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
      return true;
  } else {
     return false;
  }
}
function is_good_url($url) {
  if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
      return true;
  } else {
     return false;
  }
}

Javascript Function to Download Instagram Videos

You can use the following Javascript function to extract the video URL of Instagram.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function GetInstagramVideo() {
  var video_dom = document.querySelector("meta[property='og:video:secure_url']");
  var video_url = "";
  if (video_dom) {
      video_url = video_dom.getAttribute("content");
  }  
  if (!ValidURL(video_url)) {
      video_dom = document.querySelector("meta[property='og:video']");
      if (video_dom) {
          video_url = video_dom.getAttribute("content");
      }
  }
  return video_url;
}
function GetInstagramVideo() {
  var video_dom = document.querySelector("meta[property='og:video:secure_url']");
  var video_url = "";
  if (video_dom) {
      video_url = video_dom.getAttribute("content");
  }  
  if (!ValidURL(video_url)) {
      video_dom = document.querySelector("meta[property='og:video']");
      if (video_dom) {
          video_url = video_dom.getAttribute("content");
      }
  }
  return video_url;
}

The Above JavaScript function GetInstagramVideo relies on the ValidURL function that checks if the URL is valid, and can be defined using regular expression:

1
2
3
function ValidURL(value){
    return /^(https?):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}
function ValidURL(value){
    return /^(https?):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}

Chrome Extension to Download Instagram Videos

Unfortunately, Instagram has changed its API policy, so a crawler bot (such as curl) cannot access the HTML of the video page (it will get a different page i.e. 404) without login. The only way to login via PHP script (programmatic) is to add an application, fill in correct and nice details, submit for approval. After your Instagram application is approved, the APIs can be used outside the SandBox (by default, you can only use your Instagram API within the SandBox, for testing). After that, you will require a third party library to get the access token (client id and secret are not sufficient any more) in order to crawl the real/correct Instagram video page.

download-instagram-videos-via-video-url-parser-chrome-extension How to Download Instagram Videos using PHP and Javascript? chrome extension javascript php video download tutorial

download-instagram-videos-via-video-url-parser-chrome-extension

However, with this powerful Chrome Extension (Video URL Parser), you can do this offline with just a few lines of Javascript (as mentioned above).

Relevant Video Download Posts

Here are some posts that relate to download videos (parser):

You may also like: 如何下载 Instagram 视频?

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
885 words
Last Post: Simple Tutorial with OpenMP: How to Use Parallel Block in C/C++ using OpenMP?
Next Post: A Home-made Video Download Helper (Client + Server) - Simple way to Download Videos

The Permanent URL is: How to Download Instagram Videos using PHP and Javascript?

5 Comments

  1. grambe
  2. John Redken
  3. Mr. Alexsparrow

Leave a Reply