]> jfr.im git - yt-dlp.git/commitdiff
Pass any field to `--exec` using similar syntax to output template
authorpukkandan <redacted>
Sun, 11 Apr 2021 00:09:55 +0000 (05:39 +0530)
committerpukkandan <redacted>
Sun, 11 Apr 2021 00:36:12 +0000 (06:06 +0530)
Related: https://github.com/ytdl-org/youtube-dl/issues/28642

README.md
yt_dlp/options.py
yt_dlp/postprocessor/execafterdownload.py

index e689f955acec95b2f76e3da3748deb3155a59206..a7832508b899b9a0df22394aa2c015d5d13035af 100644 (file)
--- a/README.md
+++ b/README.md
@@ -686,9 +686,14 @@ ## Post-Processing Options:
                                      path to the binary or its containing
                                      directory
     --exec CMD                       Execute a command on the file after
-                                     downloading and post-processing, similar to
-                                     find's -exec syntax. Example: --exec 'adb
-                                     push {} /sdcard/Music/ && rm {}'
+                                     downloading and post-processing. Similar
+                                     syntax to the output template can be used
+                                     to pass any field as arguments to the
+                                     command. An additional field "filepath"
+                                     that contains the final path of the
+                                     downloaded file is also available. If no
+                                     fields are passed, "%(filepath)s" is
+                                     appended to the end of the command
     --convert-subs FORMAT            Convert the subtitles to another format
                                      (currently supported: srt|ass|vtt|lrc)
                                      (Alias: --convert-subtitles)
index ace353042e91b0823f68c189263a470db97cbafa..c4cb57e2fa5d90dbf36df38a791c615c902f8792 100644 (file)
@@ -1195,7 +1195,11 @@ def _dict_from_multiple_values_options_callback(
     postproc.add_option(
         '--exec',
         metavar='CMD', dest='exec_cmd',
-        help='Execute a command on the file after downloading and post-processing, similar to find\'s -exec syntax. Example: --exec \'adb push {} /sdcard/Music/ && rm {}\'')
+        help=(
+            'Execute a command on the file after downloading and post-processing. '
+            'Similar syntax to the output template can be used to pass any field as arguments to the command. '
+            'An additional field "filepath" that contains the final path of the downloaded file is also available. '
+            'If no fields are passed, "%(filepath)s" is appended to the end of the command'))
     postproc.add_option(
         '--convert-subs', '--convert-sub', '--convert-subtitles',
         metavar='FORMAT', dest='convertsubtitles', default=None,
index 24dc64ef0b5e5b4085edf7d4909c0941d0bd9077..95159cbc2793fc8f9d044293ac31f56ea935bed2 100644 (file)
@@ -20,12 +20,13 @@ def __init__(self, downloader, exec_cmd):
     def pp_key(cls):
         return 'Exec'
 
-    def run(self, information):
-        cmd = self.exec_cmd
-        if '{}' not in cmd:
-            cmd += ' {}'
-
-        cmd = cmd.replace('{}', compat_shlex_quote(information['filepath']))
+    def run(self, info):
+        tmpl, info_copy = self._downloader.prepare_outtmpl(self.exec_cmd, info)
+        cmd = tmpl % info_copy
+        if cmd == self.exec_cmd:  # No replacements were made
+            if '{}' not in self.exec_cmd:
+                self.exec_cmd += ' {}'
+            cmd = self.exec_cmd.replace('{}', compat_shlex_quote(info['filepath']))
 
         self.to_screen('Executing command: %s' % cmd)
         retCode = subprocess.call(encodeArgument(cmd), shell=True)
@@ -33,4 +34,4 @@ def run(self, information):
             raise PostProcessingError(
                 'Command returned error code %d' % retCode)
 
-        return [], information
+        return [], info