Open PannenkoekenNL opened 7 years ago
meanwhile, here is an implementation in PHP-cli, maybe it'll help speed up development of a youtube-dl python implementation
#!/usr/bin/env php
<?php
declare (strict_types = 1);
if ($argc !== 2 && $argc !== 3) {
die("usage: {$argv[0]} url {int_version}\n");
}
$url = $argv[1];
$quality = (int)($argv[2] ?? -1);
class HC
{
public $ch;
public function __construct()
{
$this->ch = curl_init();
curl_setopt_array($this->ch, array(
CURLOPT_ENCODING => '',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => '',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => false
));
}
public function __destruct()
{
curl_close($this->ch);
}
public function fetch(string $url)
{
curl_setopt($this->ch, CURLOPT_URL, $url);
return curl_exec($this->ch);
}
};
$hc = new HC();
$html = $hc->fetch($url);
// playlist.source = "https://ztv-mcloud-mpl-s3.zadn.vn/9aVVgQFDDxU/whls/vod/480/lxgx4OHaPMKb9IVNfG/HaoLanTruyen_10_muxed.m3u8?authen=exp=1548875226~acl=/9aVVgQFDDxU/*~hmac=8c1864f2bcfe04c9bb306a2b1651acb5";
$domd = @DOMDocument::loadHTML($html);
$xp = new DOMXPath($domd);
$title = $xp->query("//div[@class='box-description']//strong")->item(0)->getAttribute("alt");
echo "title: {$title}\n";
$versions = array();
/*
playlist.sources.push({
label: "720p(VIP)",
disabled: true,
isHD: true,
index: 100
});
playlist.sources.push({
label: "480p",
source: "https://ztv-mcloud-bf-s3.zadn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541",
index: 1
});
*/
preg_match_all("/playlist\.sources\.push\s*\(\s*(\{[\s\S]*?\}\s*)\s*\)\s*\;/", $html, $matches);
if (!empty($matches)) {
$matches = $matches[1];
$matches = array_filter(array_map(function (string $str) {
$str = preg_replace('/(\S+)\:([\s\"0-9]|(?:null))/', '"${1}":${2}', $str);
return (json_decode($str, true) ?? []);
}, $matches), function (array $data) {
return (empty($data['disabled']) && !empty($data['label']) && !empty($data['source']));
});
foreach ($matches as $match) {
$versions[] = array(
'type' => 'direct',
'quality' => $match['label'],
'url' => $match['source'],
);
}
}
// playlist.source = "https://ztv-mcloud-mpl-s3.zadn.vn/9aVVgQFDDxU/whls/vod/480/lxgx4OHaPMKb9IVNfG/HaoLanTruyen_10_muxed.m3u8?authen=exp=1548875226~acl=/9aVVgQFDDxU/*~hmac=8c1864f2bcfe04c9bb306a2b1651acb5";
preg_match("/playlist\.source\s*\=\s*\"([^\"]*)/", $html, $matches);
$versionsURL = $matches[1];
$versions_raw = $hc->fetch($versionsURL);
$versions_raw = preg_split("/\r?\n/", $versions_raw, -1, PREG_SPLIT_NO_EMPTY);
for ($i = 1; $i < count($versions_raw); $i += 2) {
preg_match('/NAME\=\"(.*?)\"/', $versions_raw[$i], $q);
$q = $q[1];
$u = 'https:' . $versions_raw[$i + 1];
$versions[] = ['type' => 'm3u8', 'quality' => $q, 'url' => $u];
}
if (!isset($versions[$quality])) {
echo "available version: \n";
print_r($versions);
echo "you must chose a version, as a number, add it as the last argument.\n";
die(1);
}
if ($versions[$quality]["type"] === 'direct') {
$cmd = "wget " . escapeshellarg($versions[$quality]['url']) . " -O " . escapeshellarg($title . "." . $versions[$quality]["quality"] . ".mp4");
echo "{$cmd}\n";
system($cmd);
} elseif ($versions[$quality]['type'] === 'm3u8') {
$urls_raw = $hc->fetch($versions[$quality]['url']);
$fname = $title . ".mp4";
echo " saving to \"{$fname}\"\n";
$fp = fopen($title . ".mp4", 'wb');
$urls_raw = array_values(array_filter(preg_split("/\r?\n/", $urls_raw), function ($str) {
return ('//' === substr($str, 0, 2));
}));
//var_dump($urls_raw);
for ($i = 0, $max = count($urls_raw); $i < $max; ++$i) {
echo "\rdownloading: " . ($i + 1) . "/{$max}";
fwrite($fp, $hc->fetch('https:' . $urls_raw[$i]));
}
echo " done!\n";
fclose($fp);
}
(license, if you're looking for one: https://unlicense.org/UNLICENSE )
examples:
php vdl.php 'https://tv.zing.vn/video/Hao-Lan-Truyen-Tap-10/IWZEUOD7.html'
title: Hạo Lan Truyện - Tập 10 Vietsub
available version:
Array
(
[0] => Array
(
[type] => direct
[quality] => 360p
[url] => https://ztv-mcloud-bf-s3.zadn.vn/F32TbEFuvIY/39ec5d719935706b2924/3855165c3219db478208/360/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/F32TbEFuvIY/*~hmac=e45964ff18ac53b36bd6090f7e9c9020
)
[1] => Array
(
[type] => direct
[quality] => 480p
[url] => https://ztv-mcloud-bf-s3.zadn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541
)
[2] => Array
(
[type] => m3u8
[quality] => 240p
[url] => https://mcloud-hls-pl-3-ztv-aka.zdn.vn/tLCZVEubRa4/whls/vod/240/wk_kvPDhQcegAYXGQG/HaoLanTruyen_10_muxed.m3u8?authen=exp=1549275188~acl=/tLCZVEubRa4/*~hmac=4ac9c6f6fbfffb529d466546dd9f3eb9
)
[3] => Array
(
[type] => m3u8
[quality] => 480p
[url] => https://mcloud-hls-pl-3-ztv-aka.zdn.vn/0r7cJgVxuNA/whls/vod/480/yVJrwfnaPMKb9IVNfG/HaoLanTruyen_10_muxed.m3u8?authen=exp=1549275188~acl=/0r7cJgVxuNA/*~hmac=94255dbee530a69c7a6551e8e9d259c0
)
[4] => Array
(
[type] => m3u8
[quality] => 360p
[url] => https://mcloud-hls-pl-3-ztv-aka.zdn.vn/kCGyIZ6njhQ/whls/vod/360/S7LqP1Bgw-khgwcTom/HaoLanTruyen_10_muxed.m3u8?authen=exp=1549275188~acl=/kCGyIZ6njhQ/*~hmac=ae758552e0e9ec3471bcfc1b4f876c81
)
)
you must chose a version, as a number, add it as the last argument.```
$ php vdl.php 'https://tv.zing.vn/video/Hao-Lan-Truyen-Tap-10/IWZEUOD7.html' 1 title: Hạo Lan Truyện - Tập 10 Vietsub wget 'https://ztv-mcloud-bf-s3.zadn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541' -O 'Hạo Lan Truyện - Tập 10 Vietsub.480p.mp4' --2019-02-02 01:09:05-- https://ztv-mcloud-bf-s3.zadn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541 Resolving ztv-mcloud-bf-s3.zadn.vn (ztv-mcloud-bf-s3.zadn.vn)... 49.213.106.142, 49.213.106.143 Connecting to ztv-mcloud-bf-s3.zadn.vn (ztv-mcloud-bf-s3.zadn.vn)|49.213.106.142|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://mcloud-bf-3-ztv-aka.zdn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541 [following] --2019-02-02 01:09:07-- https://mcloud-bf-3-ztv-aka.zdn.vn/mq1rFE38Xsc/635908c4cc8025de7c91/156b34621027f979a036/480/HaoLanTruyen_10_muxed.mp4?authen=exp=1549188788~acl=/mq1rFE38Xsc/*~hmac=3b0f9e0cfdc0ccf043f08b43d5dc3541 Resolving mcloud-bf-3-ztv-aka.zdn.vn (mcloud-bf-3-ztv-aka.zdn.vn)... 193.212.174.33, 193.212.174.11 Connecting to mcloud-bf-3-ztv-aka.zdn.vn (mcloud-bf-3-ztv-aka.zdn.vn)|193.212.174.33|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 315942033 (301M) [video/mp4] Saving to: ‘Hạo Lan Truyện - Tập 10 Vietsub.480p.mp4’
Hạo Lan Truyện - Tập 10 Vietsub.480p.mp4 1%[=> ] 3.81M 534KB/s eta 9m 29s```
$ php vdl.php 'https://tv.zing.vn/video/Hao-Lan-Truyen-Tap-10/IWZEUOD7.html' 4
title: Hạo Lan Truyện - Tập 10 Vietsub
saving to "Hạo Lan Truyện - Tập 10 Vietsub.mp4"
downloading: 3/549
Description of your issue, suggested solution and other information
Please add support for tv.zing.vn. The video URL is a mp4 in the source code:
Example: http://tv.zing.vn/video/Sien-Nhan-Perman-Tap-130/IWZBEEEF.html