android-youtubeExtractor icon indicating copy to clipboard operation
android-youtubeExtractor copied to clipboard

ytFiles.get(itag).getUrl() returns null

Open YogevUzan opened this issue 6 years ago • 21 comments

Hi, I just noticed there's an issue with extracting old youtube videos.

For example: I'm trying to download Timbaland - Apologize (2009) and app crashes,

java.lang.nullpointerexception: Attempt to invoke virtual method 'java.lang.String at.huber.youtubeExtractor.YtFile.getUrl()' on a null object reference

YogevUzan avatar Mar 08 '18 21:03 YogevUzan

Yes, the age gate ... That is something that the library can’t detected I will take a look into it tomorrow Can you provide a url ?

hannojg avatar Mar 08 '18 21:03 hannojg

https://www.youtube.com/watch?v=ZSM3w1v-A_Y

YogevUzan avatar Mar 08 '18 21:03 YogevUzan

Any progress here? In my case onUrisAvailable callback returns ytFiles==null. But earlier it was working fine. Maybe youtube change smth?

Skullper avatar Mar 15 '18 17:03 Skullper

Asking again... Any progress here??

YogevUzan avatar Apr 03 '18 17:04 YogevUzan

@YogevUzan I have started to using this library: https://github.com/Commit451/YouTubeExtractor

Skullper avatar Apr 03 '18 18:04 Skullper

@Skullper Thanks! I'll start using it too...

  • Do you know if there's a way to get video information ?

YogevUzan avatar Apr 11 '18 14:04 YogevUzan

Maybe this can be useful to somebody. One of the ways for me to fix it was to go over multiple itags (resolutions). As for the video mentioned above, the highest resolution is 480 :D

        @Override
        protected void onExtractionComplete(SparseArray<YtFile> files, VideoMeta videoMeta) {
            if (files != null) {

                // 720, 1080, 480
                List<Integer> iTags = Arrays.asList(22, 137, 18);

                for (Integer iTag : iTags) {

                    YtFile ytFile = files.get(iTag);

                    if (ytFile != null) {

                        String downloadUrl = ytFile.getUrl();

                        if (downloadUrl != null && !downloadUrl.isEmpty()) {

                            Uri youtubeUri = Uri.parse(downloadUrl);

                            mSingleSubscriber.onSuccess(youtubeUri);
                            return;

                        }

                    }

                }

            }

            onExtractionFailed();
        }

ghost avatar May 08 '18 07:05 ghost

@jeroenstevens thank you man, your solution is working well.

sensoft2207 avatar Oct 18 '18 07:10 sensoft2207

@jeroenstevens itags 137 no sound comes out are there another index beside 22, 18, 137, 17?

masday86 avatar Dec 21 '18 03:12 masday86

Try this while calling


                YTDownload(18);
            

                YTDownload(22);
      

My function:

public void YTDownload(final int itag){
        String VideoURLDownload = "https://www.youtube.com/watch?v=5izFFbdHnWY";
        @SuppressLint("StaticFieldLeak") YouTubeUriExtractor youTubeUriExtractor = new YouTubeUriExtractor(this) {
            @Override
            public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
                if (ytFiles != null) {
                    String downloadURL = ytFiles.get(itag).getUrl();
                    Log.e("Download URL: ", downloadURL);
                    DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                    request.setTitle(videoTitle);
                    request.setDestinationInExternalPublicDir("/Downloads/YouTube-Downloader/", videoTitle + ".mp4");
                    if (downloadManager != null) {
                        downloadManager.enqueue(request);
                    }
                }
            }
        };
        youTubeUriExtractor.execute(VideoURLDownload);
    }

VishnuSivadasVS avatar May 03 '19 18:05 VishnuSivadasVS

hi , youtube video url are played , but if video is live at current time then getting error

imrankhanluhar avatar Sep 10 '19 11:09 imrankhanluhar

Maybe this can be useful to somebody. One of the ways for me to fix it was to go over multiple itags (resolutions). As for the video mentioned above, the highest resolution is 480 :D

        @Override
        protected void onExtractionComplete(SparseArray<YtFile> files, VideoMeta videoMeta) {
            if (files != null) {

                // 720, 1080, 480
                List<Integer> iTags = Arrays.asList(22, 137, 18);

                for (Integer iTag : iTags) {

                    YtFile ytFile = files.get(iTag);

                    if (ytFile != null) {

                        String downloadUrl = ytFile.getUrl();

                        if (downloadUrl != null && !downloadUrl.isEmpty()) {

                            Uri youtubeUri = Uri.parse(downloadUrl);

                            mSingleSubscriber.onSuccess(youtubeUri);
                            return;

                        }

                    }

                }

            }

            onExtractionFailed();
        }

can you please share the complete code here

imrankhanluhar avatar Sep 10 '19 11:09 imrankhanluhar

### for me, "int itag = 18;" worked

String youtubeLink = ("https://www.youtube.com/watch?v=" + youtubeDataModel.getVideo_id()); YouTubeUriExtractor ytEx = new YouTubeUriExtractor(getApplicationContext()) { @Override public void onUrisAvailable(String videoID, String videoTitle, SparseArray<YtFile> ytFiles) { if (ytFiles != null) { int itag = 18; //This is the download URL String downloadURL = ytFiles.get(itag).getUrl(); Log.e("download URL :", downloadURL);

                //now download it like a file
                new RequestDownloadVideoStream().execute(downloadURL, videoTitle);


            }

        }
    };

    ytEx.execute(youtubeLink);

``

birdeveloper avatar Oct 11 '19 15:10 birdeveloper

The tag 18 and 22 is working perfectly. Though there are some other problems with the URL formats too. But for now use 18 (480p) and 22 (720p). 137 (1080) is not having audio. I have mentioned the codes above.

VishnuSivadasVS avatar Oct 11 '19 16:10 VishnuSivadasVS

The tag 18 and 22 is working perfectly. Though there are some other problems with the URL formats too. But for now use 18 (480p) and 22 (720p). 137 (1080) is to having audio. I have mentioned the codes above.

roger that, boss. you are the king :))

birdeveloper avatar Oct 14 '19 18:10 birdeveloper

for me tag=18 worked

noushadullal avatar Oct 28 '20 21:10 noushadullal

All available formats (for an example video) :

171 webm audio only DASH audio 115k , audio@128k (44100Hz), 2.59MiB (worst) 140 m4a audio only DASH audio 129k , audio@128k (44100Hz), 3.02MiB 141 m4a audio only DASH audio 255k , audio@256k (44100Hz), 5.99MiB 160 mp4 256x144 DASH video 111k , 12fps, video only, 2.56MiB 247 webm 1280x720 DASH video 1807k , 1fps, video only, 23.48MiB 136 mp4 1280x720 DASH video 2236k , 24fps, video only, 27.73MiB 248 webm 1920x1080 DASH video 3993k , 1fps, video only, 42.04MiB 137 mp4 1920x1080 DASH video 4141k , 24fps, video only, 60.28MiB 43 webm 640x360 18 mp4 640x360 22 mp4 1280x720 (best)

partho5 avatar Dec 16 '20 04:12 partho5

is there any way we can download 137(1080) video with audio?

Aairaa avatar Jan 26 '21 11:01 Aairaa

Is there any way to get audio also

ghost avatar Mar 02 '21 15:03 ghost

hi , youtube video url are played , but if video is live at current time then getting error

Do you get the solution?

lifeofdekisugi avatar Jul 08 '21 22:07 lifeofdekisugi

Maybe this can be useful to somebody. One of the ways for me to fix it was to go over multiple itags (resolutions). As for the video mentioned above, the highest resolution is 480 :D

        @Override
        protected void onExtractionComplete(SparseArray<YtFile> files, VideoMeta videoMeta) {
            if (files != null) {

                // 720, 1080, 480
                List<Integer> iTags = Arrays.asList(22, 137, 18);

                for (Integer iTag : iTags) {

                    YtFile ytFile = files.get(iTag);

                    if (ytFile != null) {

                        String downloadUrl = ytFile.getUrl();

                        if (downloadUrl != null && !downloadUrl.isEmpty()) {

                            Uri youtubeUri = Uri.parse(downloadUrl);

                            mSingleSubscriber.onSuccess(youtubeUri);
                            return;

                        }

                    }

                }

            }

            onExtractionFailed();
        }

can you please share the complete code here

Well this download multiple files if that contains all the format

Dkathayat avatar Jul 16 '22 06:07 Dkathayat