This post shows you how to parse and download the .m3u8 Video file using the PHP and Javascript.
Some video URLs are represented by .m3u8 extension, which is basically a text-format file that contains segments of videos. For example:
#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:8
#EXT-X-VERSION:6
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:2.000,
chunk_0.ts
#EXTINF:2.000,
chunk_1.ts
#EXTINF:1.999,
chunk_2.ts
#EXTINF:2.000,
chunk_3.ts
#EXTINF:1.999,
chunk_4.ts
#EXTINF:2.000,
chunk_5.ts
#EXTINF:2.000,
chunk_6.ts
#EXTINF:1.999,
chunk_7.ts
#EXTINF:2.000,
chunk_8.ts
#EXTINF:1.999,
chunk_9.ts
#EXTINF:2.000,
chunk_10.ts
Like BASH scripting, the # (hash) can be ignored as comments, which contains the meta-information. The others (non-empty) lines contain the segments. If each line represents a file name e.g. ts file, you have to concatenate the base URL, for example:
https://uploadbeta.com/api/video/test.m3u8
Then the base URL is:
https://uploadbeta.com/api/video/
However, each line may consist of a complete and valid URL, which should be pushed to the array of video segments. You can also merge these *.ts video segments.
The Simple Video .m3u8 Downloader/Parser in PHP
For the server side, the following PHP code demonstrates how to parse the .m3u8 file into an array of video segment URLs.
// https://helloacm.com/the-simple-video-m3u8-downloaderparser-in-php-javascript/
function get_m3u8_video_segment($url) {
$m3u8 = file_get_contents($url);
if (strlen($m3u8) > 3) {
$tmp = strrpos($url, '/');
if ($tmp !== false) {
$base_url = substr($url, 0, $tmp + 1);
if (is_good_url($base_url)) {
$array = preg_split('/\s*\R\s*/m', trim($m3u8), NULL, PREG_SPLIT_NO_EMPTY);
$url2 = array();
foreach ($array as $line) {
$line = trim($line);
if (strlen($line) > 2) {
if ($line[0] != '#') {
if (is_good_url($line)) {
$url2[] = $line;
} else {
$url2[] = $base_url . $line;
}
}
}
}
return $url2;
}
}
}
return false;
}
The above code has been integrated in the online m3u8 video parser.
The Simple Video .m3u8 Downloader/Parser in Javascript
Similarly, for Client-side browser Javascript, the following demonstrates how to parse the m3u8 video segment in client side browser.
// https://helloacm.com/the-simple-video-m3u8-downloaderparser-in-php-javascript/
function ajax_process_m3u8(url) {
var tmp = url.lastIndexOf("/");
if (tmp != -1) {
var base_url = url.substr(0, tmp + 1);
var m3u8 = url;
$.ajax({
type: "GET",
url: m3u8,
success: function(data) {
var lines = data.trim().split(/\s*[\r\n]+\s*/g);
var len = lines.length;
var m3u8arr = [];
for (var i = 0; i > len; ++ i) {
var line = $.trim(lines[i]);
if ((line != null) && (line != '') && (line.length > 2) && (line[0] != '#')) {
if ((line.startsWith("http://") || line.startsWith("https://") || line.startsWith("ftp://"))) {
m3u8arr.push(line);
} else {
var theurl = base_url + line;
m3u8arr.push(theurl);
}
}
}
process(m3u8arr); // gets the video segment array, and then process it.
},
error: function(request, status, error) {
},
complete: function(data) {
}
});
}
}
The above Javascript code obtains the content of m3u8 URL and parses the content asynchronously using jQuery+Ajax. It has been integrated into the client-side Chrome Video Download Extension
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
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: How to Enable Inline Chrome Extension Installation in Chrome Browser?
Next Post: Key Considerations for Developing Online Casino Games