TheOlDirtyBastard TheOlDirtyBastard - 1 month ago 13x
Python Question

How can I download just thumbnails using youtube-dl?

I've been trying to download the thumbnails of a list of URL's (youtube videos) I have.

I've been using youtube-dl and I've worked it out to this so far:

import os

with open('results.txt') as f:
for line in f:
os.system("youtube-dl " + "--write-thumbnail " + line)

Like this I'm able to download the thumbnails but I'm forced to downloading the youtube videos as well.

How can I just download the thumbnail?


It looks like passing --list-thumbnails will return the url to the thumbnail images, but it will just output to the screen when calling os.system().

The following isn't the prettiest, but it's a quick working example of getting the output of youtube-dl into a string using subprocess, parsing it to get the url, and downloading with requests:

import re
import requests
import subprocess

with open('results.txt') as f:
    for line in f:
        proc = subprocess.Popen(['youtube-dl', '--list-thumbnails', line], stdout=subprocess.PIPE)
        youtubedl_output, err = proc.communicate()
        imgurl ="(?P<url>https?://[^\s]+)", youtubedl_output).group('url')
        r = requests.get(imgurl)
        if r.status_code == 200:
            with open(imgurl.split('/')[4] + '.jpg', 'wb') as file:
                for chunk in r.iter_content(1024):

Hope this helped!