youtube: change client to web and pass proper headers to stream (#454)

This commit is contained in:
wukko 2024-04-27 06:08:24 +06:00 committed by GitHub
commit ff93f7e42f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 36 deletions

View file

@ -40,6 +40,6 @@
"set-cookie-parser": "2.6.0", "set-cookie-parser": "2.6.0",
"undici": "^6.7.0", "undici": "^6.7.0",
"url-pattern": "1.0.3", "url-pattern": "1.0.3",
"youtubei.js": "^9.2.0" "youtubei.js": "^9.3.0"
} }
} }

View file

@ -33,7 +33,7 @@ export default async function(o) {
} }
try { try {
info = await yt.getBasicInfo(o.id, 'YTMUSIC_ANDROID'); info = await yt.getBasicInfo(o.id, 'WEB');
} catch (e) { } catch (e) {
return { error: 'ErrorCantConnectToServiceAPI' }; return { error: 'ErrorCantConnectToServiceAPI' };
} }
@ -43,7 +43,9 @@ export default async function(o) {
if (info.playability_status.status !== 'OK') return { error: 'ErrorYTUnavailable' }; if (info.playability_status.status !== 'OK') return { error: 'ErrorYTUnavailable' };
if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' }; if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' };
let bestQuality, hasAudio, adaptive_formats = info.streaming_data.adaptive_formats.filter(e => let bestQuality, hasAudio;
let adaptive_formats = info.streaming_data.adaptive_formats.filter(e =>
e.mime_type.includes(c[o.format].codec) || e.mime_type.includes(c[o.format].aCodec) e.mime_type.includes(c[o.format].codec) || e.mime_type.includes(c[o.format].aCodec)
).sort((a, b) => Number(b.bitrate) - Number(a.bitrate)); ).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
@ -96,7 +98,7 @@ export default async function(o) {
if (hasAudio && o.isAudioOnly) return { if (hasAudio && o.isAudioOnly) return {
type: "render", type: "render",
isAudioOnly: true, isAudioOnly: true,
urls: audio.url, urls: audio.decipher(yt.session.player),
filenameAttributes: filenameAttributes, filenameAttributes: filenameAttributes,
fileMetadata: fileMetadata fileMetadata: fileMetadata
} }
@ -108,14 +110,14 @@ export default async function(o) {
if (!o.isAudioOnly && !o.isAudioMuted && o.format === 'h264') { if (!o.isAudioOnly && !o.isAudioMuted && o.format === 'h264') {
match = info.streaming_data.formats.find(checkSingle); match = info.streaming_data.formats.find(checkSingle);
type = "bridge"; type = "bridge";
urls = match?.url; urls = match?.decipher(yt.session.player);
} }
const video = adaptive_formats.find(checkRender); const video = adaptive_formats.find(checkRender);
if (!match && video) { if (!match && video) {
match = video; match = video;
type = "render"; type = "render";
urls = [video.url, audio.url]; urls = [video.decipher(yt.session.player), audio.decipher(yt.session.player)];
} }
if (match) { if (match) {

View file

@ -5,6 +5,30 @@ import { metadataManager } from "../sub/utils.js";
import { request } from "undici"; import { request } from "undici";
import { create as contentDisposition } from "content-disposition-header"; import { create as contentDisposition } from "content-disposition-header";
const defaultHeaders = {
'user-agent': genericUserAgent
}
const serviceHeaders = {
bilibili: {
referer: 'https://www.bilibili.com/'
},
youtube: {
accept: '*/*',
origin: 'https://www.youtube.com',
referer: 'https://www.youtube.com',
DNT: '?1'
}
}
function getHeaders(service) {
return { ...defaultHeaders, ...serviceHeaders[service] }
}
function toRawHeaders(headers) {
return Object.entries(headers)
.map(([key, value]) => `${key}: ${value}\r\n`)
.join('');
}
function closeRequest(controller) { function closeRequest(controller) {
try { controller.abort() } catch {} try { controller.abort() } catch {}
} }
@ -53,7 +77,7 @@ export async function streamDefault(streamInfo, res) {
res.setHeader('Content-disposition', contentDisposition(filename)); res.setHeader('Content-disposition', contentDisposition(filename));
const { body: stream, headers } = await request(streamInfo.urls, { const { body: stream, headers } = await request(streamInfo.urls, {
headers: { 'user-agent': genericUserAgent }, headers: getHeaders(streamInfo.service),
signal: abortController.signal, signal: abortController.signal,
maxRedirections: 16 maxRedirections: 16
}); });
@ -68,49 +92,43 @@ export async function streamDefault(streamInfo, res) {
} }
export async function streamLiveRender(streamInfo, res) { export async function streamLiveRender(streamInfo, res) {
let abortController = new AbortController(), process; let process, abortController = new AbortController();
const shutdown = () => ( const shutdown = () => (
closeRequest(abortController), closeRequest(abortController),
killProcess(process), killProcess(process),
closeResponse(res) closeResponse(res)
); );
const headers = getHeaders(streamInfo.service);
const rawHeaders = toRawHeaders(headers);
try { try {
if (streamInfo.urls.length !== 2) return shutdown(); if (streamInfo.urls.length !== 2) return shutdown();
const { body: audio } = await request(streamInfo.urls[1], { const { body: audio } = await request(streamInfo.urls[1], {
maxRedirections: 16, signal: abortController.signal, headers,
headers: { signal: abortController.signal,
'user-agent': genericUserAgent, maxRedirections: 16
referer: streamInfo.service === 'bilibili'
? 'https://www.bilibili.com/'
: undefined,
}
}); });
const format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; const format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1];
let args = [ let args = [
'-loglevel', '-8', '-loglevel', '-8',
'-user_agent', genericUserAgent '-headers', rawHeaders,
];
if (streamInfo.service === 'bilibili') {
args.push(
'-headers', 'Referer: https://www.bilibili.com/\r\n',
)
}
args.push(
'-i', streamInfo.urls[0], '-i', streamInfo.urls[0],
'-i', 'pipe:3', '-i', 'pipe:3',
'-map', '0:v', '-map', '0:v',
'-map', '1:a', '-map', '1:a',
); ]
args = args.concat(ffmpegArgs[format]); args = args.concat(ffmpegArgs[format]);
if (streamInfo.metadata) { if (streamInfo.metadata) {
args = args.concat(metadataManager(streamInfo.metadata)) args = args.concat(metadataManager(streamInfo.metadata))
} }
args.push('-f', format, 'pipe:4'); args.push('-f', format, 'pipe:4');
process = spawn(...getCommand(args), { process = spawn(...getCommand(args), {
@ -128,6 +146,7 @@ export async function streamLiveRender(streamInfo, res) {
audio.on('error', shutdown); audio.on('error', shutdown);
audioInput.on('error', shutdown); audioInput.on('error', shutdown);
audio.pipe(audioInput); audio.pipe(audioInput);
pipe(muxOutput, res, shutdown); pipe(muxOutput, res, shutdown);
@ -145,13 +164,11 @@ export function streamAudioOnly(streamInfo, res) {
try { try {
let args = [ let args = [
'-loglevel', '-8', '-loglevel', '-8',
'-user_agent', genericUserAgent '-headers', toRawHeaders(getHeaders(streamInfo.service)),
]; ]
if (streamInfo.service === "twitter") { if (streamInfo.service === "twitter") {
args.push('-seekable', '0'); args.push('-seekable', '0');
} else if (streamInfo.service === 'bilibili') {
args.push('-headers', 'Referer: https://www.bilibili.com/\r\n');
} }
args.push( args.push(
@ -162,12 +179,12 @@ export function streamAudioOnly(streamInfo, res) {
if (streamInfo.metadata) { if (streamInfo.metadata) {
args = args.concat(metadataManager(streamInfo.metadata)) args = args.concat(metadataManager(streamInfo.metadata))
} }
let arg = streamInfo.copy ? ffmpegArgs["copy"] : ffmpegArgs["audio"];
args = args.concat(arg);
args = args.concat(ffmpegArgs[streamInfo.copy ? 'copy' : 'audio']);
if (ffmpegArgs[streamInfo.audioFormat]) { if (ffmpegArgs[streamInfo.audioFormat]) {
args = args.concat(ffmpegArgs[streamInfo.audioFormat]) args = args.concat(ffmpegArgs[streamInfo.audioFormat])
} }
args.push('-f', streamInfo.audioFormat === "m4a" ? "ipod" : streamInfo.audioFormat, 'pipe:3'); args.push('-f', streamInfo.audioFormat === "m4a" ? "ipod" : streamInfo.audioFormat, 'pipe:3');
process = spawn(...getCommand(args), { process = spawn(...getCommand(args), {
@ -196,13 +213,12 @@ export function streamVideoOnly(streamInfo, res) {
try { try {
let args = [ let args = [
'-loglevel', '-8' '-loglevel', '-8',
'-headers', toRawHeaders(getHeaders(streamInfo.service)),
] ]
if (streamInfo.service === "twitter") { if (streamInfo.service === "twitter") {
args.push('-seekable', '0') args.push('-seekable', '0')
} else if (streamInfo.service === 'bilibili') {
args.push('-headers', 'Referer: https://www.bilibili.com/\r\n')
} }
args.push( args.push(
@ -222,6 +238,7 @@ export function streamVideoOnly(streamInfo, res) {
if (format === "mp4") { if (format === "mp4") {
args.push('-movflags', 'faststart+frag_keyframe+empty_moov') args.push('-movflags', 'faststart+frag_keyframe+empty_moov')
} }
args.push('-f', format, 'pipe:3'); args.push('-f', format, 'pipe:3');
process = spawn(...getCommand(args), { process = spawn(...getCommand(args), {
@ -254,10 +271,12 @@ export function convertToGif(streamInfo, res) {
let args = [ let args = [
'-loglevel', '-8' '-loglevel', '-8'
] ]
if (streamInfo.service === "twitter") { if (streamInfo.service === "twitter") {
args.push('-seekable', '0') args.push('-seekable', '0')
} }
args.push('-i', streamInfo.urls)
args.push('-i', streamInfo.urls);
args = args.concat(ffmpegArgs["gif"]); args = args.concat(ffmpegArgs["gif"]);
args.push('-f', "gif", 'pipe:3'); args.push('-f', "gif", 'pipe:3');