From 4292fd33715e7d5a6a1aa9e0be619030ce32ddd1 Mon Sep 17 00:00:00 2001 From: fnord Date: Wed, 24 Jun 2015 01:23:14 -0500 Subject: [PATCH 1/7] --custom-meta option to write specific information to tags, for --add-metadata Syntax: --custom-meta "TAGNAME=string with optional %(format)s" Example: --custom-meta "comment=%(webpage_url)s\n%(description)s" This can be invoked multiple times for different tags. Example 2: youtube-dl --add-metadata -f 18 --meta 'artist=%(uploader_id)s' --meta 'comment=url: %(webpage_url)s\nviews: %(view_count)s / Rating: %(average_rating)s\n\n%(description)s' https://www.youtube.com/watch?v=WzhW20hLp6M (snip) [ffmpeg] Adding metadata to 'Breaking Bad Remix (Seasons 3-5)-WzhW20hLp6M.mp4' mp4info Breaking\ Bad\ Remix\ \(Seasons\ 3-5\)-WzhW20hLp6M.mp4 (snip) Artist: uploader: placeboing Comments: url: https://www.youtube.com/watch?v=WzhW20hLp6M views: 5601679 / Rating: 4.97684526443 Music and remixing by me. Seasons 1 and 2 remix: http://www.youtube.com/watch?v=WsqdmqRgrIc MP3 download: https://www.dropbox.com/s/39luqbbi5cu36du/Chris%20Lohr%20-%20Breaking%20Bad%20Remix%20seasons%203-5.mp3 --- youtube_dl/__init__.py | 1 + youtube_dl/options.py | 10 ++++++++++ youtube_dl/postprocessor/ffmpeg.py | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index ace17857c..89192b3d3 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -367,6 +367,7 @@ def _real_main(argv=None): 'hls_prefer_native': opts.hls_prefer_native, 'external_downloader_args': external_downloader_args, 'cn_verification_proxy': opts.cn_verification_proxy, + 'custommeta': opts.custommeta, } with YoutubeDL(ydl_opts) as ydl: diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 740458e51..8427a58e5 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -707,6 +707,16 @@ def parseOpts(overrideArguments=None): '--add-metadata', action='store_true', dest='addmetadata', default=False, help='Write metadata to the video file') + postproc.add_option( + '--custom-meta', + action='append', dest='custommeta', default=[], + help='Write specific information to a metadata tag.' + 'Syntax: "tagname=string to add with %(format)s" ' + 'The formatting syntax is the same as output. ' + 'Example: --custom-meta "comment=%(webpage_url)s\\n%(description)s" will ' + 'add a line with the url, then the description in the "comment" tag. ' + 'Tags are format-specific, common ones include: artist, comment, title, copyright, uploader. ' + 'This can be invoked multiple times for different tags.') postproc.add_option( '--metadata-from-title', metavar='FORMAT', dest='metafromtitle', diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index fe7e0a8ee..c27c0db3b 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -367,6 +367,10 @@ class FFmpegMetadataPP(FFmpegPostProcessor): if info.get('album') is not None: metadata['album'] = info['album'] + for m in self._downloader.params.get('custommeta'): + key,val = m.split('=',1) + metadata[key] = val.replace('\\n', '\n') % info + if not metadata: self._downloader.to_screen('[ffmpeg] There isn\'t any metadata to add') return [], info From 1db323f913f1a30d1153c5db0af078491c2dca5d Mon Sep 17 00:00:00 2001 From: fnord Date: Thu, 25 Jun 2015 00:56:52 -0500 Subject: [PATCH 2/7] fix --help: '--custom-meta None' => '--custom-meta TAG=FORMAT' --- youtube_dl/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 8427a58e5..d7ec8fe8b 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -709,7 +709,7 @@ def parseOpts(overrideArguments=None): help='Write metadata to the video file') postproc.add_option( '--custom-meta', - action='append', dest='custommeta', default=[], + action='append', dest='custommeta', default=[], metavar='TAG=FORMAT', help='Write specific information to a metadata tag.' 'Syntax: "tagname=string to add with %(format)s" ' 'The formatting syntax is the same as output. ' From fdc3791989870c5ce02bb96fa7cf73e4cd456fd8 Mon Sep 17 00:00:00 2001 From: fnord Date: Fri, 17 Jul 2015 01:53:52 -0500 Subject: [PATCH 3/7] --custom-meta: do not carp on invalid %(info_keys) --- youtube_dl/postprocessor/ffmpeg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index c27c0db3b..1a3dc47af 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -4,7 +4,7 @@ import io import os import subprocess import time - +import collections from .common import AudioConversionError, PostProcessor @@ -368,8 +368,8 @@ class FFmpegMetadataPP(FFmpegPostProcessor): metadata['album'] = info['album'] for m in self._downloader.params.get('custommeta'): - key,val = m.split('=',1) - metadata[key] = val.replace('\\n', '\n') % info + key, val = m.split('=', 1) + metadata[key] = val.replace('\\n', '\n') % collections.defaultdict(lambda: 'NA', info) if not metadata: self._downloader.to_screen('[ffmpeg] There isn\'t any metadata to add') From 01e224e0dae3ce4f1a85bdb97682cd9e22049077 Mon Sep 17 00:00:00 2001 From: fnord Date: Fri, 17 Jul 2015 02:01:26 -0500 Subject: [PATCH 4/7] --custom-meta make %(invalid_key)s blank. This means --custom-meta 'album=%(playlist_title)s' will not have an album tag for media with playlist_title, as opposed to having it set to NA. This is more in line with --add-metadata's behavior --- youtube_dl/postprocessor/ffmpeg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index 1a3dc47af..e67f14acb 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -369,7 +369,7 @@ class FFmpegMetadataPP(FFmpegPostProcessor): for m in self._downloader.params.get('custommeta'): key, val = m.split('=', 1) - metadata[key] = val.replace('\\n', '\n') % collections.defaultdict(lambda: 'NA', info) + metadata[key] = val.replace('\\n', '\n') % collections.defaultdict(lambda: '', info) if not metadata: self._downloader.to_screen('[ffmpeg] There isn\'t any metadata to add') From a95e0691026dfd8d7e60c027b455adb9b09c4f2f Mon Sep 17 00:00:00 2001 From: fnord Date: Fri, 24 Jul 2015 03:39:43 -0500 Subject: [PATCH 5/7] --custom-meta activate --add-metadata --- youtube_dl/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 89192b3d3..daab9f728 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -214,7 +214,7 @@ def _real_main(argv=None): 'key': 'MetadataFromTitle', 'titleformat': opts.metafromtitle }) - if opts.addmetadata: + if opts.addmetadata or opts.custommeta: postprocessors.append({'key': 'FFmpegMetadata'}) if opts.extractaudio: postprocessors.append({ From 489e08cdf412272256bb4b7371af347bec080d1e Mon Sep 17 00:00:00 2001 From: fnord Date: Fri, 24 Jul 2015 03:47:26 -0500 Subject: [PATCH 6/7] Rename --custom-meta to --custom-metadata. Esplain --custom-metadata activating --add-metadata --- youtube_dl/options.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index d7ec8fe8b..7e1de9568 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -708,15 +708,16 @@ def parseOpts(overrideArguments=None): action='store_true', dest='addmetadata', default=False, help='Write metadata to the video file') postproc.add_option( - '--custom-meta', + '--custom-metadata', action='append', dest='custommeta', default=[], metavar='TAG=FORMAT', help='Write specific information to a metadata tag.' 'Syntax: "tagname=string to add with %(format)s" ' 'The formatting syntax is the same as output. ' - 'Example: --custom-meta "comment=%(webpage_url)s\\n%(description)s" will ' + 'Example: --custom-metadata "comment=%(webpage_url)s\\n%(description)s" will ' 'add a line with the url, then the description in the "comment" tag. ' 'Tags are format-specific, common ones include: artist, comment, title, copyright, uploader. ' - 'This can be invoked multiple times for different tags.') + 'This can be invoked multiple times for different tags. ' + '--add-metadata is activated with this option.') postproc.add_option( '--metadata-from-title', metavar='FORMAT', dest='metafromtitle', From 699a91a8f276a559adf6be644f8a10c0d80b7b74 Mon Sep 17 00:00:00 2001 From: fnord Date: Fri, 24 Jul 2015 04:02:11 -0500 Subject: [PATCH 7/7] (alternative behavior): don't add default metadata when --custom-metadata is used without --add-metadata --- youtube_dl/__init__.py | 1 + youtube_dl/postprocessor/ffmpeg.py | 35 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index daab9f728..17cb49071 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -367,6 +367,7 @@ def _real_main(argv=None): 'hls_prefer_native': opts.hls_prefer_native, 'external_downloader_args': external_downloader_args, 'cn_verification_proxy': opts.cn_verification_proxy, + 'addmetadata': opts.addmetadata, 'custommeta': opts.custommeta, } diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index e67f14acb..a87fd51c1 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -349,23 +349,24 @@ class FFmpegEmbedSubtitlePP(FFmpegPostProcessor): class FFmpegMetadataPP(FFmpegPostProcessor): def run(self, info): metadata = {} - if info.get('title') is not None: - metadata['title'] = info['title'] - if info.get('upload_date') is not None: - metadata['date'] = info['upload_date'] - if info.get('artist') is not None: - metadata['artist'] = info['artist'] - elif info.get('uploader') is not None: - metadata['artist'] = info['uploader'] - elif info.get('uploader_id') is not None: - metadata['artist'] = info['uploader_id'] - if info.get('description') is not None: - metadata['description'] = info['description'] - metadata['comment'] = info['description'] - if info.get('webpage_url') is not None: - metadata['purl'] = info['webpage_url'] - if info.get('album') is not None: - metadata['album'] = info['album'] + if self._downloader.params.get('addmetadata'): + if info.get('title') is not None: + metadata['title'] = info['title'] + if info.get('upload_date') is not None: + metadata['date'] = info['upload_date'] + if info.get('artist') is not None: + metadata['artist'] = info['artist'] + elif info.get('uploader') is not None: + metadata['artist'] = info['uploader'] + elif info.get('uploader_id') is not None: + metadata['artist'] = info['uploader_id'] + if info.get('description') is not None: + metadata['description'] = info['description'] + metadata['comment'] = info['description'] + if info.get('webpage_url') is not None: + metadata['purl'] = info['webpage_url'] + if info.get('album') is not None: + metadata['album'] = info['album'] for m in self._downloader.params.get('custommeta'): key, val = m.split('=', 1)