twitter: use original media timestamp for fix check

This commit is contained in:
wukko 2023-12-17 23:45:15 +06:00
parent 8556a5fe2c
commit 4834f21554

View file

@ -1,13 +1,22 @@
import { genericUserAgent } from "../../config.js"; import { genericUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js"; import { createStream } from "../../stream/manage.js";
function bestQuality(arr) {
return arr.filter(v => v["content_type"] === "video/mp4").sort((a, b) => Number(b.bitrate) - Number(a.bitrate))[0]["url"]
}
// fix all videos affected by the container bug in twitter muxer (took them over two weeks to fix it????) // fix all videos affected by the container bug in twitter muxer (took them over two weeks to fix it????)
const badContainerStart = new Date(1701446400000); const badContainerStart = new Date(1701446400000);
const badContainerEnd = new Date(1702605600000); const badContainerEnd = new Date(1702605600000);
const TWITTER_EPOCH = 1288834974657n;
function needsFixing(media) {
const representativeId = media.source_status_id_str ?? media.id_str;
const mediaTimestamp = new Date(
Number((BigInt(representativeId) >> 22n) + TWITTER_EPOCH)
)
return mediaTimestamp > badContainerStart && mediaTimestamp < badContainerEnd;
}
function bestQuality(arr) {
return arr.filter(v => v["content_type"] === "video/mp4").sort((a, b) => Number(b.bitrate) - Number(a.bitrate))[0]["url"]
}
export default async function(obj) { export default async function(obj) {
let _headers = { let _headers = {
@ -64,13 +73,14 @@ export default async function(obj) {
let single, multiple = [], media = baseMedia["media"]; let single, multiple = [], media = baseMedia["media"];
media = media.filter((i) => { if (i["type"] === "video" || i["type"] === "animated_gif") return true }); media = media.filter((i) => { if (i["type"] === "video" || i["type"] === "animated_gif") return true });
let tweetDate = new Date(baseTweet.created_at), if (media.length === 0) {
needsFixing = tweetDate > badContainerStart && tweetDate < badContainerEnd; return { error: 'ErrorNoVideosInTweet' }
}
if (media.length > 1) { if (media.length > 1) {
for (let i in media) { for (let i in media) {
let downloadUrl = bestQuality(media[i]["video_info"]["variants"]); let downloadUrl = bestQuality(media[i]["video_info"]["variants"]);
if (needsFixing) { if (needsFixing(media[i])) {
downloadUrl = createStream({ downloadUrl = createStream({
service: "twitter", service: "twitter",
type: "remux", type: "remux",
@ -84,15 +94,13 @@ export default async function(obj) {
url: downloadUrl url: downloadUrl
}) })
} }
} else if (media.length === 1) {
single = bestQuality(media[0]["video_info"]["variants"])
} else { } else {
return { error: 'ErrorNoVideosInTweet' } single = bestQuality(media[0]["video_info"]["variants"])
} }
if (single) { if (single) {
return { return {
type: needsFixing? "remux" : "normal", type: needsFixing(media[0]) ? "remux" : "normal",
urls: single, urls: single,
filename: `twitter_${obj.id}.mp4`, filename: `twitter_${obj.id}.mp4`,
audioFilename: `twitter_${obj.id}_audio` audioFilename: `twitter_${obj.id}_audio`