Skip to content

Commit

Permalink
fix youtube audio not loading and potentially improve compatibility w…
Browse files Browse the repository at this point in the history
…ith ios devices
  • Loading branch information
eyezahhhh committed Jul 12, 2024
1 parent 603eefb commit e4d7f1d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"spotify-web-api-node": "^5.0.2",
"youtube-music-api": "github:Pipe-Bomb/Youtube-Music-API",
"yt-search": "^2.10.4",
"ytdl-core": "github:eyezahhhh/node-ytdl-core",
"ytdl-core": "^4.11.5",
"ytsr": "^3.8.4"
}
}
62 changes: 52 additions & 10 deletions src/service/YoutubeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Exception from "../response/Exception.js";
import StreamingService, { SearchOptions, UrlType } from "./StreamingService.js";
import APIResponse from "../response/APIResponse.js";
import StreamInfo from "./StreamInfo.js";
import Axios from "axios";
import Axios, { AxiosError } from "axios";
import { getHttpAgent, removeDuplicates, removeItems } from "../Utils.js";
import ExternalCollection from "../collection/ExternalCollection.js";

Expand All @@ -18,9 +18,10 @@ export default class YoutubeService extends StreamingService {
search: true
});

YTDL.setIpBindCallback(() => {
return getHttpAgent().httpsAgent as any;
});
// // @ts-expect-error
// YTDL.setIpBindCallback(() => {
// return getHttpAgent().httpsAgent as any;
// });
}

public async convertUrl(url: string): Promise<UrlType> {
Expand Down Expand Up @@ -68,36 +69,77 @@ export default class YoutubeService extends StreamingService {
}
}

private async getFormats(trackId: string): Promise<YTDL.videoFormat[]> {
const apiKey = 'AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc'
const headers = {
'X-YouTube-Client-Name': '5',
'X-YouTube-Client-Version': '19.09.3',
Origin: 'https://www.youtube.com',
'User-Agent': 'com.google.ios.youtube/19.09.3 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)',
'content-type': 'application/json'
}

const body = {
context: {
client: {
clientName: 'IOS',
clientVersion: '19.09.3',
deviceModel: 'iPhone14,3',
userAgent: 'com.google.ios.youtube/19.09.3 (iPhone14,3; U; CPU iOS 15_6 like Mac OS X)',
hl: 'en',
timeZone: 'UTC',
utcOffsetMinutes: 0
}
},
videoId: trackId,
playbackContext: { contentPlaybackContext: { html5Preference: 'HTML5_PREF_WANTS' } },
contentCheckOk: true,
racyCheckOk: true
};

const response = await Axios.post(`https://www.youtube.com/youtubei/v1/player?key${apiKey}&prettyPrint=false`, body, { headers });

const formats = response.data.streamingData.adaptiveFormats;
return formats;
}

private forceGetAudio(trackID: string): Promise<StreamInfo> {
trackID = this.convertTrackIDToLocal(trackID);

return new Promise<StreamInfo>(async (resolve, reject) => {
try {
let info: YTDL.videoInfo;
let formats: YTDL.videoFormat[];

try {
info = await YTDL.getInfo(trackID);
if (!info) throw "invalid";
formats = await this.getFormats(trackID);
if (!formats?.length) throw "invalid";
} catch (e) {
console.log(e);
return reject(new APIResponse(400, `Invalid track ID '${trackID}'`));
}
const audioFormats = YTDL.filterFormats(info.formats, "audioonly");

const audioFormats = formats.filter(format => format.mimeType.startsWith("audio/mp4"));

audioFormats.sort((a, b) => {
return b.audioBitrate - a.audioBitrate
});

async function checkFormat(url: string): Promise<StreamInfo> {
try {
console.log(getHttpAgent());

const { headers, status } = await Axios.head(url, {
...getHttpAgent()
});
if (status == 200) {
return new StreamInfo(url, headers["content-type"], headers["content-length"]);
}
} catch (e) {
console.error(e?.cause);
if (e instanceof AxiosError) {
console.error(e.response?.status, e.response.statusText, e.response.data);
} else {
console.error(e);
}
}
return null;
}
Expand Down

0 comments on commit e4d7f1d

Please sign in to comment.