297 lines
8.9 KiB
Python
297 lines
8.9 KiB
Python
# License: GNU AGPL v3: http://www.gnu.org/licenses/
|
|
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
|
|
|
|
"""Provide HLS protocol utilities."""
|
|
|
|
|
|
import contextlib
|
|
import os
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
import m3u8
|
|
|
|
from . import subtitles
|
|
from .error import UnexpectedHLSResponse
|
|
from .model import Rendition, RenditionAudio, RenditionSubtitles, Source, Variant
|
|
|
|
#
|
|
# WARNING !
|
|
#
|
|
# This module does not aim for a full implementation of HLS, only the
|
|
# subset useful for the actual observed usage of ArteTV.
|
|
#
|
|
# - URIs are relative file paths
|
|
# - Program indexes have at least one variant
|
|
# - Every variant is of different resolution
|
|
# - Every variant has exactly one audio medium
|
|
# - Every variant has at most one subtitles medium
|
|
# - Audio and video indexes segments are incremental ranges of
|
|
# the same file
|
|
# - Subtitles indexes have only one segment
|
|
|
|
MIME_TYPE = "application/x-mpegURL"
|
|
|
|
|
|
def _fetch_index(http_session, url):
|
|
# Fetch a M3U8 playlist
|
|
r = http_session.get(url)
|
|
r.raise_for_status()
|
|
|
|
if (_ := r.headers["content-type"]) != MIME_TYPE:
|
|
raise UnexpectedHLSResponse("MIME_TYPE", url, MIME_TYPE, _)
|
|
|
|
r.encoding = "utf-8"
|
|
|
|
return m3u8.loads(r.text, url)
|
|
|
|
|
|
def fetch_program_sources(http_session, metadata, program_index_url):
|
|
"""Fetch the given index and yield available sources."""
|
|
program_index = _fetch_index(http_session, program_index_url)
|
|
|
|
audio_media = None
|
|
subtitles_media = None
|
|
|
|
for media in program_index.media:
|
|
match media.type:
|
|
case "AUDIO":
|
|
if audio_media:
|
|
raise UnexpectedHLSResponse(
|
|
"MULTIPLE_AUDIO_MEDIA", program_index_url
|
|
)
|
|
audio_media = media
|
|
case "SUBTITLES":
|
|
if subtitles_media:
|
|
raise UnexpectedHLSResponse(
|
|
"MULTIPLE_SUBTITLES_MEDIA", program_index_url
|
|
)
|
|
subtitles_media = media
|
|
|
|
if not audio_media:
|
|
raise UnexpectedHLSResponse("NO_AUDIO_MEDIA", program_index_url)
|
|
|
|
rendition = Rendition(
|
|
RenditionAudio(
|
|
audio_media.language,
|
|
audio_media.name.startswith("VO"),
|
|
(
|
|
audio_media.characteristics is not None
|
|
and ("public.accessibility" in audio_media.characteristics)
|
|
),
|
|
),
|
|
RenditionSubtitles(
|
|
subtitles_media.language,
|
|
(
|
|
subtitles_media.characteristics is not None
|
|
and ("public.accessibility" in subtitles_media.characteristics)
|
|
),
|
|
)
|
|
if subtitles_media
|
|
else None,
|
|
)
|
|
|
|
cache = set()
|
|
|
|
for video_media in program_index.playlists:
|
|
stream_info = video_media.stream_info
|
|
if stream_info.audio != audio_media.group_id:
|
|
raise UnexpectedHLSResponse(
|
|
"INVALID_VARIANT_AUDIO_MEDIA", program_index_url, stream_info.audio
|
|
)
|
|
|
|
if subtitles_media:
|
|
if stream_info.subtitles != subtitles_media.group_id:
|
|
raise UnexpectedHLSResponse(
|
|
"INVALID_VARIANT_SUBTITLES_MEDIA",
|
|
program_index_url,
|
|
stream_info.subtitles,
|
|
)
|
|
elif stream_info.subtitles:
|
|
raise UnexpectedHLSResponse(
|
|
"INVALID_VARIANT_SUBTITLES_MEDIA",
|
|
program_index_url,
|
|
stream_info.subtitles,
|
|
)
|
|
|
|
variant = Variant(
|
|
stream_info.resolution[0],
|
|
stream_info.resolution[1],
|
|
stream_info.frame_rate,
|
|
)
|
|
|
|
if variant in cache:
|
|
raise UnexpectedHLSResponse("DUPLICATE_VARIANT", program_index_url, variant)
|
|
cache.add(variant)
|
|
|
|
yield Source(
|
|
metadata,
|
|
rendition,
|
|
variant,
|
|
video_media.absolute_uri,
|
|
audio_media.absolute_uri,
|
|
subtitles_media.absolute_uri if subtitles_media else None,
|
|
)
|
|
|
|
|
|
def _convert_byterange(obj):
|
|
# Convert a M3U8 `byterange` (1) to an `http range` (2).
|
|
# 1. "count@offset"
|
|
# 2. (start, end)
|
|
count, offset = [int(v) for v in obj.byterange.split("@")]
|
|
return offset, offset + count - 1
|
|
|
|
|
|
def _fetch_av_track_index(http_session, track_index_url):
|
|
# Fetch an audio or video index.
|
|
# Return a tuple:
|
|
# - the media file url
|
|
# - the media file's ranges
|
|
track_index = _fetch_index(http_session, track_index_url)
|
|
|
|
file_name = track_index.segment_map[0].uri
|
|
start, end = _convert_byterange(track_index.segment_map[0])
|
|
if start != 0:
|
|
raise UnexpectedHLSResponse("INVALID_AV_INDEX_FRAGMENT_START", track_index_url)
|
|
ranges = [(start, end)]
|
|
next_start = end + 1
|
|
|
|
for segment in track_index.segments:
|
|
if segment.uri != file_name:
|
|
raise UnexpectedHLSResponse("MULTIPLE_AV_INDEX_FILES", track_index_url)
|
|
|
|
start, end = _convert_byterange(segment)
|
|
if start != next_start:
|
|
raise UnexpectedHLSResponse(
|
|
"DISCONTINUOUS_AV_INDEX_FRAGMENT", track_index_url
|
|
)
|
|
|
|
ranges.append((start, end))
|
|
next_start = end + 1
|
|
|
|
return track_index.segment_map[0].absolute_uri, ranges
|
|
|
|
|
|
def _fetch_subtitles_track_index(http_session, track_index_url):
|
|
# Fetch subtitles index.
|
|
# Return the subtitle file url.
|
|
track_index = _fetch_index(http_session, track_index_url)
|
|
urls = [s.absolute_uri for s in track_index.segments]
|
|
|
|
if not urls:
|
|
raise UnexpectedHLSResponse("SUBTITLES_INDEX_NO_FILES", track_index_url)
|
|
|
|
if len(urls) > 1:
|
|
raise UnexpectedHLSResponse("SUBTITLES_INDEX_MULTIPLE_FILES", track_index_url)
|
|
|
|
return urls[0]
|
|
|
|
|
|
def _download_av_track(http_session, track_index_url, progress):
|
|
# Download an audio or video stream to temporary file.
|
|
# Return the temporary file name.
|
|
url, ranges = _fetch_av_track_index(http_session, track_index_url)
|
|
total = ranges[-1][1]
|
|
|
|
with (
|
|
NamedTemporaryFile(
|
|
mode="w+b", delete=False, prefix="delarte.", suffix=".mp4"
|
|
) as f
|
|
):
|
|
for range_start, range_end in ranges:
|
|
r = http_session.get(
|
|
url,
|
|
headers={
|
|
"Range": f"bytes={range_start}-{range_end}",
|
|
},
|
|
timeout=5,
|
|
)
|
|
|
|
r.raise_for_status()
|
|
|
|
if r.status_code != 206:
|
|
raise UnexpectedHLSResponse(
|
|
"UNEXPECTED_AV_TRACK_HTTP_STATUS",
|
|
track_index_url,
|
|
r.request.headers,
|
|
r.status,
|
|
)
|
|
|
|
if len(r.content) != range_end - range_start + 1:
|
|
raise UnexpectedHLSResponse(
|
|
"INVALID_AV_TRACK_FRAGMENT_LENGTH", track_index_url
|
|
)
|
|
f.write(r.content)
|
|
|
|
progress(range_end, total)
|
|
|
|
return f.name
|
|
|
|
|
|
def _download_subtitles_track(http_session, track_index_url, progress):
|
|
# Download a subtitle file (converted from VTT to SRT format) into a temporary file.
|
|
# Return the temporary file name.
|
|
url = _fetch_subtitles_track_index(http_session, track_index_url)
|
|
|
|
progress(0, 2)
|
|
r = http_session.get(url)
|
|
r.raise_for_status()
|
|
r.encoding = "utf-8"
|
|
progress(1, 2)
|
|
|
|
with NamedTemporaryFile(
|
|
"w", delete=False, prefix="delarte.", suffix=".srt", encoding="utf8"
|
|
) as f:
|
|
subtitles.convert(r.text, f)
|
|
progress(2, 2)
|
|
return f.name
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def download_source(http_session, source, progress):
|
|
"""Download source inputs into temporary files.
|
|
|
|
Returns a context manager that will delete the temporary files on exit.
|
|
The context expression is a local version of the given source.
|
|
"""
|
|
video_filename = None
|
|
audio_filename = None
|
|
subtitles_filename = None
|
|
|
|
try:
|
|
subtitles_filename = (
|
|
_download_subtitles_track(
|
|
http_session,
|
|
source.subtitles,
|
|
lambda i, n: progress("subtitles", i, n),
|
|
)
|
|
if source.subtitles
|
|
else None
|
|
)
|
|
|
|
video_filename = _download_av_track(
|
|
http_session, source.video, lambda i, n: progress("video", i, n)
|
|
)
|
|
|
|
audio_filename = _download_av_track(
|
|
http_session, source.audio, lambda i, n: progress("audio", i, n)
|
|
)
|
|
|
|
yield Source(
|
|
source.metadata,
|
|
source.rendition,
|
|
source.variant,
|
|
video_filename,
|
|
audio_filename,
|
|
subtitles_filename,
|
|
)
|
|
|
|
finally:
|
|
if video_filename and os.path.isfile(video_filename):
|
|
os.unlink(video_filename)
|
|
|
|
if audio_filename and os.path.isfile(audio_filename):
|
|
os.unlink(audio_filename)
|
|
|
|
if subtitles_filename and os.path.isfile(subtitles_filename):
|
|
os.unlink(subtitles_filename)
|