A feature-rich command-line audio/video downloader
[YoutubeDL] Make postprocessors declarative
Philipp Hagemeister4f026fafbc6c
diff --git a/README.md b/README.mdindex f10f06ee8c..edfcfc2236 100644--- a/README.md+++ b/README.md@@ -537,6 +537,34 @@ From a Python program, you can embed youtube-dl in a more powerful fashion, likeMost 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:+++ import youtube_dl+++ class MyLogger(object):+ def debug(self, msg):+ pass++ def warning(self, msg):+ pass++ def error(self, msg):+ print(msg)++ ydl_opts = {+ 'format': 'bestaudio/best',+ 'postprocessors': [{+ 'key': 'FFmpegExtractAudio',+ 'preferredcodec': 'mp3',+ 'preferredquality': '64',+ }],+ 'logger': MyLogger(),+ }+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:+ ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])+# BUGSBugs and suggestions should be reported at: <https://github.com/rg3/youtube-dl/issues> . Unless you were prompted so or there is another pertinent reason (e.g. GitHub fails to accept the bug report), please do not send bug reports via personal email. For discussions, join us in the irc channel #youtube-dl on freenode.diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.pyindex 578c8daf25..6acfd8cf9b 100755--- a/youtube_dl/YoutubeDL.py+++ b/youtube_dl/YoutubeDL.py@@ -27,6 +27,7 @@ from .compat import (compat_cookiejar,compat_expanduser,compat_http_client,+ compat_kwargs,compat_str,compat_urllib_error,compat_urllib_request,@@ -67,7 +68,11 @@ from .cache import Cachefrom .extractor import get_info_extractor, gen_extractorsfrom .downloader import get_suitable_downloaderfrom .downloader.rtmp import rtmpdump_version-from .postprocessor import FFmpegMergerPP, FFmpegPostProcessor+from .postprocessor import (+ FFmpegMergerPP,+ FFmpegPostProcessor,+ get_postprocessor,+)from .version import __version__@@ -176,6 +181,11 @@ class YoutubeDL(object):extract_flat: Do not resolve URLs, return the immediate result.Pass in 'in_playlist' to only show this behavior forplaylist 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.+ as well as any further keyword arguments for the+ postprocessor.The following parameters are not used by YoutubeDL itself, they are used bythe FileDownloader:@@ -256,6 +266,13 @@ class YoutubeDL(object):self.print_debug_header()self.add_default_info_extractors()+ for pp_def_raw in self.params.get('postprocessors', []):+ pp_class = get_postprocessor(pp_def_raw['key'])+ pp_def = dict(pp_def_raw)+ del pp_def['key']+ pp = pp_class(self, **compat_kwargs(pp_def))+ self.add_post_processor(pp)+def warn_if_short_id(self, argv):# short YouTube ID starting with dash?idxs = [diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.pyindex 70c4f25b16..e793203232 100644--- a/youtube_dl/__init__.py+++ b/youtube_dl/__init__.py@@ -40,16 +40,6 @@ from .downloader import ()from .extractor import gen_extractorsfrom .YoutubeDL import YoutubeDL-from .postprocessor import (- AtomicParsleyPP,- FFmpegAudioFixPP,- FFmpegMetadataPP,- FFmpegVideoConvertor,- FFmpegExtractAudioPP,- FFmpegEmbedSubtitlePP,- XAttrMetadataPP,- ExecAfterDownloadPP,-)def _real_main(argv=None):@@ -212,6 +202,43 @@ def _real_main(argv=None):any_printing = opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_jsondownload_archive_fn = compat_expanduser(opts.download_archive) if opts.download_archive is not None else opts.download_archive+ # PostProcessors+ postprocessors = []+ # Add the metadata pp first, the other pps will copy it+ if opts.addmetadata:+ postprocessors.append({'key': 'FFmpegMetadata'})+ if opts.extractaudio:+ postprocessors.append({+ 'key': 'FFmpegExtractAudio',+ 'preferredcodec': opts.audioformat,+ 'preferredquality': opts.audioquality,+ 'nopostoverwrites': opts.nopostoverwrites,+ })+ if opts.recodevideo:+ postprocessors.append({+ 'key': 'FFmpegVideoConvertor',+ 'preferedformat': opts.recodevideo,+ })+ if opts.embedsubtitles:+ postprocessors.append({+ 'key': 'FFmpegEmbedSubtitle',+ 'subtitlesformat': opts.subtitlesformat,+ })+ if opts.xattrs:+ postprocessors.append({'key': 'XAttrMetadata'})+ if opts.embedthumbnail:+ if not opts.addmetadata:+ postprocessors.append({'key': 'FFmpegAudioFix'})+ postprocessors.append({'key': 'AtomicParsley'})+ # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.+ # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.+ if opts.exec_cmd:+ postprocessors.append({+ 'key': 'ExecAfterDownload',+ 'verboseOutput': opts.verbose,+ 'exec_cmd': opts.exec_cmd,+ })+ydl_opts = {'usenetrc': opts.usenetrc,'username': opts.username,@@ -297,32 +324,10 @@ def _real_main(argv=None):'encoding': opts.encoding,'exec_cmd': opts.exec_cmd,'extract_flat': opts.extract_flat,+ 'postprocessors': postprocessors,}with YoutubeDL(ydl_opts) as ydl:- # PostProcessors- # Add the metadata pp first, the other pps will copy it- if opts.addmetadata:- ydl.add_post_processor(FFmpegMetadataPP())- if opts.extractaudio:- ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat, preferredquality=opts.audioquality, nopostoverwrites=opts.nopostoverwrites))- if opts.recodevideo:- ydl.add_post_processor(FFmpegVideoConvertor(preferedformat=opts.recodevideo))- if opts.embedsubtitles:- ydl.add_post_processor(FFmpegEmbedSubtitlePP(subtitlesformat=opts.subtitlesformat))- if opts.xattrs:- ydl.add_post_processor(XAttrMetadataPP())- if opts.embedthumbnail:- if not opts.addmetadata:- ydl.add_post_processor(FFmpegAudioFixPP())- ydl.add_post_processor(AtomicParsleyPP())-- # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way.- # So if the user is able to remove the file before your postprocessor runs it might cause a few problems.- if opts.exec_cmd:- ydl.add_post_processor(ExecAfterDownloadPP(- verboseOutput=opts.verbose, exec_cmd=opts.exec_cmd))-# Update versionif opts.update_self:update_self(ydl.to_screen, opts.verbose)diff --git a/youtube_dl/postprocessor/__init__.py b/youtube_dl/postprocessor/__init__.pyindex fb367ebe44..7f505b58e2 100644--- a/youtube_dl/postprocessor/__init__.py+++ b/youtube_dl/postprocessor/__init__.py@@ -8,11 +8,16 @@ from .ffmpeg import (FFmpegExtractAudioPP,FFmpegMergerPP,FFmpegMetadataPP,- FFmpegVideoConvertor,+ FFmpegVideoConvertorPP,)from .xattrpp import XAttrMetadataPPfrom .execafterdownload import ExecAfterDownloadPP++def get_postprocessor(key):+ return globals()[key + 'PP']++__all__ = ['AtomicParsleyPP','ExecAfterDownloadPP',@@ -22,6 +27,6 @@ __all__ = ['FFmpegMergerPP','FFmpegMetadataPP','FFmpegPostProcessor',- 'FFmpegVideoConvertor',+ 'FFmpegVideoConvertorPP','XAttrMetadataPP',]diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.pyindex 965ded4c15..048525efca 100644--- a/youtube_dl/postprocessor/ffmpeg.py+++ b/youtube_dl/postprocessor/ffmpeg.py@@ -236,9 +236,9 @@ class FFmpegExtractAudioPP(FFmpegPostProcessor):return self._nopostoverwrites, information-class FFmpegVideoConvertor(FFmpegPostProcessor):+class FFmpegVideoConvertorPP(FFmpegPostProcessor):def __init__(self, downloader=None, preferedformat=None):- super(FFmpegVideoConvertor, self).__init__(downloader)+ super(FFmpegVideoConvertorPP, self).__init__(downloader)self._preferedformat = preferedformatdef run(self, information):