]> git.bitcoin.ninja Git - youtube-dl/commitdiff
add --write-thumbnail option to download thumbnail (Suggested by `)
authorPhilipp Hagemeister <phihag@phihag.de>
Wed, 1 May 2013 12:04:33 +0000 (14:04 +0200)
committerPhilipp Hagemeister <phihag@phihag.de>
Wed, 1 May 2013 12:04:33 +0000 (14:04 +0200)
youtube_dl/FileDownloader.py
youtube_dl/__init__.py

index a67d3c902e3facfca27e0b8546d3f3bc491df4a7..574863e7c9da066b1fc29741a83a58f2a2baaed4 100644 (file)
@@ -7,6 +7,7 @@ import math
 import io
 import os
 import re
+import shutil
 import socket
 import subprocess
 import sys
@@ -79,6 +80,7 @@ class FileDownloader(object):
     updatetime:        Use the Last-modified header to set output file timestamps.
     writedescription:  Write the video description to a .description file
     writeinfojson:     Write the video description to a .info.json file
+    writethumbnail:    Write the thumbnail image to a file
     writesubtitles:    Write the video subtitles to a file
     onlysubtitles:     Downloads only the subtitles of the video
     allsubtitles:      Downloads all the subtitles of the video
@@ -658,6 +660,20 @@ class FileDownloader(object):
                 self.report_error(u'Cannot write metadata to JSON file ' + infofn)
                 return
 
+        if self.params.get('writethumbnail', False):
+            if 'thumbnail' in info_dict:
+                thumb_format = info_dict['thumbnail'].rpartition(u'/')[2].rpartition(u'.')[2]
+                if not thumb_format:
+                    thumb_format = 'jpg'
+                thumb_filename = filename.rpartition('.')[0] + u'.' + thumb_format
+                self.to_screen(u'[%s] %s: Downloading thumbnail ...' %
+                               (info_dict['extractor'], info_dict['id']))
+                uf = compat_urllib_request.urlopen(info_dict['thumbnail'])
+                with open(thumb_filename, 'wb') as thumbf:
+                    shutil.copyfileobj(uf, thumbf)
+                self.to_screen(u'[%s] %s: Writing thumbnail to: %s' %
+                               (info_dict['extractor'], info_dict['id'], thumb_filename))
+
         if not self.params.get('skip_download', False):
             if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(filename)):
                 success = True
index 27f36c6adb852d34d4b70b4ba5fc9ba9257da244..808d4a8c2c4109f274e9ab4c61b2742db41c1149 100644 (file)
@@ -284,6 +284,9 @@ def parseOpts(overrideArguments=None):
     filesystem.add_option('--write-info-json',
             action='store_true', dest='writeinfojson',
             help='write video metadata to a .info.json file', default=False)
+    filesystem.add_option('--write-thumbnail',
+            action='store_true', dest='writethumbnail',
+            help='write thumbnail image to disk', default=False)
 
 
     postproc.add_option('-x', '--extract-audio', action='store_true', dest='extractaudio', default=False,
@@ -513,6 +516,7 @@ def _real_main(argv=None):
         'updatetime': opts.updatetime,
         'writedescription': opts.writedescription,
         'writeinfojson': opts.writeinfojson,
+        'writethumbnail': opts.writethumbnail,
         'writesubtitles': opts.writesubtitles,
         'onlysubtitles': opts.onlysubtitles,
         'allsubtitles': opts.allsubtitles,
@@ -529,7 +533,7 @@ def _real_main(argv=None):
         'keepvideo': opts.keepvideo,
         'min_filesize': opts.min_filesize,
         'max_filesize': opts.max_filesize,
-        'daterange': date
+        'daterange': date,
         })
 
     if opts.verbose: