Implemented --exec option.

This commit is contained in:
mcd1992 2014-08-22 16:40:43 -05:00
parent b8313f07bc
commit a7cacbca2b
5 changed files with 54 additions and 1 deletions

View file

@ -288,6 +288,8 @@ which means you can modify it, redistribute it or use it however you like.
postprocessors (default) postprocessors (default)
--prefer-ffmpeg Prefer ffmpeg over avconv for running the --prefer-ffmpeg Prefer ffmpeg over avconv for running the
postprocessors postprocessors
--exec Execute a command after the file is finished downloading, similar to find's -exec format
Example: --exec 'adb push {} /sdcard/Music/ && rm {}'
# CONFIGURATION # CONFIGURATION

BIN
youtube_dl/.__init__.py.swp Normal file

Binary file not shown.

View file

@ -71,8 +71,12 @@ __authors__ = (
'Sebastian Haas', 'Sebastian Haas',
'Alexander Kirk', 'Alexander Kirk',
'Erik Johnson', 'Erik Johnson',
<<<<<<< HEAD
'Keith Beckman', 'Keith Beckman',
'Ole Ernst', 'Ole Ernst',
=======
'Aaron McDaniel (mcd1992)',
>>>>>>> Implemented --exec option.
) )
__license__ = 'Public Domain' __license__ = 'Public Domain'
@ -119,6 +123,7 @@ from .postprocessor import (
FFmpegExtractAudioPP, FFmpegExtractAudioPP,
FFmpegEmbedSubtitlePP, FFmpegEmbedSubtitlePP,
XAttrMetadataPP, XAttrMetadataPP,
ExecAfterDownload,
) )
@ -550,7 +555,8 @@ def parseOpts(overrideArguments=None):
help='Prefer avconv over ffmpeg for running the postprocessors (default)') help='Prefer avconv over ffmpeg for running the postprocessors (default)')
postproc.add_option('--prefer-ffmpeg', action='store_true', dest='prefer_ffmpeg', postproc.add_option('--prefer-ffmpeg', action='store_true', dest='prefer_ffmpeg',
help='Prefer ffmpeg over avconv for running the postprocessors') help='Prefer ffmpeg over avconv for running the postprocessors')
postproc.add_option('--exec', metavar='', action='store', dest='execstring',
help='Execute a command on the file after downloading, similar to find\'s -exec syntax. Must be enclosed in quotes. Example: --exec \'adb push {} /sdcard/Music/ && rm {}\'' )
parser.add_option_group(general) parser.add_option_group(general)
parser.add_option_group(selection) parser.add_option_group(selection)
@ -831,6 +837,7 @@ def _real_main(argv=None):
'default_search': opts.default_search, 'default_search': opts.default_search,
'youtube_include_dash_manifest': opts.youtube_include_dash_manifest, 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
'encoding': opts.encoding, 'encoding': opts.encoding,
'execstring': opts.execstring,
} }
with YoutubeDL(ydl_opts) as ydl: with YoutubeDL(ydl_opts) as ydl:
@ -854,6 +861,12 @@ def _real_main(argv=None):
ydl.add_post_processor(FFmpegAudioFixPP()) ydl.add_post_processor(FFmpegAudioFixPP())
ydl.add_post_processor(AtomicParsleyPP()) 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.execstring:
ydl.add_post_processor(ExecAfterDownload(commandString=opts.execstring))
# Update version # Update version
if opts.update_self: if opts.update_self:
update_self(ydl.to_screen, opts.verbose) update_self(ydl.to_screen, opts.verbose)

View file

@ -9,6 +9,7 @@ from .ffmpeg import (
FFmpegEmbedSubtitlePP, FFmpegEmbedSubtitlePP,
) )
from .xattrpp import XAttrMetadataPP from .xattrpp import XAttrMetadataPP
from .execafterdownload import ExecAfterDownload
__all__ = [ __all__ = [
'AtomicParsleyPP', 'AtomicParsleyPP',
@ -19,4 +20,5 @@ __all__ = [
'FFmpegExtractAudioPP', 'FFmpegExtractAudioPP',
'FFmpegEmbedSubtitlePP', 'FFmpegEmbedSubtitlePP',
'XAttrMetadataPP', 'XAttrMetadataPP',
'ExecAfterDownload',
] ]

View file

@ -0,0 +1,36 @@
# ExecAfterDownload written by AaronM / mcd1992.
# If there are any issues with this postprocessor please contact me via github or admin@fgthou.se
import os, re, shlex
from ..utils import PostProcessingError
class ExecAfterDownload( object ):
_downloader = None
def __init__( self, downloader = None, commandString = None ):
self._downloader = downloader
self.commandString = commandString
def set_downloader( self, downloader ):
"""Sets the downloader for this PP."""
self._downloader = downloader
def run( self, information ):
self.targetFile = information["filepath"]
self.finalCommand = None;
if( re.search( '{}', self.commandString ) ): # Find and replace all occurrences of {} with the file name.
self.finalCommand = re.sub( "{}", '\'' + self.targetFile + '\'', self.commandString )
else:
self.finalCommand = self.commandString + ' \'' + self.targetFile + '\''
if( self.finalCommand ):
print( "[exec] Executing command: " + self.finalCommand )
os.system( self.finalCommand )
else:
raise PostProcessingExecError( "Invalid syntax for --exec post processor" )
return None, information # by default, keep file and do nothing
class PostProcessingExecError( PostProcessingError ):
pass