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.
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):- How to Download Any Videos in Chrome Browser and Merge Video Segments?
- How to Solve 403 Forbidden Error (Denied by Referer ACL) when Downloading Weibo Video?
- VideoDownloader Update: New UI, d.tube and steemit video URL parser, code refactoring!
- VideoDownloader Update: VIP Feature of Server Video Parser
- How to Download Tumblr Posts?
- A Home-made Video Download Helper (Client + Server)
- The Simple Video .m3u8 Downloader/Parser in PHP and Javascript
- How to Download Instagram Videos using PHP and Javascript?
- How to Download Video via Workflow APP?
- The TED Video Downloader
- Adding `Image Download List` to the Popular `VideoDownloadHelper` Chrome Extension
You may also like: 如何下载 Instagram 视频?
–EOF (The Ultimate Computing & Technology Blog) —
loading...
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
You can download your favorite video in your device.You have to copy and paste the link of the Video you need to download, and you can save that Video on your PC, mobile, laptop, or any device you own.
nice method. just checked and it’s still relevant after 2 years.. links are still there.. i guess that’s why there are so many online instagram downloaders.. =)
Yes, thank you.
Thanks for the review, I like to download video from instagram, without apps.
Then try this: https://weibomiaopai.com/download-video-parser.php