[YoutubeDL] Add declarative version of progress hooks
authorPhilipp Hagemeister <phihag@phihag.de>
Mon, 15 Dec 2014 00:26:18 +0000 (01:26 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Mon, 15 Dec 2014 00:26:20 +0000 (01:26 +0100)
README.md
youtube_dl/YoutubeDL.py
youtube_dl/downloader/common.py

index edfcfc2236d7935c1131a566d55aa60dad7a6de1..bd35a821449116d17ed9a76e2abe29ae1d1fcb42 100644 (file)
--- a/README.md
+++ b/README.md
@@ -537,7 +537,7 @@ From a Python program, you can embed youtube-dl in a more powerful fashion, like
 
 Most likely, you'll want to use various options. For a list of what can be done, have a look at [youtube_dl/YoutubeDL.py](https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L69). For a start, if you want to intercept youtube-dl's output, set a `logger` object.
 
-Here's a more complete example of a program that only outputs errors, and downloads/converts the video as mp3:
+Here's a more complete example of a program that outputs only errors (and a short message after the download is finished), and downloads/converts the video to an mp3 file:
 
 
     import youtube_dl
@@ -553,6 +553,12 @@ Here's a more complete example of a program that only outputs errors, and downlo
         def error(self, msg):
             print(msg)
 
+
+    def my_hook(d):
+        if d['status'] == 'finished':
+            print('Done downloading, now converting ...')
+
+
     ydl_opts = {
         'format': 'bestaudio/best',
         'postprocessors': [{
@@ -561,6 +567,7 @@ Here's a more complete example of a program that only outputs errors, and downlo
             'preferredquality': '64',
         }],
         'logger': MyLogger(),
+        'progress_hooks': [my_hook],
     }
     with youtube_dl.YoutubeDL(ydl_opts) as ydl:
         ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
index 6acfd8cf9b14b60cefbe080123fd57514a2f8b52..bea1ef83c82abb790d33851fb92cb8778a4bc3be 100755 (executable)
@@ -182,10 +182,27 @@ class YoutubeDL(object):
                        Pass in 'in_playlist' to only show this behavior for
                        playlist items.
     postprocessors:    A list of dictionaries, each with an entry
-                       key:  The name of the postprocessor. See
-                             youtube_dl/postprocessor/__init__.py for a list.
+                       key:  The name of the postprocessor. See
+                               youtube_dl/postprocessor/__init__.py for a list.
                        as well as any further keyword arguments for the
                        postprocessor.
+    progress_hooks:    A list of functions that get called on download
+                       progress, with a dictionary with the entries
+                       * filename: The final filename
+                       * status: One of "downloading" and "finished"
+
+                       The dict may also have some of the following entries:
+
+                       * downloaded_bytes: Bytes on disk
+                       * total_bytes: Size of the whole file, None if unknown
+                       * tmpfilename: The filename we're currently writing to
+                       * eta: The estimated time in seconds, None if unknown
+                       * speed: The download speed in bytes/second, None if
+                                unknown
+
+                       Progress hooks are guaranteed to be called at least once
+                       (with status "finished") if the download is successful.
+
 
     The following parameters are not used by YoutubeDL itself, they are used by
     the FileDownloader:
@@ -273,6 +290,9 @@ class YoutubeDL(object):
             pp = pp_class(self, **compat_kwargs(pp_def))
             self.add_post_processor(pp)
 
+        for ph in self.params.get('progress_hooks', []):
+            self.add_progress_hook(ph)
+
     def warn_if_short_id(self, argv):
         # short YouTube ID starting with dash?
         idxs = [
index 8181bca093c227d1f11b1c2d91d61f026749a731..d3e0b011047e8f59a10639d41d191d6cd8800a31 100644 (file)
@@ -305,19 +305,6 @@ class FileDownloader(object):
             ph(status)
 
     def add_progress_hook(self, ph):
-        """ ph gets called on download progress, with a dictionary with the entries
-        * filename: The final filename
-        * status: One of "downloading" and "finished"
-
-        It can also have some of the following entries:
-
-        * downloaded_bytes: Bytes on disks
-        * total_bytes: Total bytes, None if unknown
-        * tmpfilename: The filename we're currently writing to
-        * eta: The estimated time in seconds, None if unknown
-        * speed: The download speed in bytes/second, None if unknown
-
-        Hooks are guaranteed to be called at least once (with status "finished")
-        if the download is successful.
-        """
+        # See YoutubeDl.py (search for progress_hooks) for a description of
+        # this interface
         self._progress_hooks.append(ph)