mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-12-06 14:55:02 +01:00
use extinf
This commit is contained in:
parent
c7df4bf3ec
commit
2c6b324278
1 changed files with 25 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import re
|
import re
|
||||||
|
import urllib.parse
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from .common import InfoExtractor
|
from .common import InfoExtractor
|
||||||
|
|
@ -29,28 +30,38 @@ class PlutoTVBase(InfoExtractor):
|
||||||
|
|
||||||
def _to_ad_free_formats(self, video_id, formats):
|
def _to_ad_free_formats(self, video_id, formats):
|
||||||
for fmt in formats:
|
for fmt in formats:
|
||||||
res = self._download_webpage(
|
res, base = self._download_webpage_handle(
|
||||||
fmt.get('url'), video_id, 'Downloading m3u8 playlist',
|
fmt.get('url'), video_id, 'Downloading m3u8 playlist',
|
||||||
fatal=False)
|
fatal=False)
|
||||||
if not res:
|
if not res:
|
||||||
continue
|
continue
|
||||||
lines = res.splitlines()
|
base = base.url
|
||||||
max_iv = 0
|
res = res.splitlines()
|
||||||
url_path = None
|
path_occur = {}
|
||||||
for line in lines:
|
extinf = 0
|
||||||
if line.startswith('#EXT-X-KEY:'):
|
for line in res:
|
||||||
path, iv = re.search(r'URI="(?P<path>[^"]+)/[^.]+\.key",IV=0x0*(?P<iv>\d+)', line).group('path', 'iv')
|
match = re.match(r'^#EXTINF:(\d+)', line)
|
||||||
iv = int(iv)
|
if match:
|
||||||
if iv > max_iv:
|
extinf = float_or_none(match.group(1)) or 0
|
||||||
max_iv = iv
|
elif not line.startswith('#'):
|
||||||
url_path = path
|
match = re.search(r'^(.+)/[^/]+$', line)
|
||||||
|
if match:
|
||||||
|
path = urllib.parse.urlparse(urllib.parse.urljoin(base, match.group(1))).path
|
||||||
|
path_occur[path] = path_occur.get(path, 0) + extinf
|
||||||
|
url_path = max(path_occur, key=path_occur.get)
|
||||||
if url_path:
|
if url_path:
|
||||||
fmt['hls_media_playlist_data'] = ''
|
fmt['hls_media_playlist_data'] = ''
|
||||||
valid = True
|
valid = True
|
||||||
for line in lines:
|
for line in res:
|
||||||
|
# prevent key mismatch
|
||||||
if line.startswith('#EXT-X-KEY:'):
|
if line.startswith('#EXT-X-KEY:'):
|
||||||
path = re.search(r'URI="([^"]+)/[^.]+\.key"', line).group(1)
|
match = re.search(r'URI="([^"]+)/[^.]+\.key"', line)
|
||||||
valid = path == url_path
|
# if no match, the line is probably malformed, keep it as is
|
||||||
|
valid = not match or urllib.parse.urlparse(urllib.parse.urljoin(base, match.group(1))).path == url_path
|
||||||
|
elif not line.startswith('#'):
|
||||||
|
match = re.search(r'^(.+)/[^/]+$', line)
|
||||||
|
if match:
|
||||||
|
valid = urllib.parse.urlparse(urllib.parse.urljoin(base, match.group(1))).path == url_path
|
||||||
if valid:
|
if valid:
|
||||||
fmt['hls_media_playlist_data'] += line + '\n'
|
fmt['hls_media_playlist_data'] += line + '\n'
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue