Merge branch 'imputnet:current' into newgrounds-support

This commit is contained in:
hyperdefined 2024-07-26 18:07:42 -04:00 committed by GitHub
commit 2439be868f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 127 additions and 55 deletions

1
.github/FUNDING.yml vendored
View File

@ -1 +0,0 @@
custom: https://boosty.to/wukko/donate

View File

@ -49,9 +49,9 @@ item type: `object`
| key | type | variables | description | | key | type | variables | description |
|:--------|:---------|:--------------------------------------------------------|:---------------------------------------| |:--------|:---------|:--------------------------------------------------------|:---------------------------------------|
| `type` | `string` | `video` | used only if `pickerType`is `various`. | | `type` | `string` | `video / photo / gif` | used only if `pickerType` is `various` |
| `url` | `string` | direct link to a file or a link to cobalt's live render | | | `url` | `string` | direct link to a file or a link to cobalt's live render | |
| `thumb` | `string` | item thumbnail that's displayed in the picker | used only for `video` type. | | `thumb` | `string` | item thumbnail that's displayed in the picker | used for `video` and `gif` types |
## GET: `/api/stream` ## GET: `/api/stream`
cobalt's live render (or stream) endpoint. usually, you will receive a url to this endpoint cobalt's live render (or stream) endpoint. usually, you will receive a url to this endpoint

13
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "cobalt", "name": "cobalt",
"version": "7.14.6", "version": "7.15",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "cobalt", "name": "cobalt",
"version": "7.14.6", "version": "7.15",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"content-disposition-header": "0.6.0", "content-disposition-header": "0.6.0",
@ -24,7 +24,7 @@
"set-cookie-parser": "2.6.0", "set-cookie-parser": "2.6.0",
"undici": "^5.19.1", "undici": "^5.19.1",
"url-pattern": "1.0.3", "url-pattern": "1.0.3",
"youtubei.js": "^10.1.0" "youtubei.js": "^10.2.0"
}, },
"engines": { "engines": {
"node": ">=18" "node": ">=18"
@ -1123,12 +1123,13 @@
} }
}, },
"node_modules/youtubei.js": { "node_modules/youtubei.js": {
"version": "10.1.0", "version": "10.2.0",
"resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-10.1.0.tgz", "resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-10.2.0.tgz",
"integrity": "sha512-MokZMAnpWH11VYvWuW6qjPiiPmgRl5rfDgPQOpif9qXcVHoVw1hi8ePuRSD0AZSZ+uvWGe8rvas2dzp+Jv5JKQ==", "integrity": "sha512-JLKW9AHQ1qrTwBbre1aDkH8UJFmNcc4+kOSaVou5jSY7AzfFPFJK0yvX6afnLst0UVC9wfXHrLiNx93sutVErA==",
"funding": [ "funding": [
"https://github.com/sponsors/LuanRT" "https://github.com/sponsors/LuanRT"
], ],
"license": "MIT",
"dependencies": { "dependencies": {
"jintr": "^2.0.0", "jintr": "^2.0.0",
"tslib": "^2.5.0", "tslib": "^2.5.0",

View File

@ -1,7 +1,7 @@
{ {
"name": "cobalt", "name": "cobalt",
"description": "save what you love", "description": "save what you love",
"version": "7.14.6", "version": "7.15",
"author": "imput", "author": "imput",
"exports": "./src/cobalt.js", "exports": "./src/cobalt.js",
"type": "module", "type": "module",
@ -40,7 +40,7 @@
"set-cookie-parser": "2.6.0", "set-cookie-parser": "2.6.0",
"undici": "^5.19.1", "undici": "^5.19.1",
"url-pattern": "1.0.3", "url-pattern": "1.0.3",
"youtubei.js": "^10.1.0" "youtubei.js": "^10.2.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"freebind": "^0.2.2" "freebind": "^0.2.2"

View File

@ -147,7 +147,6 @@ export default async function({ id, index, toGif, dispatcher }) {
} }
let media = (repostedTweet?.media || baseTweet?.extended_entities?.media); let media = (repostedTweet?.media || baseTweet?.extended_entities?.media);
media = media?.filter(m => m.video_info?.variants?.length);
// check if there's a video at given index (/video/<index>) // check if there's a video at given index (/video/<index>)
if (index >= 0 && index < media?.length) { if (index >= 0 && index < media?.length) {
@ -159,18 +158,38 @@ export default async function({ id, index, toGif, dispatcher }) {
case 0: case 0:
return { error: 'ErrorNoVideosInTweet' }; return { error: 'ErrorNoVideosInTweet' };
case 1: case 1:
if (media[0].type === "photo") {
return {
type: "normal",
isPhoto: true,
urls: `${media[0].media_url_https}?name=4096x4096`
}
}
return { return {
type: needsFixing(media[0]) ? "remux" : "normal", type: needsFixing(media[0]) ? "remux" : "normal",
urls: bestQuality(media[0].video_info.variants), urls: bestQuality(media[0].video_info.variants),
filename: `twitter_${id}.mp4`, filename: `twitter_${id}.mp4`,
audioFilename: `twitter_${id}_audio`, audioFilename: `twitter_${id}_audio`,
isGif: media[0].type === "animated_gif" isGif: media[0].type === "animated_gif"
}; }
default: default:
const picker = media.map((content, i) => { const picker = media.map((content, i) => {
if (content.type === "photo") {
let url = `${content.media_url_https}?name=4096x4096`;
return {
type: "photo",
url,
thumb: url,
}
}
let url = bestQuality(content.video_info.variants); let url = bestQuality(content.video_info.variants);
const shouldRenderGif = content.type === 'animated_gif' && toGif; const shouldRenderGif = content.type === 'animated_gif' && toGif;
let type = "video";
if (shouldRenderGif) type = "gif";
if (needsFixing(content) || shouldRenderGif) { if (needsFixing(content) || shouldRenderGif) {
url = createStream({ url = createStream({
service: 'twitter', service: 'twitter',
@ -181,9 +200,9 @@ export default async function({ id, index, toGif, dispatcher }) {
} }
return { return {
type: 'video', type,
url, url,
thumb: content.media_url_https, thumb: content.media_url_https
} }
}); });
return { picker }; return { picker };

View File

@ -1,25 +1,27 @@
import { Innertube, Session } from 'youtubei.js'; import { fetch } from "undici";
import { env } from '../../config.js';
import { cleanString } from '../../sub/utils.js'; import { Innertube, Session } from "youtubei.js";
import { fetch } from 'undici'
import { getCookie, updateCookieValues } from '../cookie/manager.js' import { env } from "../../config.js";
import { cleanString } from "../../sub/utils.js";
import { getCookie, updateCookieValues } from "../cookie/manager.js";
const ytBase = Innertube.create().catch(e => e); const ytBase = Innertube.create().catch(e => e);
const codecMatch = { const codecMatch = {
h264: { h264: {
codec: "avc1", videoCodec: "avc1",
aCodec: "mp4a", audioCodec: "mp4a",
container: "mp4" container: "mp4"
}, },
av1: { av1: {
codec: "av01", videoCodec: "av01",
aCodec: "mp4a", audioCodec: "mp4a",
container: "mp4" container: "mp4"
}, },
vp9: { vp9: {
codec: "vp9", videoCodec: "vp9",
aCodec: "opus", audioCodec: "opus",
container: "webm" container: "webm"
} }
} }
@ -94,11 +96,16 @@ const cloneInnertube = async (customFetch) => {
export default async function(o) { export default async function(o) {
const yt = await cloneInnertube( const yt = await cloneInnertube(
(input, init) => fetch(input, { ...init, dispatcher: o.dispatcher }) (input, init) => fetch(input, {
...init,
dispatcher: o.dispatcher
})
); );
let info, isDubbed, format = o.format || "h264"; const quality = o.quality === "max" ? "9000" : o.quality;
let quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality
let info, isDubbed,
format = o.format || "h264";
function qual(i) { function qual(i) {
if (!i.quality_label) { if (!i.quality_label) {
@ -121,6 +128,7 @@ export default async function(o) {
if (!info) return { error: 'ErrorCantConnectToServiceAPI' }; if (!info) return { error: 'ErrorCantConnectToServiceAPI' };
const playability = info.playability_status; const playability = info.playability_status;
const basicInfo = info.basic_info;
if (playability.status === 'LOGIN_REQUIRED') { if (playability.status === 'LOGIN_REQUIRED') {
if (playability.reason.endsWith('bot')) { if (playability.reason.endsWith('bot')) {
@ -135,11 +143,11 @@ export default async function(o) {
} }
if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' }; if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' };
if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' }; if (basicInfo.is_live) return { error: 'ErrorLiveVideo' };
// return a critical error if returned video is "Video Not Available" // return a critical error if returned video is "Video Not Available"
// or a similar stub by youtube // or a similar stub by youtube
if (info.basic_info.id !== o.id) { if (basicInfo.id !== o.id) {
return { return {
error: 'ErrorCantConnectToServiceAPI', error: 'ErrorCantConnectToServiceAPI',
critical: true critical: true
@ -148,11 +156,16 @@ export default async function(o) {
let bestQuality, hasAudio; let bestQuality, hasAudio;
const filterByCodec = (formats) => formats.filter(e => const filterByCodec = (formats) =>
e.mime_type.includes(codecMatch[format].codec) || e.mime_type.includes(codecMatch[format].aCodec) formats
).sort((a, b) => Number(b.bitrate) - Number(a.bitrate)); .filter(e =>
e.mime_type.includes(codecMatch[format].videoCodec)
|| e.mime_type.includes(codecMatch[format].audioCodec)
)
.sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats); let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats);
if (adaptive_formats.length === 0 && format === "vp9") { if (adaptive_formats.length === 0 && format === "vp9") {
format = "h264" format = "h264"
adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats) adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats)
@ -162,27 +175,43 @@ export default async function(o) {
hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length); hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length);
if (bestQuality) bestQuality = qual(bestQuality); if (bestQuality) bestQuality = qual(bestQuality);
if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' };
if (info.basic_info.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
let checkBestAudio = (i) => (i.has_audio && !i.has_video), if ((!bestQuality && !o.isAudioOnly) || !hasAudio)
audio = adaptive_formats.find(i => checkBestAudio(i) && !i.is_dubbed); return { error: 'ErrorYTTryOtherCodec' };
if (basicInfo.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
const checkBestAudio = (i) => (i.has_audio && !i.has_video);
let audio = adaptive_formats.find(i =>
checkBestAudio(i) && i.is_original
);
if (o.dubLang) { if (o.dubLang) {
let dubbedAudio = adaptive_formats.find(i => let dubbedAudio = adaptive_formats.find(i =>
checkBestAudio(i) && i.language === o.dubLang && i.audio_track && !i.audio_track.audio_is_default checkBestAudio(i)
); && i.language === o.dubLang
&& i.audio_track
)
if (dubbedAudio) { if (dubbedAudio) {
audio = dubbedAudio; audio = dubbedAudio;
isDubbed = true isDubbed = true;
} }
} }
if (!audio) {
audio = adaptive_formats.find(i => checkBestAudio(i));
}
let fileMetadata = { let fileMetadata = {
title: cleanString(info.basic_info.title.trim()), title: cleanString(basicInfo.title.trim()),
artist: cleanString(info.basic_info.author.replace("- Topic", "").trim()), artist: cleanString(basicInfo.author.replace("- Topic", "").trim()),
} }
if (info.basic_info.short_description && info.basic_info.short_description.startsWith("Provided to YouTube by")) {
let descItems = info.basic_info.short_description.split("\n\n"); if (basicInfo?.short_description?.startsWith("Provided to YouTube by")) {
let descItems = basicInfo.short_description.split("\n\n");
fileMetadata.album = descItems[2]; fileMetadata.album = descItems[2];
fileMetadata.copyright = descItems[3]; fileMetadata.copyright = descItems[3];
if (descItems[4].startsWith("Released on:")) { if (descItems[4].startsWith("Released on:")) {
@ -198,19 +227,23 @@ export default async function(o) {
youtubeDubName: isDubbed ? o.dubLang : false youtubeDubName: isDubbed ? o.dubLang : false
} }
if (hasAudio && o.isAudioOnly) return { if (audio && o.isAudioOnly) return {
type: "render", type: "render",
isAudioOnly: true, isAudioOnly: true,
urls: audio.decipher(yt.session.player), urls: audio.decipher(yt.session.player),
filenameAttributes: filenameAttributes, filenameAttributes: filenameAttributes,
fileMetadata: fileMetadata, fileMetadata: fileMetadata,
bestAudio: format === "h264" ? 'm4a' : 'opus' bestAudio: format === "h264" ? "m4a" : "opus"
} }
const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality, const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].codec), checkSingle = i =>
checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio; qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].videoCodec),
checkRender = i =>
qual(i) === matchingQuality && i.has_video && !i.has_audio;
let match, type, urls; let match, type, urls;
if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') { if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') {
match = info.streaming_data.formats.find(checkSingle); match = info.streaming_data.formats.find(checkSingle);
type = "bridge"; type = "bridge";
@ -218,10 +251,14 @@ export default async function(o) {
} }
const video = adaptive_formats.find(checkRender); const video = adaptive_formats.find(checkRender);
if (!match && video) {
if (!match && video && audio) {
match = video; match = video;
type = "render"; type = "render";
urls = [video.decipher(yt.session.player), audio.decipher(yt.session.player)]; urls = [
video.decipher(yt.session.player),
audio.decipher(yt.session.player)
]
} }
if (match) { if (match) {

View File

@ -11,8 +11,7 @@
"code": 200, "code": 200,
"status": "redirect" "status": "redirect"
} }
}, }, {
{
"name": "video with mobile web mediaviewer", "name": "video with mobile web mediaviewer",
"url": "https://twitter.com/X/status/1697304622749086011/mediaViewer?currentTweet=1697304622749086011&currentTweetUser=X&currentTweet=1697304622749086011&currentTweetUser=X", "url": "https://twitter.com/X/status/1697304622749086011/mediaViewer?currentTweet=1697304622749086011&currentTweetUser=X&currentTweet=1697304622749086011&currentTweetUser=X",
"params": {}, "params": {},
@ -137,6 +136,22 @@
"code": 200, "code": 200,
"status": "redirect" "status": "redirect"
} }
}, {
"name": "post with 1 image",
"url": "https://x.com/PopCrave/status/1815960083475423235",
"params": {},
"expected": {
"code": 200,
"status": "redirect"
}
}, {
"name": "post with 4 images",
"url": "https://x.com/PopCrave/status/1816260887147114696",
"params": {},
"expected": {
"code": 200,
"status": "picker"
}
}, { }, {
"name": "retweeted video, isAudioOnly", "name": "retweeted video, isAudioOnly",
"url": "https://twitter.com/hugekiwinuts/status/1618671150829309953?s=46&t=gItGzgwGQQJJaJrO6qc1Pg", "url": "https://twitter.com/hugekiwinuts/status/1618671150829309953?s=46&t=gItGzgwGQQJJaJrO6qc1Pg",
@ -1065,6 +1080,7 @@
}, { }, {
"name": "yappy", "name": "yappy",
"url": "https://rutube.ru/yappy/c8c32bf7aee04412837656ea26c2b25b/", "url": "https://rutube.ru/yappy/c8c32bf7aee04412837656ea26c2b25b/",
"canFail": true,
"params": {}, "params": {},
"expected": { "expected": {
"code": 200, "code": 200,