You are not logged in.

#1 2021-10-15 12:05:39

froli
Member
From: Germany
Registered: 2008-06-17
Posts: 455

[SOLVED] Want to make a script that finds a youtube url to pass to mpv

Hello there!

I'm totally out of my comfort zone here and I don't know where to start. I don't know any programming language but I would like to learn enough to do basic tasks like what I'm trying to achieve here.

My situation:
I want to watch the game highlights of the latest game of my favorite hockey team without opening youtube in my web browser.

What I think the script needs to do:
Look into the NHL youtube account, in the Highlights playlist and get the url of the latest video that contains my team's name in the title. Then run mpv <url>

What it doesn't need to do:
I added this to my mpv.conf

script-opts=ytdl_hook-ytdl_path=yt-dlp

so it uses yt-dlp to pick the best quality available and everything by default.
It also doesn't need to store the video locally. I only want to watch it once, so streaming is the way to go.

Another possible approach:
I saw yt-dlp has --title and --date options so it might be easier to automate filling these options, although I also wouldn't know how to use that in conjunction with mpv. As I said, I want to stream the video, not download it locally.

Any pointers will be greatly appreciated!

Last edited by froli (2021-10-15 16:57:27)


archlinux on Macbook Pro 10,1

Offline

#2 2021-10-15 12:48:10

seth
Member
Registered: 2012-09-03
Posts: 59,158

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

You're aware of youtube-viewer?

Offline

#3 2021-10-15 15:00:33

froli
Member
From: Germany
Registered: 2008-06-17
Posts: 455

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

seth wrote:

You're aware of youtube-viewer?

I wasn't no, thanks for that. But it doesn't quite achieve what I want. I need to launch it from cli and then select my video from the search result. My ultimate goal would be to automate all that, create a .desktop file so that I can start it with rofi and the video starts playing right away in mpv.

Is there a way to make youtube-viewer automatically play the first search result? Cause that would solve it. I looked for the --autoplay option but it seems to be for autoplaying the next video.


archlinux on Macbook Pro 10,1

Offline

#4 2021-10-15 15:24:38

lmn
Member
Registered: 2021-05-09
Posts: 68

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

You could also use jq in conjuction with youtube-dl (yt-dlp in your case) to download and parse the desired playlist.
Something like this should work:

#!/bin/sh

highlights='https://www.youtube.com/watch?list=PL1NbHSfosBuHInmjsLcBuqeSV256FqlOO'

youtube-dl --dump-json --flat-playlist --playlist-end 50 "$highlights" | \
	jq -r 'select(.title | contains("Sharks")) .url' 

I took the liberty to guess the playlist on youtube and used Sharks as the search term.
The `--playlist-end=50` is arbitrary (to reduce the  number of requests and time) and should defniitly be changed, maybe you could leverage the `--date` flag from youtube-dl.

Edit: changed selector

Last edited by lmn (2021-10-15 15:30:11)

Offline

#5 2021-10-15 16:36:31

froli
Member
From: Germany
Registered: 2008-06-17
Posts: 455

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

lmn wrote:

You could also use jq in conjuction with youtube-dl (yt-dlp in your case) to download and parse the desired playlist.
Something like this should work:

#!/bin/sh

highlights='https://www.youtube.com/watch?list=PL1NbHSfosBuHInmjsLcBuqeSV256FqlOO'

youtube-dl --dump-json --flat-playlist --playlist-end 50 "$highlights" | \
	jq -r 'select(.title | contains("Sharks")) .url' 

I took the liberty to guess the playlist on youtube and used Sharks as the search term.
The `--playlist-end=50` is arbitrary (to reduce the  number of requests and time) and should defniitly be changed, maybe you could leverage the `--date` flag from youtube-dl.

Edit: changed selector

That helps a lot! Thank you.


archlinux on Macbook Pro 10,1

Offline

#6 2021-10-15 16:57:01

froli
Member
From: Germany
Registered: 2008-06-17
Posts: 455

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

I slightly modified Imn's suggestion to pick the first line of the output (which seems to always be the latest added to the playlist), set it as a variable and run it with mpv.

Here's the result

#!/bin/sh

highlights='https://www.youtube.com/watch?list=PL1NbHSfosBuHInmjsLcBuqeSV256FqlOO'

url=$(youtube-dl --dump-json --flat-playlist --playlist-end 50 "$highlights" | \
	jq -r 'select(.title | contains("Canadiens")) .url' | head -n 1)

exec mpv ytdl://$url

HUGE thanks for all the help!

Last edited by froli (2021-10-15 16:57:50)


archlinux on Macbook Pro 10,1

Offline

#7 2021-10-15 20:40:35

seth
Member
Registered: 2012-09-03
Posts: 59,158

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

Fwwi

youtube-viewer -A --skip-watched --prefer-mp4 --results=1 --within=7d --author=UCqFMzb-4AUf6WAIbl132QKA Flyers

(I know nothing about NHL, but they've the best Mascot ;-)

"-A" plays all found videos, you can ignore seen ones, control the format, the resolution etcetc.

Offline

#8 2021-10-15 21:20:30

latalante1
Member
Registered: 2018-08-30
Posts: 110

Re: [SOLVED] Want to make a script that finds a youtube url to pass to mpv

mpv is also doing well

mpv --ytdl-raw-options=match-filter="title~=(?i)sharks" --ytdl-raw-options-append=playlist-end=50 "https://www.youtube.com/watch?list=PL1NbHSfosBuHInmjsLcBuqeSV256FqlOO"

Offline

Board footer

Powered by FluxBB