ybd-project / ytdl-core

Fast and secure YouTube downloader for JavaScript and TypeScript
MIT License
27 stars 5 forks source link

Using only single client does not seem to work. #40

Open Elite opened 2 days ago

Elite commented 2 days ago

Describe the bug

When using the Webpack version of the library and injecting it onto YouTube as a background script, the extraction works; however, instead of fetching only the iOS client (as I am using clients: ['ios']), it still retrieves data from all clients.

// Listen for tab updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    // Check if the URL is a YouTube video
    if (changeInfo.status === 'complete' && tab.url && tab.url.includes('youtube.com/watch')) {
        // Inject ytdl-core.js first
        chrome.tabs.executeScript(tabId, {
            file: 'ytdl-core.js'
        }, () => {
            // After ytdl-core is loaded, inject our main script
            chrome.tabs.executeScript(tabId, {
                code: `
          const ytdl = new window.Ytdl.YtdlCore({
            logDisplay: ['info', 'success', 'error', 'debug'],
            disableInitialSetup: false,
            disableBasicCache: true,
            disableFileCache: true,
            disablePoTokenAutoGeneration: true,
            noUpdate: true,
            clients: ['ios']
          });

          async function getVideoFormat() {
            try {
              console.log('Starting getVideoFormat...');
              let info = await ytdl.getFullInfo(window.location.href);
              console.log('Video info received:', info);

              // Get format 18 (360p MP4 with audio)
              console.log('Available formats:', info.formats);
              const format = info.formats.find(f => f.itag === 18);
              console.log('Selected format:', format);

              if (format) {
                const videoUrl = format.url;

                console.log('Format found:', {
                  itag: format.itag,
                  quality: format.quality,
                  container: format.container,
                  url: videoUrl
                });
                localStorage.setItem('downloadUrl', videoUrl);
                return format;
              } else {
                console.error('No format 18 found');
                return null;
              }
            } catch (error) {
              console.error('Error getting video format:', error);
              console.error('Error stack:', error.stack);
              return null;
            }
          }

          // Run the format extraction
          getVideoFormat().then(result => {
            console.log('getVideoFormat completed with result:', result);
          }).catch(error => {
            console.error('getVideoFormat failed:', error);
          });
        `
            });
        });
    }
});

Error Details (Log)

[  DEBUG  ]: To speed up processing, html5Player and signatureTimestamp are pre-fetched and cached.
3ytdl-core.js:1 [  DEBUG  ]: [ Request ]: GET -> https://www.youtube.com/iframe_api
2ytdl-core.js:1 [  DEBUG  ]: [ Request ]: GET -> https://www.youtube.com/s/player/5f315c3d/player_ias.vflset/en_US/base.js
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: GET -> https://www.youtube.com/s/player/5f315c3d/player_ias.vflset/en_US/base.js
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: POST -> https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: POST -> https://www.youtube.com/youtubei/v1/player?key=AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc&prettyPrint=false&id=JCDTpbpKC-g&t=rlP5-HjntZGg
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: POST -> https://www.youtube.com/youtubei/v1/player?prettyPrint=false
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: POST -> https://www.youtube.com/youtubei/v1/player?prettyPrint=false
ytdl-core.js:1 [  DEBUG  ]: [ Request ]: POST -> https://www.youtube.com/youtubei/v1/next?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false
ytdl-core.js:1 [  DEBUG  ]: [ Web ]: Success
ytdl-core.js:1 [  DEBUG  ]: [ MWeb ]: Success
ytdl-core.js:1 [  DEBUG  ]: [ Tv ]: Success
ytdl-core.js:1 [  DEBUG  ]: [ Ios ]: Success
ytdl-core.js:1 [  DEBUG  ]: [ Next ]: Success

Environment

ybd-project commented 2 days ago

To ensure stable operation, ios and web, mweb, and tv clients are specified by default; if you want to use only IOS clients, specify the option disableDefaultClients: true.

const ytdl = new YtdlCore({
    disableDefaultClients: true,
});
Elite commented 2 days ago

@ybd-project When you say stable operation does that mean, using only WEB client to get pre-merged 360p (itag=18) can fails under some circumstances.