0

So, I'm writing a basic python script to use youtube-dl to download a highquality thumbnail from a video. With the command line youtube-dl, you can run "youtube-dl --list-thumbnails [LINK]" and it will output a list of different quality links to the thumbnail images. Usually the highest resolution one has 'maxresdefault' in its link. I want to be able to download this image from the command line with wget. This is the code I have so far to achieve it. I'm not familiar with regex, but according to this site: regexr.com, it should have a match in the link with 'maxresdefault'.

import subprocess
import sys
import re
youtubeoutput = subprocess.call(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'], shell=True, stdout=subprocess.PIPE)
print(str(youtubeoutput))
imgurl = re.search("/maxresdefault/g", str(youtubeoutput)).group(0)
print(imgurl)
subprocess.run('wget', str(imgurl))

I put the print statements in there to see what the outputs were. When I run the code, I can see the youtube-dl doesn't recognize a link being in there. youtube-dl: error: You must provide at least one url. Since there's no links in the output, the re.search becomes a NoneType and it gives me an error. I don't know why youtube-dl won't recognize the link. I'm not even sure it recognizes the --list-thumnails. Could anyone help?

ZackT
  • 61
  • 4

1 Answers1

2

You've asked subprocess to use a shell (shell=True), so you would usually pass an entire command to call, like so:

youtubeoutput = subprocess.call("youtube-dl --list-thumbnails https://www.youtube.com/watch?v=t2U2mUtTnzY", shell=True, stdout=subprocess.PIPE)

But really, you may not need a shell. Try something like:

youtubeoutput = subprocess.check_output(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'])

Note that call does not actually return the program's standard output; check_output does.

Reference

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Thank you for this! I can see the links in the output. However, my regex search still is NoneType, do you know if my regex works? Still can't wrap my head around regex. If not, thanks for the help – ZackT Apr 28 '21 at 13:39
  • 1
    Your regex looks like it would match a literal slash, then the word maxresdefault, then another slash and a literal g. That's probably not what you want. This similar question has a different regex: https://stackoverflow.com/a/39607541/182402 – Wander Nauta Apr 28 '21 at 13:47