The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Brief Introduction: I want to talk about the YouTube online player for Android with local playlists, channels, and recommendations.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Expanded Introduction:
Some time ago, I faced exactly the same issue as the author of this wonderful app did. Channel Whitelist, and I had exactly the same attitude towards it: I want to be able to sometimes give my child a tablet or smartphone with cartoons, but I am completely dissatisfied with where the recommended list in standard YouTube client apps takes the child after 2-3 clicks.

Unfortunately, after installing the Channel Whitelist app, I discovered another more mundane but still fatal drawback — NIH seemed not very convenient to me (and, importantly, to my son), especially after getting used to the YouTube Kids player interface.

In general, after a while, I was ready to create my own implementation. After some time, it became possible to tag the first release.

Main Features:

  • Add your favorite channels and playlists — they will be saved and indexed in the local database.
  • Within added playlists, disable unnecessary videos if you do not need them.
  • The recommendations list is generated randomly only from the channels and playlists added to the app.

The source code is open, licensed under GPLv3: https://github.com/sadr0b0t/yashlang/

Next, an overview of the main features in more detail, plus some technical details on how to play videos from YouTube in your Android app without using Google APIs and web wrappers.

On the main screen and on the player screen: random recommendations from non-random channels.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Instant search in the local database.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) => The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Add a new channel or playlist.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Search by name online or paste a known address. The list of videos from the channel or playlist is saved in the local database; icons are not cached.

Dynamic playlist — play search results.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) => The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

In the recommendations under the video, there will only be videos that satisfy the search query.

Similarly, if you open a video from the playlist settings, the recommendations list will only contain videos from that same playlist.

Playlists and channels can be temporarily disabled and re-enabled.

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Note that videos from a disabled playlist will also disappear from search results, watch history, and favorites. However, don't worry, they will reappear there as soon as the playlist is turned back on.

Add video to the blacklist

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

A blocked video will not appear in recommendations, search results, will disappear from favorites and watch history. The video will still be visible in the playlist settings.

View the blacklist and re-enable items blocked by mistake:
Settings > menu in the header > Blacklist

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Favorites and watch history

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Favorite videos on the player screen are marked with a star in the upper right corner.

Context menus in the screen header and on long click in galleries and lists

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

Copy the name or address of the video or playlist in the viewing screen or in any list.

Quick start — add recommended channels and playlists

The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?) => The Death of Koschei in the Recommendations List (Can You Make YouTube Cozy and Safe?)

The app will immediately look like it does in the screenshots above.

Unwanted channels and playlists can be turned off or deleted in the settings.

Installation

Project page: https://github.com/sadr0b0t/yashlang/
in English: https://github.com/sadr0b0t/yashlang/blob/master/README.en.md
releases: https://github.com/sadr0b0t/yashlang/releases

Keep in mind that switching between different versions from different sources on one device won't work due to different apk signatures; you'll need to uninstall the installed version along with the data — the cache of playlists and watch history (or figure out how to transfer that data) before installing a version from a new source.

Technical details

Does not require a Google/YouTube account, only the internet is needed, uses libraries:

Open source, free GPLv3 license.

question: Is it legal to scrape websites without permission (or with an explicit ban) from the authors? Google removes applications from Google Play, which do not use their API and scrape their websites, as they violate their user agreement.
Answer: of course, it is legal; it's up to you which tool to use for reading publicly available information. Moreover: The U.S. court has fully legalized website scraping and prohibited it from technically obstructing., but Google may have a different opinion; personally, I currently have no desire to go to an American court to convince them otherwise.

A bit of code

Library NewPipeExtractor — a supporting project of the player NewPipe, allows you to download a list of videos for a specified channel or playlist, download detailed information about a known video (what is visible on the webpage of the video), get the video thumbnail URL, and also get the video stream URL.

The code to download the playlist is a bit bulky, so I won't include it here; for those interested, check the source files, it mainly involves the class ContentLoader.

Let's see how to get the video stream URL from the public video page URL and play it in the player.

Include the library in
app/build.gradle

dependencies {
...
    // NewPipe: youtube parser
    // https://github.com/TeamNewPipe/NewPipeExtractor
    implementation "com.github.TeamNewPipe:NewPipeExtractor:v0.17.4"
...
}

Interestingly, even after this, it still won't be usable, as the examples will complain about the missing Downloader class. You can copy it into the project from the automated tests directory. NewPipeExtractor/extractor/src/test/java/org/schabi/newpipe/Downloader.java — works for version 0.17.4 (it seems that in a newer version of the library this part has been restructured, but it still needs to be verified).

Get the video stream URL from the video page address on the YouTube site:

app/src/main/java/su/sadrobot/yashlang/controller/ContentLoader.java

    public String extractYtStreamUrl(final String ytVidUrl) throws ExtractionException, IOException {
        // https://github.com/TeamNewPipe/NewPipeExtractor/blob/dev/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java
        NewPipe.init(Downloader.getInstance(), new Localization("GB", "en"));
        final YoutubeStreamExtractor extractor = (YoutubeStreamExtractor) YouTube
                .getStreamExtractor(ytVidUrl);
        extractor.fetchPage();
        final String streamUrl = extractor.getVideoStreams().size() > 0 ? extractor.getVideoStreams().get(0).getUrl() : null;
//        for (final VideoStream stream : extractor.getVideoStreams()) {
//            stream.getUrl();
//        }
        return streamUrl;
    }

For the video address ytVidUrl, it can be the public address of the page of any video on the YouTube site, for example https://www.youtube.com/watch?v=pd2RlatmNRk

The player will ExoPlayer from Google itself. This is not just a web wrapper for YouTube; it is a fully-fledged embedded player capable of playing any video content. It is quite flexible and customizable. In particular, it can stream video content from YouTube if given the correct address. We just obtained the stream address, so let's see how to send it to the player.

Connect the library to the project app/build.gradle:

dependencies {
...
    // google Exoplayer
    // https://github.com/google/ExoPlayer
    // https://exoplayer.dev/
    implementation 'com.google.android.exoplayer:exoplayer:2.10.8'
...
}

We will not cover all the nuances of placing the player component on the application screen (you can refer to the examples on the project website or in the code), but we will only look at how to start video playback from YouTube in the player using the address we obtained above:

app/src/main/java/su/sadrobot/yashlang/WatchVideoActivity.java

private void playVideoStream(final String streamUrl, final long seekTo) {
    if (streamUrl == null) {
        // stop playing the current video if it has been loaded
        videoPlayerView.getPlayer().stop(true);
    } else {
        // https://exoplayer.dev/
        // https://github.com/google/ExoPlayer

        final Uri mp4VideoUri = Uri.parse(streamUrl);
        final MediaSource videoSource = new ProgressiveMediaSource.Factory(videoDataSourceFactory)
                .createMediaSource(mp4VideoUri);

        // Pause the old video while preparing the new one
        if (videoPlayerView.getPlayer().getPlaybackState() != Player.STATE_ENDED) {
            // If pausing here after the player has paused itself upon finishing the video,
            // we will get a second STATE_ENDED event here, so we need a special check.
            // The value of getPlayWhenReady() will remain true, so we are checking the state.
            // https://github.com/google/ExoPlayer/issues/2272
            videoPlayerView.getPlayer().setPlayWhenReady(false);
        }

        // Prepare the player with the source.
        ((SimpleExoPlayer) videoPlayerView.getPlayer()).prepare(videoSource);

        // Set the current position immediately upon loading the video
        // (in comments, there's mention of data sources that support or do not support
        // seeking during loading; it seems like this is nonsense - just seek right after loading)
        // Exoplayer plays new Playlist from the beginning instead of provided position
        // https://github.com/google/ExoPlayer/issues/4375
        // How to load stream in the desired position? #2197
        // https://github.com/google/ExoPlayer/issues/2197
        // at this point, normal duration is not available yet, so we don’t check it
        // if(seekTo > 0 && seekTo  0) {
            // 5 seconds earlier
            videoPlayerView.getPlayer().seekTo(seekTo - 5000 > 0 ? seekTo - 5000 : 0);
        }
        videoPlayerView.getPlayer().setPlayWhenReady(true);
    }
}

Known Issues

  • Will not play videos with age restrictions that require a Google/YouTube account login

for example: Ilya Muromets, Mosfilm Studio, Ruslan and Lyudmila Episode 1 / Ruslan and Lyudmila film 1, Mosfilm Studio

Tip: Add such videos to a blacklist or ask the video owner to remove the mistakenly set restriction.

  • Will not play some live-stream videos for which the service returns a zero length (for such videos, the duration in lists and the gallery is noted as "[dur undef]")

for example: Nu, Pogodi! All Episodes Soyuzmultfilm HD (Cartoons for Children), Soyuzmultfilm Studio Cartoons, Top Cartoons Soyuzmultfilm, Cartoons from Soyuzmultfilm Studio

Tip: Add such videos to the blacklist.

  • Videos available only via direct links may not appear in the local playlist, even if you upload all of the user's videos.

for example: Taming Fire Episode 1, Movie Studio 'Mosfilm'

  • If you encounter a public video that does not require a login, plays in the browser but does not play in the player, please send a bug report (it is quite possible that the problem has already been fixed in a new version, NewPipeExtractor and you will only need to update the build with this version, for example).

  • The interface may lag on a slow (but not turned off) internet connection.

As a result,

The son switched from a tablet to a Samsung smart TV that cannot run Android applications. Therefore, the best parental control is still personal supervision.

But the app turned out to be convenient enough for me to start using it myself. My first impression from the early working versions was that I stepped into another world. All content is loaded from YouTube, but this is no longer YouTube; it is something else—safe and controlled, as if I had removed a centipede from my eye and placed it in a glass jar. And the key issue is indeed the recommendations.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster