11

Is there a way to play a video and make it follow YouTube autoplay videos?

Can this be done with youtube-dl and/or mpv?

0

It is possible to stream with youtube-dl and mpv.

My script does exactly what you want. It is very simple...

  1. checks if its a YT link or not,
  2. check the params given
    • -d for download
    • -nv for no video, so you can listen a music video.
  3. with wget get the source of the YT link for example
  4. with some grepping and cutting get the video IDs of all the following recommended videos
  5. play them one by one
| improve this answer | |
  • 1
    not good. kills all other currently running instances of mpv. miffed >9000 – Digit Jan 18 '19 at 0:22
  • seems to work happy just comment out the kill line, then all other paused mpv instances dont need to die. #[[ $(pgrep "mpv") ]] && pkill "mpv" – Digit Feb 9 '19 at 9:58
0

I think your best bet with the least effort might be with the terminal program mps-youtube's related video functionality. Check it out here: https://github.com/mps-youtube/mps-youtube.

For example:

If you want to watch a bunch of Chris Cornell tribute videos and you have mps-youtube installed and launched, you can use the following command to set video playback to "true" (otherwise you will only get audio)

set show_video true

Then, find the video, or playlist your want to play:

For Videos:

.Chris Cornell Tribute

Then choose the video you want related videos for:

r 1

(where 1 is the video you want related videos for)

Then simply enter the range of the videos you would like played.

1-50

(will play all related videos, up to number 50.)

You may also need to set the default video player to mpv, as I think the program defaults to mPlayer. I realize this adds an entirely separate program into the mix, but it does utilize the tech's your asking about, while keeping you in the terminal.

| improve this answer | |
  • Do you know that this will work, or are you just guessing? Can you describe how to do what the OP wants to do? Please do not respond in comments; edit your answer to make it clearer and more complete. – G-Man Says 'Reinstate Monica' May 17 '17 at 21:24
0

If found a script for mpv that does this: https://github.com/cvzi/mpv-youtube-upnext

It always adds the autoplay video to mpv's playlist if you start a youtube video.

| improve this answer | |
0

simplified approach

you could use a small script:

#!/usr/bin/perl
use strict;
use warnings;

my $id = $ARGV[0] or die "youtube ID as param needed"; 
$id =~ s/.*v=//;
my $yt = "https://www.youtube.com/watch?v="; 
while(1){
    `mpv $yt$id`; 
    ($id) = `wget -q -O- "$yt$id"` =~ /"autoplay":\{.*?"videoId":"([^"]+)"/; 
    print "$id\n";
}

save this as autoplay.pl, call chmod 755 autoplay.pl, and then try e.g. ./autoplay.pl 5YTxsc3Cz24

or, directly in your shell:

perl -e 'my $id="5YTxsc3Cz24"; my $yt = "https://www.youtube.com/watch?v="; while(1){`mpv $yt$id`; ($id) = `wget -q -O- "$yt$id"` =~ /"autoplay":\{.*?"videoId":"([^"]+)"/; print "$id\n";}'

you can end the playing by pressing ctrl+c (two times, one for mpv, one for the script).

avoid recurring IDs

the above code is a basic example and does not avoid any loops. if you want to avoid playing videos with the same ID twice, this can be achieved by adding a bucket:

#!/usr/bin/perl
use strict;
use warnings;

my $id = $ARGV[0] or die "youtube ID or youtube url as param needed"; 
$id =~ s/.*v=//;
my $yt = "https://www.youtube.com/watch?v="; 
my %bucket = ($id => 1);
while(defined $id){
    print "$id\n";
    `mpv $yt$id`; 
    my @ids = `wget -q -O- "$yt$id"` =~ /"videoId":"([^"]+)"/g; 
    undef $id;
    for my $i(@ids){
        unless($bucket{$i}){
            $id = $i;
            ++$bucket{$id};
        }
    }
}

this code searches the current youtube page for all IDs and skips those IDs already played. it ends, if no unplayed ID is found at the current page.

avoid recurring IDs harder

however, in some rare(?) cases you might have played all IDs of the current youtube page already. then the script should look up the pages of already seen IDs:

#!/usr/bin/perl
use strict;
use warnings;

my $id = $ARGV[0] or die "youtube ID or youtube url as param needed"; 
$id =~ s/.*v=//;
my $yt = "https://www.youtube.com/watch?v="; 
my %bucket = ($id => 1);

sub get_new_videoID{
    my $id = shift;
    my @ids = `wget -q -O- "$yt$id"` =~ /"videoId":"([^"]+)"/g; 
    my $new_id;
    for my $i(@ids){
        unless($bucket{$i}){
            ++$bucket{$i};
            $new_id = $i;
            last;
        }
    }
    return $new_id;
}

sub search_seen_videos{
    my @seen_IDs = keys %bucket;
    my $new_id;
    for my $id(@seen_IDs){
        $new_id = get_new_videoID($id);
        last if defined $new_id;
    }
    return $new_id;
}

while(defined $id){
    print "$id\n";
    `mpv $yt$id`; 
    $id = get_new_videoID($id) // search_seen_videos();
}
print "you have watched all related videos. better do something else now.\n";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.