Skip to content

Instantly share code, notes, and snippets.

@Patrick-web
Created July 23, 2021 15:53
Show Gist options
  • Save Patrick-web/7ce72be272d030a9c6eb942daee18df8 to your computer and use it in GitHub Desktop.
Save Patrick-web/7ce72be272d030a9c6eb942daee18df8 to your computer and use it in GitHub Desktop.
Download audio from a YouTube video
import axios from "axios";
interface AudioQualities {
"320kbs": string;
"256kbs": string;
"192kbs": string;
"128kbs": string;
}
interface Format {
size: string;
f: string;
q: string;
k: string;
}
interface Mp3 {
"auto": Format;
"320": Format;
"256": Format;
"192": Format;
"128": Format;
"96": Format;
"64": Format;
}
interface Links {
mp3: Mp3;
}
interface _9CloudResponse {
data: {
status: string;
mess: string;
p: string;
vid: string;
title: string;
t: number;
a: string;
links: Links;
};
}
export interface DownloadResponse {
data: {
status: string;
mess: string;
c_status: string;
vid: string;
title: string;
ftype: string;
fquality: string;
dlink: string;
}
}
function downloadYTVideo(ytLink: string, quality: string = "128kbs") {
let videoID: string = "";
const URLEncodedLink: string = encodeURI(ytLink);
const data: string = `query=${URLEncodedLink}&vt=mp3`;
const config: object = {
method: "post",
url: "https://9convert.com/api/ajaxSearch/index",
data: data,
};
axios(config)
.then(function (response: _9CloudResponse) {
const qualities: AudioQualities | any = {
"320kbs": response.data.links.mp3["320"].k,
"256kbs": response.data.links.mp3["256"].k,
"192kbs": response.data.links.mp3["192"].k,
"128kbs": response.data.links.mp3["128"].k,
};
videoID = response.data.vid;
getDownloadLink(videoID, qualities[quality]);
})
.catch(function (error: string) {
console.log(error);
});
}
function getDownloadLink(videoID: string, _9ConvertID: string) {
let data = `vid=${videoID}&k=0%2${_9ConvertID}%3D`;
data = data.replace("0+", "B").replace("=%", "%");
console.log(data);
const config: object = {
method: "post",
url: "https://9convert.com/api/ajaxConvert/convert",
data: data,
};
axios(config)
.then(function (response: DownloadResponse) {
console.log(response.data);
})
.catch(function (error: string) {
console.log(error);
});
}
downloadYTVideo("https://www.youtube.com/watch?v=AtQKdlPeV0A", "256kbs");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment