Package Exports
- ytcog
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ytcog) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ytcog
YouTube innertube class library for node-js; session, player, searches, channels, videos and downloads.
Features
- Simple, efficient, fast and powerful.
- No Google developer key required.
- The innertube api is what the YouTube website itself uses to efficiently deliver search, channel and video information (json only).
- The downloader is a forked process allowing for concurrent non-blocking downloads.
Classes
- Session - manage your Youtube session/player - enables seemless search, channel, video and download requests.
- Player - used by the session object for deciphering, encoding and hashing - you would not normally need to use this directly.
- Search - fetch videos from specific search requests.
- Channel - fetch metadata and videos from specific channels.
- Video - create video objects directly or from search and channel scan results - fetch metadata and stream information deciphered/encoded as necessary - ensure reliable and fast downloads.
- Download - a convenience object for easy once-off, non-session, downloads.
See the wiki for greater detail.
Usage
Easy downloader
const ytcog = require('ytcog');
await ytcog.dl(downloadOptions);See the wiki for downloadOptions
Session
const ytcog = require('ytcog');
const session = new ytcog.Session([cookie, userAgent]);
await session.fetch();cookie is optional. With a cookie, everything will work. Without it, age-restricted video streams will not be retrieved and there might be some rate-limiting. userAgent is optional. Since ytcog emulates a browser session, you can make all requests use your browser's user agent.
In order to obtain your youtube cookie and user agent: Log onto YouTube in your browser. Goto settings > ... > developer tools. Refresh the page. Goto network>headers. Find the "www.youtube.com" entry. In the request headers you will find "cookie" and "user-agent". Pass these string values in your ytcog sessions.
A session object is required to create searches, channels and videos.
Video
const video = new ytcog.Video(session, videoOptions);
await video.fetch();
await video.download([downloadOptions]);See wiki for videoOptions.
Running this HEAD request and testing the status/reason is an inexpensive way to check if a video is still online:
await video.imageOnline();Cancels the current download:
video.cancel();Search
const search = new ytcog.Search(session, searchOptions);
await search.fetch();See the wiki for searchOptions.
Search again over a different period:
search.updateOptions({period:'year'});
await search.fetch();If available, you can get an additional (+-20) results with each successive continuation:
await search.continued(); Examine the results in an array of Video objects:
search.videos.forEach((video)=>{
// do something with the results, like collect and display their streams
await video.fetch();
console.log(video.info());
console.log(video.streamInfo);
});Channel
const channel = new ytcog.Channel(session, channelOptions);
await channel.fetch();See wiki for channelOptions.
Get channel metadata with
channel.updateOptions({order:'about'});
await channel.fetch();If available, you can get an additional (+-30) videos with each successive continuation
await channel.continued();Examine the results in an array of Video objects:
channel.videos.forEach((video)=>{...});Examples
Check the examples folder for more clarity on usage of Session, Search, Channel and Video classes.
Install
npm install ytcogRoadmap
There are some limitaions. ytcog does not currently handle playlists or download live videos.
Disclaimer
YouTube can and will change how their innertube api works at any time. So potential disruptions are likely in the future. I will try to evolve and adapt this library asap, but without gaurantees.
Acknowledgement to the following node-js projects on which ytcog depends:
- miniget (robust web requests)
- ffmpeg-static (muxing video & audio when necessary)
- sanitize-filename (as the name suggests)