The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
fix(Browser Extension): 修复抖音和 TikTok 下载改动带来的回归
XiaoYouChR864d34ffa5a5
diff --git a/browser_extension/GLOSSARY.md b/browser_extension/GLOSSARY.mdindex eb66f32c..57cba915 100644--- a/browser_extension/GLOSSARY.md+++ b/browser_extension/GLOSSARY.md@@ -96,7 +96,7 @@ _Avoid_: confirmed、bound**Clicked Video Match**:判断一个 URL 是否装着用户点击的那个视频:same / other / unknown。-证据是精确的——页面上的视频 id 与 URL 里的视频 id 相同,或文件时长与该 video 元素的时长相差不超过一帧;时长相差超过一秒才判为 other。+证据是精确的——页面上的视频 id 与 URL 里的视频 id 相同,或文件时长与该 video 元素的时长相差不超过一帧;文件比该 video 元素长出一秒以上才判为 other。解析时 Clicked Video Match 优先于 MSE Lock,MSE Lock 优先于捕获时间。_Avoid_: page videodiff --git a/browser_extension/app/src/page-media/attribution/attribution.ts b/browser_extension/app/src/page-media/attribution/attribution.tsindex 29b2fa8c..a968d6ea 100644--- a/browser_extension/app/src/page-media/attribution/attribution.ts+++ b/browser_extension/app/src/page-media/attribution/attribution.ts@@ -1,7 +1,7 @@import {isCatCatchMedia} from "../../shared/cat-catch";import {fileExtension, filenameFromUrl, mimeFromUrl} from "../../shared/utils";-import {fbCdnDuration, instagramAssetId, isInstagramCdnUrl, isStreamUrl, matchPageVideo, urlIdHints} from "../resolution/url-classify";-import type {VideoMatch} from "../resolution/url-classify";+import {fbCdnDuration, instagramAssetId, isInstagramCdnUrl, isStreamUrl, matchClickedVideoById, urlIdHints} from "../resolution/url-classify";+import type {ClickedVideoMatch} from "../resolution/url-classify";import {AttributionLedger} from "./attribution-ledger";import {selectMediaForPage} from "../resolution/strategy";@@ -30,11 +30,11 @@ const FETCH_BUFFER_CORRELATION_MS = 2500;const RECENT_FETCHES_CAP = 16;// One AAC frame: a DASH audio track ends up to a frame short of the element's (video) duration.const SAME_DURATION_TOLERANCE_S = 0.025;-// Rejecting is the risky verdict, so it waits for a gap no track of the same video shows.+// The element lasts as long as its longest track, so only a file outlasting it by more than any rounding is another video.const OTHER_DURATION_GAP_S = 1;const DURATIONS_CAP = 512;-type MatchClickedVideo = (url: string) => VideoMatch;+type MatchClickedVideo = (url: string) => ClickedVideoMatch;function toFormKind(mimeTypes: Set<string>): VideoSessionFormKind {if (mimeTypes.size === 0) { return "unknown"; }@@ -692,12 +692,12 @@ class MediaAttribution {const isSameLength = (duration: number) => Math.abs(duration - target) <= SAME_DURATION_TOLERANCE_S;const canCompare = Number.isFinite(target) && target > 0 && [...this.durationByUrl.values()].some(isSameLength);return (url) => {- const byPage = matchPageVideo(url, pageUrl);+ const byPage = matchClickedVideoById(url, pageUrl);if (byPage !== "unknown" || !canCompare) { return byPage; }const duration = this.durationByUrl.get(url);if (duration === undefined) { return "unknown"; }if (isSameLength(duration)) { return "same"; }- return Math.abs(duration - target) > OTHER_DURATION_GAP_S ? "other" : "unknown";+ return duration - target > OTHER_DURATION_GAP_S ? "other" : "unknown";};}diff --git a/browser_extension/app/src/page-media/attribution/mp4.ts b/browser_extension/app/src/page-media/attribution/mp4.tsindex 1d27d416..c4ba4e29 100644--- a/browser_extension/app/src/page-media/attribution/mp4.ts+++ b/browser_extension/app/src/page-media/attribution/mp4.ts@@ -52,6 +52,11 @@ export function parseMp4Duration(bytes: Uint8Array): number | undefined {return timescale > 0 && duration > 0 ? duration / timescale : undefined;}+export function isInitSegment(bytes: Uint8Array): boolean {+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ return parseBoxes(view, 0, view.byteLength).some((box) => box.type === "moov");+}+// The tail of a media segment's samples: entropy-coded bytes, unlike the head's encoder SEI that repeats across videos.export function parseMdatTail(bytes: Uint8Array, length: number): Uint8Array | undefined {const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);diff --git a/browser_extension/app/src/page-media/attribution/mse-probe.ts b/browser_extension/app/src/page-media/attribution/mse-probe.tsindex a3f988a5..d6260d38 100644--- a/browser_extension/app/src/page-media/attribution/mse-probe.ts+++ b/browser_extension/app/src/page-media/attribution/mse-probe.ts@@ -5,7 +5,7 @@* capturing buffers. Built as a standalone IIFE bundle (see scripts/build.mjs).*/import {postMediaSignal} from "./attribution-signal";-import {isMp4Head, MP4_HEAD_LIMIT, parseMdatTail, parseMp4Duration} from "./mp4";+import {isInitSegment, isMp4Head, MP4_HEAD_LIMIT, parseMdatTail, parseMp4Duration} from "./mp4";declare global {interface Window {@@ -36,6 +36,11 @@ const HEADS_CAP = 16;return head;}+ // Repeated silent AAC frames are byte-identical across unrelated videos.+ function isDistinctive(tail: Uint8Array): boolean {+ return new Set(tail).size >= MDAT_TAIL_LENGTH / 2;+ }+function hasBytes(haystack: Uint8Array, needle: Uint8Array): boolean {for (let i = haystack.indexOf(needle[0]); i >= 0 && i + needle.length <= haystack.length; i = haystack.indexOf(needle[0], i + 1)) {let j = 1;@@ -75,11 +80,13 @@ const HEADS_CAP = 16;postMediaSignal({ kind: "mse_buffer_appended", mediaSourceId: id, mimeType });try {const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);- const tail = parseMdatTail(bytes, MDAT_TAIL_LENGTH);- if (!tail) {- // An init segment starts the next video in a reused SourceBuffer, so the match starts over.+ if (isInitSegment(bytes)) {+ // An init segment starts the next video in a reused SourceBuffer, so the old match must not outlive it.+ if (sourceUrls.length > 0) { postMediaSignal({ kind: "mse_buffer_matched", mediaSourceId: id, mimeType, urls: [] }); }sourceUrls = [];- } else if (sourceUrls.length !== 1) {+ }+ const tail = parseMdatTail(bytes, MDAT_TAIL_LENGTH);+ if (tail && isDistinctive(tail) && sourceUrls.length !== 1) {const found = sourceUrlsOf(tail);// Each later segment can only narrow the holders down.const narrowed = sourceUrls.length === 0 ? found : sourceUrls.filter((url) => found.includes(url));@@ -209,9 +216,9 @@ const HEADS_CAP = 16;}} catch { /* responseText throws on non-text responseType. */ }try {- if (xhr.responseType === "arraybuffer" && xhr.response instanceof ArrayBuffer) {+ if (xhr.responseType === "arraybuffer" && xhr.response instanceof ArrayBuffer && isMp4Head(new Uint8Array(xhr.response).subarray(0, 8))) {const head = new Uint8Array(xhr.response.slice(0, MP4_HEAD_LIMIT));- if (isMp4Head(head)) { addHead(resolvedUrl, head); }+ addHead(resolvedUrl, head);const duration = parseMp4Duration(head);if (duration !== undefined) { postMediaSignal({ kind: "media_duration", url: resolvedUrl, duration }); }}diff --git a/browser_extension/app/src/page-media/resolution/strategies/generic.ts b/browser_extension/app/src/page-media/resolution/strategies/generic.tsindex ceb82381..a2da44d5 100644--- a/browser_extension/app/src/page-media/resolution/strategies/generic.ts+++ b/browser_extension/app/src/page-media/resolution/strategies/generic.ts@@ -74,11 +74,14 @@ export function selectGeneric(ctx: ResolveContext): Resolution {const evidenced = post.filter((r) => r.clickedVideo === "same");// Two evidenced files are a duration collision between reels, not a choice.const evidencedMuxed = evidenced.filter((r) => isMuxedUrl(r.url));- const muxed = evidencedMuxed.length === 1 ? evidencedMuxed[0].url : (post.length === 1 && isMuxedUrl(post[0].url) ? post[0].url : undefined);+ const onlyMuxed = post.length === 1 && isMuxedUrl(post[0].url) ? post[0].url : undefined;+ const muxed = evidencedMuxed.length === 1 ? evidencedMuxed[0].url : onlyMuxed;if (muxed) {return { kind: "selection", selection: { kind: "single", url: stripRangeParams(muxed), formKind: "unknown" } };}- const pair = selectMergePair(evidenced, classifyTrackRole) ?? (post.length === 2 ? selectMergePair(post, classifyTrackRole) : null);+ // An mp4 beside an unrelated mp3 is no pair, so without evidence only URLs naming themselves as tracks are paired.+ const isTrackPair = post.length === 2 && post.every((r) => isDashSegmentUrl(r.url));+ const pair = selectMergePair(evidenced, classifyTrackRole) ?? (isTrackPair ? selectMergePair(post, classifyTrackRole) : null);if (pair) {return { kind: "selection", selection: { kind: "merge", video: stripRangeParams(pair.video), audio: stripRangeParams(pair.audio) } };}diff --git a/browser_extension/app/src/page-media/resolution/strategy.ts b/browser_extension/app/src/page-media/resolution/strategy.tsindex 0acb62d2..4ddadbd6 100644--- a/browser_extension/app/src/page-media/resolution/strategy.ts+++ b/browser_extension/app/src/page-media/resolution/strategy.ts@@ -2,7 +2,7 @@ import {selectGeneric} from "./strategies/generic";import {selectMeta} from "./strategies/instagram";import {selectYouTube} from "./strategies/youtube";import {isStreamUrl} from "./url-classify";-import type {VideoMatch} from "./url-classify";+import type {ClickedVideoMatch} from "./url-classify";import type {Resolution, VideoSessionFormKind} from "../types";// One attributed URL as the strategies see it — also the element type of@@ -14,7 +14,7 @@ export type AttributedUrlView = {readonly isMaster?: boolean;readonly isLockedByMse: boolean;// Whether the URL holds the video the user clicked, judged by the Attribution Engine.- readonly clickedVideo: VideoMatch;+ readonly clickedVideo: ClickedVideoMatch;};// Strategies see only this — they MUST NOT reach back into the controller.diff --git a/browser_extension/app/src/page-media/resolution/url-classify.ts b/browser_extension/app/src/page-media/resolution/url-classify.tsindex d2b81dd5..cf7dcf63 100644--- a/browser_extension/app/src/page-media/resolution/url-classify.ts+++ b/browser_extension/app/src/page-media/resolution/url-classify.ts@@ -137,9 +137,12 @@ export function urlIdHints(url: string): Set<string> {return result;}-function queryIdValues(parsed: URL): Set<string> {+// CDN path segments are storage hashes and v/id/aid also carry cache busters, so neither can prove "other".+const OTHER_PROVING_QUERY_KEYS = ["__vid", "modal_id", "video_id", "bvid"];++function queryIdValues(parsed: URL, keys: string[]): Set<string> {const values = new Set<string>();- for (const key of VIDEO_ID_QUERY_KEYS) {+ for (const key of keys) {const value = parsed.searchParams.get(key);if (value && value.length >= 4) { values.add(value); }}@@ -150,24 +153,23 @@ function pathIdValues(parsed: URL): Set<string> {return new Set(parsed.pathname.split("/").filter(looksLikeVideoIdSegment));}-export type VideoMatch = "same" | "other" | "unknown";+export type ClickedVideoMatch = "same" | "other" | "unknown";-// By value, not key: Douyin's page says modal_id=X where its CDN says __vid=X. CDN path segments are storage hashes, so they never prove "other".-export function matchPageVideo(url: string, pageUrl: URL): VideoMatch {+// By value, not key: Douyin's page says modal_id=X where its CDN says __vid=X.+export function matchClickedVideoById(url: string, pageUrl: URL): ClickedVideoMatch {let parsed: URL;try {parsed = new URL(url);} catch {return "unknown";}- const pageQueryIds = queryIdValues(pageUrl);- const pageIds = new Set([...pageQueryIds, ...pathIdValues(pageUrl)]);+ const pageIds = new Set([...queryIdValues(pageUrl, VIDEO_ID_QUERY_KEYS), ...pathIdValues(pageUrl)]);if (pageIds.size === 0) { return "unknown"; }- const queryIds = queryIdValues(parsed);- for (const id of [...queryIds, ...pathIdValues(parsed)]) {+ for (const id of [...queryIdValues(parsed, VIDEO_ID_QUERY_KEYS), ...pathIdValues(parsed)]) {if (pageIds.has(id)) { return "same"; }}- return queryIds.size > 0 && pageQueryIds.size > 0 ? "other" : "unknown";+ const isProvable = queryIdValues(parsed, OTHER_PROVING_QUERY_KEYS).size > 0 && queryIdValues(pageUrl, OTHER_PROVING_QUERY_KEYS).size > 0;+ return isProvable ? "other" : "unknown";}export function classifyTrackRole(url: string, contentType: string): TrackRole {