/**
 * @file Music handler for YouTube videos. Does not support livestreams.
 * @author Ovyerus
 */

const ytdl = require('ytdl-core');
const got = require('got');

const ITAG = '251'; // Preferred iTag quality to get. Default: 251.

class YouTubeHandler {
    constructor() {}

    async getInfo(url) {
        if (typeof url !== 'string') throw new TypeError('url is not a string.');

        let info = await ytdl.getInfo(url);
        let res = {
            url,
            title: info.title,
            uploader: info.author.name,
            thumbnail: info.thumbnail_url.replace('default.jpg', 'hqdefault.jpg'),
            length: Number(info.length_seconds),
            type: 'YouTubeVideo'
        };

        return res;
    }

    async getStream(url) {
        if (typeof url !== 'string') throw new TypeError('url is not a string.');
            
        let info = await ytdl.getInfo(url);
        return got.stream(info.formats.find(f => f.itag === ITAG).url);
    }
}

module.exports = YouTubeHandler;
/**
 * @file Music handler for YouTube videos. Does not support livestreams.
 * @author Ovyerus
 */

const ytdl = require('ytdl-core');
const got = require('got');

// List of all itag qualities can be found here: https://en.wikipedia.org/w/index.php?title=YouTube&oldid=800910021#Quality_and_formats.
const ITAG = '140'; // Preferred itag quality to get. Default: 140.
const ITAG_FALLBACK = '22'; // In the event that the previous itag could not be found, try finding this one. Should probably be a lower value.

class YouTubeHandler {
    constructor() {}

    async getInfo(url) {
        if (typeof url !== 'string') throw new TypeError('url is not a string.');

        let info = await ytdl.getInfo(url);
        let res = {
            url,
            title: info.title,
            uploader: info.author.name,
            thumbnail: info.thumbnail_url.replace('default.jpg', 'hqdefault.jpg'),
            length: Number(info.length_seconds),
            type: 'YouTubeVideo'
        };

        return res;
    }

    async getStream(url) {
        if (typeof url !== 'string') throw new TypeError('url is not a string.');
            
        let info = await ytdl.getInfo(url);
        let format = info.formats.find(f => f.itag === ITAG) || info.formats.find(f => f.itag === ITAG_FALLBACK);
        format = format ? format.url : info.url; // Fallback to default URL if the wanted itags could not be found;

        return got.stream(format);
    }
}

module.exports = YouTubeHandler;
Change requested itag for YouTube music to a M4A+AAC stream, instead of a WebM+Opus stream in an attempt to fix issues
Change requested itag for YouTube music to a M4A+AAC stream, instead of a WebM+Opus stream in an attempt to fix issues
