forked from kenzuya/unshackle
feat(manifests): add dts-hd, mpeg-h and lcevc codecs and a manifest corpus
Fix DASH SegmentTimeline negative @r and Pattern, TTML sidecar sets with no contentType, HLS CHANNELS identifiers and Dolby channel masks; test against 408 public DASH/HLS/ISM manifests.
This commit is contained in:
@@ -2587,6 +2587,8 @@ class dl:
|
||||
if not title.tracks.videos:
|
||||
self.log.error(f"There's no {', '.join(c.name for c in vcodec)} Video Track...")
|
||||
sys.exit(1)
|
||||
elif keep_videos:
|
||||
title.tracks.select_video(lambda x: x.codec != Video.Codec.LCEVC)
|
||||
|
||||
if keep_videos and range_:
|
||||
# Special handling for HYBRID - don't filter, keep all HDR10 and DV tracks
|
||||
|
||||
@@ -897,14 +897,14 @@ async def download(request: web.Request) -> web.Response:
|
||||
- type: array
|
||||
items:
|
||||
type: string
|
||||
description: Video codec(s) to download (e.g., "HEVC" or ["AVC", "HEVC"]) - accepts AVC, H.264, H264, HEVC, H.265, H265, VC1, VC-1, VP8, VP9, AV1 (default - None)
|
||||
description: Video codec(s) to download (e.g., "HEVC" or ["AVC", "HEVC"]) - accepts AVC, H.264, H264, HEVC, H.265, H265, VC1, VC-1, VP8, VP9, AV1, LCEVC (default - None)
|
||||
acodec:
|
||||
oneOf:
|
||||
- type: string
|
||||
- type: array
|
||||
items:
|
||||
type: string
|
||||
description: Audio codec(s) to download (e.g., "AAC" or ["AAC", "EC3"]) - accepts AAC, AC3, DD, EC3, DD+, EAC3, DDP, AC4, AC-4, OPUS, OGG, VORB, VORBIS, DTS, DTSX, DTS-X, ALAC, FLAC (default - None)
|
||||
description: Audio codec(s) to download (e.g., "AAC" or ["AAC", "EC3"]) - accepts AAC, AC3, DD, EC3, DD+, EAC3, DDP, AC4, AC-4, OPUS, OGG, VORB, VORBIS, DTS, DTSX, DTS-X, DTSHD, DTS-HD, MPEGH, MPEG-H, ALAC, FLAC (default - None)
|
||||
vbitrate:
|
||||
type: integer
|
||||
description: Video bitrate in kbps (default - None)
|
||||
|
||||
@@ -28,7 +28,7 @@ DYNAMIC_RANGE_MAP = {
|
||||
"HDR10 / HDR10+": "HDR10P",
|
||||
"HDR10 / HDR10": "HDR",
|
||||
}
|
||||
AUDIO_CODEC_MAP = {"E-AC-3": "DDP", "AC-3": "DD", "DTS-UHD": "DTS-X"}
|
||||
AUDIO_CODEC_MAP = {"E-AC-3": "DDP", "AC-3": "DD", "DTS-UHD": "DTS-X", "DTS LBR": "DTS-LBR", "MPEG-H 3D Audio": "MPEG-H"}
|
||||
|
||||
SPACED_AUDIO_CODECS = {"DTS-X"}
|
||||
|
||||
|
||||
@@ -230,6 +230,9 @@ class DASH:
|
||||
if not content_type and not mime_type:
|
||||
raise ValueError("Unable to determine the format of a Representation, cannot continue...")
|
||||
|
||||
if content_type == "application" and mime_type and "/mp4" not in mime_type:
|
||||
content_type = "text"
|
||||
|
||||
if mime_type == "application/mp4" or content_type == "application":
|
||||
# likely mp4-boxed subtitles
|
||||
# TODO: It may not actually be subtitles
|
||||
@@ -997,13 +1000,37 @@ class DASH:
|
||||
track_kid = track.get_key_id(init_data)
|
||||
|
||||
if segment_timeline is not None:
|
||||
# an S with @p takes its durations from a Pattern, cycling from index @pE
|
||||
patterns = {
|
||||
pattern.get("id"): [
|
||||
int(p.get("d")) for p in pattern.findall("P") for _ in range(1 + int(p.get("r") or 0))
|
||||
]
|
||||
for pattern in segment_timeline.findall("Pattern")
|
||||
}
|
||||
timeline = segment_timeline.findall("S")
|
||||
current_time = 0
|
||||
for s in segment_timeline.findall("S"):
|
||||
timeline_start = int(timeline[0].get("t") or 0) if timeline else 0
|
||||
for index, s in enumerate(timeline):
|
||||
if s.get("t"):
|
||||
current_time = int(s.get("t"))
|
||||
for _ in range(1 + (int(s.get("r") or 0))):
|
||||
durations = patterns[s.get("p")] if s.get("p") else [int(s.get("d"))]
|
||||
offset = int(s.get("pE") or 0)
|
||||
count = 1 + int(s.get("r") or 0)
|
||||
if count <= 0:
|
||||
# a negative @r repeats up to the next S@t, or to the end of the Period
|
||||
next_time = timeline[index + 1].get("t") if index + 1 < len(timeline) else None
|
||||
end_time: float
|
||||
if next_time:
|
||||
end_time = int(next_time)
|
||||
elif period_duration:
|
||||
end_time = timeline_start + DASH.pt_to_sec(period_duration) * segment_timescale
|
||||
else:
|
||||
raise ValueError("Duration of the Period was unable to be determined.")
|
||||
# round off float noise from the duration so it cannot add a segment
|
||||
count = math.ceil(round((end_time - current_time) / (sum(durations) / len(durations)), 6))
|
||||
for i in range(count):
|
||||
segment_durations.append(current_time)
|
||||
current_time += int(s.get("d"))
|
||||
current_time += durations[(offset + i) % len(durations)]
|
||||
|
||||
keep = track.sample_slice(segment_durations, segment_timescale, window) if window else slice(None)
|
||||
start_number += keep.start or 0
|
||||
|
||||
@@ -288,9 +288,14 @@ class HLS:
|
||||
codec: Union[Audio.Codec, Subtitle.Codec, None] = HLS.select_audio_codec(
|
||||
audio_codecs_by_group_id.get(media.group_id), media.uri
|
||||
)
|
||||
if media.channels and media.channels.endswith("/JOC"):
|
||||
joc = int(media.channels.split("/JOC")[0])
|
||||
media.channels = "5.1"
|
||||
if media.channels:
|
||||
# the channel count, then /-separated coding (JOC) and usage identifiers (RFC 8216bis)
|
||||
count, *identifiers = media.channels.split("/")
|
||||
if identifiers and "JOC" in identifiers[0].split(","):
|
||||
joc = int(count)
|
||||
media.channels = "5.1"
|
||||
else:
|
||||
media.channels = count
|
||||
else:
|
||||
track_type = Subtitle
|
||||
codec = Subtitle.Codec.WebVTT # assuming WebVTT, codec info isn't shown
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import re
|
||||
import subprocess
|
||||
from enum import Enum
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.tracks.track import Track
|
||||
from unshackle.core.tracks.track import MPEGH_SAMPLE_ENTRIES, Track
|
||||
|
||||
# the Dolby DASH channel configuration is the E-AC-3 chanmap (ETSI TS 102 366 Table E.1.4), read from the MSB
|
||||
DOLBY_CHANNEL_BITS = (
|
||||
(1, "main"), # L
|
||||
(1, "main"), # C
|
||||
(1, "main"), # R
|
||||
(1, "main"), # Ls
|
||||
(1, "main"), # Rs
|
||||
(2, "main"), # Lc/Rc
|
||||
(2, "main"), # Lrs/Rrs
|
||||
(1, "main"), # Cs
|
||||
(1, "height"), # Ts
|
||||
(2, "main"), # Lsd/Rsd
|
||||
(2, "main"), # Lw/Rw
|
||||
(2, "height"), # Vhl/Vhr
|
||||
(1, "height"), # Vhc
|
||||
(2, "height"), # Lts/Rts
|
||||
(1, "lfe"), # LFE2
|
||||
(1, "lfe"), # LFE
|
||||
)
|
||||
|
||||
|
||||
class Audio(Track):
|
||||
@@ -19,6 +40,8 @@ class Audio(Track):
|
||||
OGG = "VORB" # https://wikipedia.org/wiki/Vorbis
|
||||
DTS = "DTS" # https://en.wikipedia.org/wiki/DTS,_Inc.#DTS_Digital_Surround
|
||||
DTSX = "DTS-X" # https://en.wikipedia.org/wiki/DTS,_Inc.#DTS:X
|
||||
DTSHD = "DTS-HD" # https://en.wikipedia.org/wiki/DTS-HD_Master_Audio
|
||||
MPEGH = "MPEG-H" # https://en.wikipedia.org/wiki/MPEG-H_3D_Audio
|
||||
ALAC = "ALAC" # https://en.wikipedia.org/wiki/Apple_Lossless_Audio_Codec
|
||||
FLAC = "FLAC" # https://en.wikipedia.org/wiki/FLAC
|
||||
|
||||
@@ -43,6 +66,10 @@ class Audio(Track):
|
||||
return Audio.Codec.DTSX
|
||||
if mime == "dtsc":
|
||||
return Audio.Codec.DTS
|
||||
if mime in ("dtsh", "dtsl", "dtse"): # High Resolution, Master Audio, Express
|
||||
return Audio.Codec.DTSHD
|
||||
if mime in MPEGH_SAMPLE_ENTRIES:
|
||||
return Audio.Codec.MPEGH
|
||||
if mime == "alac":
|
||||
return Audio.Codec.ALAC
|
||||
if mime == "flac":
|
||||
@@ -204,11 +231,16 @@ class Audio(Track):
|
||||
It does not convert the value to assume a sub speaker channel layout, e.g. 5.1->6.0.
|
||||
"""
|
||||
if isinstance(channels, str):
|
||||
# TODO: Support all possible DASH channel configurations (https://datatracker.ietf.org/doc/html/rfc8216)
|
||||
if channels.upper() == "A000":
|
||||
return 2.0
|
||||
elif channels.upper() == "F801":
|
||||
return 5.1
|
||||
# TODO: decode a DASH AudioChannelConfiguration by its schemeIdUri (CICP, Dolby), not by its shape
|
||||
if re.fullmatch(r"[0-9A-Fa-f]{4}", channels) and not channels.isdigit():
|
||||
mask = int(channels, 16)
|
||||
counts = {"main": 0, "lfe": 0, "height": 0}
|
||||
for bit, (count, kind) in enumerate(DOLBY_CHANNEL_BITS):
|
||||
if mask & (0x8000 >> bit):
|
||||
counts[kind] += count
|
||||
if counts["height"]:
|
||||
return f"{counts['main']}.{counts['lfe']}.{counts['height']}"
|
||||
return float(f"{counts['main']}.{counts['lfe']}")
|
||||
layout = channels.replace("ch", "")
|
||||
if layout.count(".") == 2 and all(part.isdigit() for part in layout.split(".")):
|
||||
return layout
|
||||
|
||||
@@ -210,12 +210,16 @@ def strip_duplicate_init_boxes(src: Path, dst: Path) -> int:
|
||||
return dropped
|
||||
|
||||
|
||||
def has_dts_uhd_sample_entry(path: Path) -> bool:
|
||||
"""True if the MP4 holds a DTS-UHD (DTS:X Profile 2) audio sample entry.
|
||||
MPEGH_SAMPLE_ENTRIES = ("mha1", "mha2", "mhm1", "mhm2")
|
||||
|
||||
|
||||
def has_mp4_only_audio_entry(path: Path) -> bool:
|
||||
"""True if the MP4 holds a DTS-UHD (DTS:X Profile 2) or MPEG-H audio sample entry.
|
||||
|
||||
Matroska has no CodecID for DTS-UHD, so mkvmerge stores it as an A_QUICKTIME
|
||||
passthrough and every reader reports the container fallback instead of the codec. A
|
||||
file this returns True for has to go into MP4 to keep its codec readable.
|
||||
passthrough and every reader reports the container fallback instead of the codec.
|
||||
mkvmerge drops an MPEG-H track completely. A file this returns True for has to go
|
||||
into MP4 to keep its audio.
|
||||
|
||||
Only "dtsx"/"dtsy" mean DTS-UHD. DTS:X Profile 1 rides inside DTS-HD Master Audio as
|
||||
"dtsc"/"dtse"/"dtsh"/"dtsl", which Matroska maps to A_DTS and stores properly, so it
|
||||
@@ -231,7 +235,7 @@ def has_dts_uhd_sample_entry(path: Path) -> bool:
|
||||
return False
|
||||
if not moov:
|
||||
return False
|
||||
for tok in (b"dtsx", b"dtsy"):
|
||||
for tok in (b"dtsx", b"dtsy", *(entry.encode() for entry in MPEGH_SAMPLE_ENTRIES)):
|
||||
i = moov.find(tok, 4)
|
||||
while i != -1:
|
||||
size = int.from_bytes(moov[i - 4 : i], "big")
|
||||
|
||||
@@ -26,7 +26,7 @@ from unshackle.core.tracks.attachment import Attachment
|
||||
from unshackle.core.tracks.audio import Audio
|
||||
from unshackle.core.tracks.chapters import Chapter, Chapters
|
||||
from unshackle.core.tracks.subtitle import Subtitle
|
||||
from unshackle.core.tracks.track import Track, has_dts_uhd_sample_entry, strip_duplicate_init_boxes
|
||||
from unshackle.core.tracks.track import Track, has_mp4_only_audio_entry, strip_duplicate_init_boxes
|
||||
from unshackle.core.tracks.video import Video
|
||||
from unshackle.core.utilities import (
|
||||
is_close_match,
|
||||
@@ -574,7 +574,7 @@ class Tracks:
|
||||
"""
|
||||
Multiplex the Tracks into an MP4 Container with MP4Box.
|
||||
|
||||
Only used for a codec Matroska cannot name, currently DTS-UHD, because MP4 carries
|
||||
Only used for a codec Matroska cannot carry, DTS-UHD or MPEG-H, because MP4 carries
|
||||
far less: it drops the forced, hearing-impaired and original flags, holds no
|
||||
attachments at all, and leaves out a subtitle it has no format for rather than
|
||||
mangling it. Losing those beats shipping audio no player can name.
|
||||
@@ -583,7 +583,7 @@ class Tracks:
|
||||
"""
|
||||
mp4box = getattr(binaries, "MP4Box", None)
|
||||
if not mp4box:
|
||||
raise RuntimeError("MP4Box (GPAC) not found, muxing DTS-UHD (DTS:X Profile 2) audio needs it")
|
||||
raise RuntimeError("MP4Box (GPAC) not found, muxing DTS-UHD or MPEG-H audio needs it")
|
||||
|
||||
progress = progress or partial(lambda **kwargs: None)
|
||||
|
||||
@@ -669,7 +669,7 @@ class Tracks:
|
||||
"muxer": str(mp4box),
|
||||
"command": full_command,
|
||||
"container": "mp4",
|
||||
"reason": "DTS-UHD audio has no Matroska CodecID",
|
||||
"reason": "DTS-UHD or MPEG-H audio has no Matroska CodecID",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -788,7 +788,7 @@ class Tracks:
|
||||
video.needs_repack = True
|
||||
video.data["audio_language"] = lang_code
|
||||
|
||||
if any(at.path and at.path.exists() and has_dts_uhd_sample_entry(at.path) for at in self.audio):
|
||||
if any(at.path and at.path.exists() and has_mp4_only_audio_entry(at.path) for at in self.audio):
|
||||
return self._mux_mp4(
|
||||
title=title,
|
||||
delete=delete,
|
||||
|
||||
@@ -29,6 +29,7 @@ class Video(Track):
|
||||
VP8 = "VP8"
|
||||
VP9 = "VP9"
|
||||
AV1 = "AV1"
|
||||
LCEVC = "LCEVC"
|
||||
|
||||
@property
|
||||
def extension(self) -> str:
|
||||
@@ -66,6 +67,8 @@ class Video(Track):
|
||||
return Video.Codec.VP9
|
||||
if mime == "av01":
|
||||
return Video.Codec.AV1
|
||||
if mime == "lvc1":
|
||||
return Video.Codec.LCEVC
|
||||
raise ValueError(f"The MIME '{mime}' is not a supported Video Codec")
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user