|
| 1 | +package mServer.crawler.sender.ard; |
| 2 | + |
| 3 | +import de.mediathekview.mlib.tool.Log; |
| 4 | +import mServer.crawler.sender.base.Qualities; |
| 5 | + |
| 6 | +import java.util.EnumMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +/** |
| 12 | + * * Converts an m3u8 playlist URL (in the style shown in the example) * into a list of progressive |
| 13 | + * mp4 URLs. * * Default progressive base host: https://wdr-progressive.ard-mcdn.de * * Example: * |
| 14 | + * input: * |
| 15 | + * https://wdrvod-rwrtr.akamaized.net/i/,/media/.../ID_AVC-,270,360,540,720,1080,.mp4.csmil/index-...m3u8 |
| 16 | + * * * produced outputs: * https://wdr-progressive.ard-mcdn.de/media/.../ID_AVC-270.mp4 * ... |
| 17 | + */ |
| 18 | +public class WdrM3U8ToMp4Converter { |
| 19 | + |
| 20 | + // Looks for /media/...<codec>-<comma-list>.mp4.csmil |
| 21 | + // group(1) = /media/... (path before codec) |
| 22 | + // group(2) = _<CODEC>- (codec token + trailing '-') |
| 23 | + // group(3) = the comma separated tail that contains bitrate numbers (e.g. ",270,360,540,") |
| 24 | + private static final Pattern MEDIA_PATTERN = |
| 25 | + Pattern.compile( |
| 26 | + "(/media/.+?)(_[A-Za-z0-9]+-)([^/]+?)\\.mp4\\.csmil", Pattern.CASE_INSENSITIVE); |
| 27 | + |
| 28 | + private final String progressiveBase; // no trailing slash |
| 29 | + |
| 30 | + public WdrM3U8ToMp4Converter() { |
| 31 | + this("https://wdr-progressive.ard-mcdn.de"); |
| 32 | + } |
| 33 | + |
| 34 | + public WdrM3U8ToMp4Converter(String progressiveBase) { |
| 35 | + if (progressiveBase == null) |
| 36 | + throw new IllegalArgumentException("progressiveBase must not be null"); |
| 37 | + // ensure no trailing slash to make concatenation predictable |
| 38 | + this.progressiveBase = progressiveBase.replaceAll("/+$", ""); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Convert an m3u8 url into a list of mp4 URLs. |
| 43 | + * |
| 44 | + * @param m3u8Url the source m3u8 url |
| 45 | + * @return map of mp4 URLs (in the same order as bitrates found) |
| 46 | + * @throws IllegalArgumentException if the url cannot be parsed or no bitrates found |
| 47 | + */ |
| 48 | + public Map<Qualities, String> convert(String m3u8Url) { |
| 49 | + if (m3u8Url == null) throw new IllegalArgumentException("m3u8Url must not be null"); |
| 50 | + |
| 51 | + Map<Qualities, String> result = new EnumMap<>(Qualities.class); |
| 52 | + |
| 53 | + Matcher m = MEDIA_PATTERN.matcher(m3u8Url); |
| 54 | + if (!m.find()) { |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + String pathBeforeCodec = m.group(1); // includes leading /media/... |
| 59 | + String codecWithDash = m.group(2); // e.g. _AVC- |
| 60 | + String bitrateListPart = m.group(3); // e.g. ",270,360,540,720,1080," or "270,360" |
| 61 | + |
| 62 | + // build the base path (starts with /media/...) |
| 63 | + String basePrefix = pathBeforeCodec + codecWithDash; // ends with '-' |
| 64 | + |
| 65 | + // extract numeric tokens (bitrate values) |
| 66 | + Pattern digits = Pattern.compile("\\d+"); |
| 67 | + Matcher mDigits = digits.matcher(bitrateListPart); |
| 68 | + |
| 69 | + while (mDigits.find()) { |
| 70 | + String bitrate = mDigits.group(); |
| 71 | + // join progressive base + basePrefix + bitrate + .mp4 |
| 72 | + String mp4 = progressiveBase + basePrefix + bitrate + ".mp4"; |
| 73 | + final Qualities resolution = getResolutionFromWidth(bitrate); |
| 74 | + result.put(resolution, mp4); |
| 75 | + } |
| 76 | + |
| 77 | + if (result.isEmpty()) { |
| 78 | + throw new IllegalArgumentException("No numeric bitrate tokens found in m3u8 URL: " + m3u8Url); |
| 79 | + } |
| 80 | + |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + private Qualities getResolutionFromWidth(String bitrate) { |
| 85 | + try { |
| 86 | + return switch (Integer.parseInt(bitrate)) { |
| 87 | + case 720 -> Qualities.NORMAL; |
| 88 | + case 1080 -> Qualities.HD; |
| 89 | + case 540, 360, 270 -> Qualities.SMALL; |
| 90 | + default -> { |
| 91 | + Log.sysLog("Unknown bitrate found in m3u8 URL: " + bitrate + ", defaulting to VERY_SMALL"); |
| 92 | + yield Qualities.SMALL; |
| 93 | + } |
| 94 | + }; |
| 95 | + } catch (NumberFormatException e) { |
| 96 | + Log.errorLog(165346373, e); |
| 97 | + return Qualities.SMALL; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments