Reputation: 247
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?
Upvotes: 18
Views: 51471
Reputation: 1452
Python 3.6+
import os
with open('urls.txt') as urls:
for url in urls:
os.system(f"youtube-dl --write-thumbnail --skip-download {url}")
Python 2.7 (deprecated)
import os
with open('urls.txt') as urls:
for url in urls:
os.system("youtube-dl --write-thumbnail --skip-download " + url)
Upvotes: 1
Reputation: 3048
Building on the other answers, you can also specify the -a
or --batch-file
flag to provide a list of files to import from, which simplifies the code a bit:
youtube-dl -a urls.txt --write-thumbnail --skip-download
Upvotes: 17
Reputation: 501
You can simply add --skip-download to your code and it will work fine. Like so:
with open('urls.txt') as f:
for line in f:
os.system("youtube-dl "+"--write-thumbnail "+"--skip-download "+line)
Upvotes: 35
Reputation: 34
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 = re.search("(?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):
file.write(chunk)
Hope this helped!
Upvotes: 0