desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Sets IP information in colname_ipdata.'
def set_data(self, addr, force=False):
if ((not force) and (self.get_data(addr) is not None)): return for data in [self.globaldb.data.country_byip(addr), self.globaldb.data.as_byip(addr), self.globaldb.data.location_byip(addr)]: if (data is not None): self.db[self.colname_ipdata].update({'addr': addr}, {'$set': data}, ups...
'Gets IP information in colname_ipdata.'
def get_data(self, addr):
data = self.find_one(self.colname_ipdata, {'addr': addr}) if (data is not None): del data['_id'] return data
'Initializes the data columns, and creates the default indexes.'
def init(self):
self.db[self.colname_geoip_country].drop() self.db[self.colname_geoip_as].drop() self.db[self.colname_geoip_city].drop() self.db[self.colname_country_codes].drop() self.db[self.colname_city_locations].drop() self.create_indexes()
'Initializes the "agent" columns, i.e., drops those columns and creates the default indexes.'
def init(self):
self.db[self.colname_agents].drop() self.db[self.colname_scans].drop() self.db[self.colname_masters].drop() self.create_indexes()
'The DB connection.'
@property def db(self):
try: return self._db except AttributeError: self._db = Graph(self.dburl) return self._db
'The tuple representing the database version'
@property def db_version(self):
try: return self._db_version except: self._db_version = self.db.neo4j_version return self._db_version
'Returns a WHERE clause (tuple (query, parameters)) from a single filter (no OR). Devs: `flt` **can** be set from an untrusted source.'
def _add_clause_from_filter(self, flt, mode='node'):
if (not flt): return None if (flt[0] in '-!~'): neg = True flt = flt[1:] else: neg = False array_mode = None len_mode = None if flt.startswith('ANY '): array_mode = 'ANY' flt = flt[4:] elif flt.startswith('ALL '): array_mode = 'AL...
'ADD a WHERE clause from a node filter. Devs: `flt` **can** be set from an untrusted source.'
def add_clause_from_filter(self, flt, mode='node'):
(clauses, params) = ([], {}) for subflt in self._split_filter_or(flt): if subflt: (subclause, subparams) = self._add_clause_from_filter(subflt, mode=mode) clauses.append(subclause) params.update(subparams) return self.add_clause(('WHERE %s' % ' OR '.join(...
'`size` is the number of inserts per commit and `retries` is the number of times to retry a failed transaction (when inserting concurrently for example). 0 is forever, 1 does not retry, 2 retries once, etc.'
def __init__(self, db, size=None, retries=0):
self.db = db self.queries = [] self.start_time = time.time() self.count = 0 self.commited_count = 0 self.size = (config.NEO4J_BATCH_SIZE if (size is None) else size) self.retries = retries
'Transforms (year, month, date, hour) into datetime.'
@staticmethod def _time_quad2date(time_quad):
return datetime(*time_quad)
'Transforms a neo4j returned by executing a query into an iterator of {src: <dict>, flow: <dict>, dst: <dict>}.'
@classmethod def cursor2json_iter(cls, cursor):
for (src, flow, dst) in cursor: for rec in [src, flow, dst]: cls._cleanup_record(rec) src_props = cls._get_props(src['elt'], src.get('meta')) src_ref = cls._get_ref(src['elt'], src_props) src_labels = cls._get_labels(src['elt'], src_props) src_node = cls._node2jso...
'Transforms a cursor of triplets of (node, edge, node) to a graph of hosts and flows. All the elements are of the form {elt: <neo4j element-like>, meta: [<list of metadata>]} This is an internal API that is very likely to change.'
@classmethod def cursor2json_graph(cls, cursor):
random.seed(0) g = {'nodes': [], 'edges': []} done = set() for row in cls.cursor2json_iter(cursor): for (node, typ) in ((row['src'], 'nodes'), (row['flow'], 'edges'), (row['dst'], 'nodes')): if (node['id'] not in done): g[typ].append(node) done.add(nod...
'Returns a dict of {flow: {time_in_day: count}} WARNING/FIXME: this mutates the query'
def flow_daily(self, query):
query.add_clause('WITH src.elt as src, link.elt as link, dst.elt as dst\nMATCH (link)-[:SEEN]->(t:Time)\nWITH src, link, dst, t, (t.time % 86400) as time_in_day\nWITH [link.proto, COALESCE(link.dport, link.type)] AS flow,\n t...
'Returns an iterator of: {fields: <fields>, count: <number of occurrence or sum of sumfields>, collected: <collected fields>}. WARNING/FIXME: this mutates the query'
def top(self, query, fields, collect=None, sumfields=None):
collect = (collect or []) sumfields = (sumfields or []) for flist in (fields, collect, sumfields): for i in range(len(flist)): if flist[i].startswith('link.'): flist[i] = flist[i].replace('flow.', 'link.') if ('.' not in flist[i]): flist[i] = (...
'Cleanup mistakes when predicting client/server ports'
def cleanup_flows(self):
self._cleanup_phase1() self._cleanup_phase2() self._sanity_check()
'Subclasses can override this method to generate the CSV line from the original line.'
@staticmethod def fixline(line):
return line
'The DB connection.'
@property def db(self):
try: return self._db except AttributeError: self._db = create_engine(self.dburl, echo=config.DEBUG_DB) return self._db
'Filters records by their ObjectID. `oid` can be a single or many (as a list or any iterable) object ID(s), specified as strings or an `ObjectID`s.'
@classmethod def searchobjectid(cls, oid, neg=False):
if isinstance(oid, (int, basestring)): oid = [int(oid)] else: oid = [int(oid) for oid in oid] return cls._searchobjectid(oid, neg=neg)
'Filters (if `neg` == True, filters out) one particular host (IP address).'
@classmethod def searchhost(cls, addr, neg=False):
if neg: return (Host.addr != cls.convert_ip(addr)) return (Host.addr == cls.convert_ip(addr))
'This method produces a generator of distinct values for a given field.'
def distinct(self, field, flt=None, sort=None, limit=None, skip=None, **kargs):
if isinstance(field, basestring): field = self.fields[field] if (flt is None): flt = self.flt_empty sort = [((self.fields[key] if isinstance(key, basestring) else key), way) for (key, way) in (sort or [])] req = self._distinct_req(field, flt, **kargs) for (key, way) in sort: ...
'Returns a condition that is true iff all of the given conditions is true.'
@classmethod def flt_and(cls, *args):
return reduce(cls._flt_and, args)
'Returns a condition that is true iff any of the given conditions is true.'
@classmethod def flt_or(cls, *args):
return reduce(cls._flt_or, args)
'`size` is the number of inserts per commit and `retries` is the number of times to retry a failed transaction (when inserting concurrently for example). 0 is forever, 1 does not retry, 2 retries once, etc.'
def __init__(self, db, size=None, retries=0):
self.db = db self.start_time = time.time() self.commited_counts = {} self.size = (config.POSTGRES_BATCH_SIZE if (size is None) else size) self.retries = retries self.conn = db.connect() self.trans = self.conn.begin() self.queries = {}
'Returns an iterator of: {fields: <fields>, count: <number of occurrence or sum of sumfields>, collected: <collected fields>}.'
def top(self, query, fields, collect=None, sumfields=None):
raise NotImplementedError()
'Country database has been dropped in favor of Location/City'
def feed_geoip_country(self, *_, **__):
pass
'Returns a generator of every (start, stop) IP ranges for a country given its ISO-3166-1 "alpha-2" code or its name.'
def ipranges_bycountry(self, code):
if (len(code) != 2): return self.db.execute(select([Location_Range.start, Location_Range.stop]).select_from(join(join(Location, Location_Range), Country)).where((Country.name == code))) return self.db.execute(select([Location_Range.start, Location_Range.stop]).select_from(join(Location, Location_Range))...
'Returns a generator of every (start, stop) IP ranges for an Autonomous System given its number or its name.'
def ipranges_byas(self, asnum):
if isinstance(asnum, basestring): try: if asnum.startswith('AS'): asnum = int(asnum[2:]) else: asnum = int(asnum) except ValueError: return self.db.execute(select([AS_Range.start, AS_Range.stop]).select_from(join(AS, AS_Range)).wher...
'Backend-specific subclasses may use this method to create some bulk insert structures.'
def start_store_hosts(self):
self.bulk = self.start_bulk_insert()
'Backend-specific subclasses may use this method to commit bulk insert structures.'
def stop_store_hosts(self):
self.bulk.close() self.bulk = None
'Removes the host scan result. "host" must be a record as yielded by .get() or a valid NmapFilter() instance. The scan files that are no longer linked to a scan are removed at the end of the call.'
def remove(self, host, archive=False):
if isinstance(host, dict): base = [host['_id']] else: base = host.query(select([Scan.id]), archive=archive).cte('base') self.db.execute(delete(Scan).where(Scan.id.in_(base))) base = select([Association_Scan_ScanFile.scan_file]).cte('base') self.db.execute(delete(ScanFile).where(ScanF...
'This method makes use of the aggregation framework to produce top values for a given field or pseudo-field. Pseudo-fields are: - category / label / asnum / country / net[:mask] - port - port:open / :closed / :filtered / :<servicename> - portlist:open / :closed / :filtered - countports:open / :closed / :filtered - serv...
def topvalues(self, field, flt=None, topnbr=10, sort=None, limit=None, skip=None, least=False, archive=False):
if (flt is None): flt = NmapFilter() base = flt.query(select([Scan.id]).select_from(flt.select_from), archive=archive).cte('base') order = ('count' if least else desc('count')) outputproc = None if (field == 'port'): field = (Port, [Port.protocol, Port.port], (Port.state == 'open')) ...
'Filters (if `neg` == True, filters out) one particular host (IP address).'
@classmethod def searchhost(cls, addr, neg=False):
if neg: return NmapFilter(main=(Scan.addr != cls.convert_ip(addr))) return NmapFilter(main=(Scan.addr == cls.convert_ip(addr)))
'Filters (if `neg` == True, filters out) one particular country, or a list of countries.'
@classmethod def searchcountry(cls, country, neg=False):
country = utils.country_unalias(country) return NmapFilter(main=cls._searchstring_list(Scan.info['country_code'].astext, country, neg=neg))
'Filters (if `neg` == True, filters out) one particular city'
@classmethod def searchcity(cls, city, neg=False):
return NmapFilter(main=cls._searchstring_re(Scan.info['city'].astext, city, neg=neg))
'Filters (if `neg` == True, filters out) one or more particular AS number(s).'
@classmethod def searchasnum(cls, asnum, neg=False):
return NmapFilter(main=cls._searchstring_list(Scan.info['as_num'], asnum, neg=neg, map_=str))
'Filters (if `neg` == True, filters out) one or more particular AS.'
@classmethod def searchasname(cls, asname, neg=False):
return NmapFilter(main=cls._searchstring_re(Scan.info['as_name'].astext, asname, neg=neg))
'Filters (if `neg` == True, filters out) records with specified protocol/port at required state. Be aware that when a host has a lot of ports filtered or closed, it will not report all of them, but only a summary, and thus the filter might not work as expected. This filter will always work to find open ports.'
@staticmethod def searchport(port, protocol='tcp', state='open', neg=False):
if (port == 'host'): return NmapFilter(port=[(True, ((Port.port >= 0) if neg else (Port.port == (-1))))]) return NmapFilter(port=[((not neg), and_((Port.port == port), (Port.protocol == protocol), (Port.state == state)))])
'Filters records with at least one port other than those listed in `ports` with state `state`.'
@staticmethod def searchportsother(ports, protocol='tcp', state='open'):
return NmapFilter(port=[(True, and_(or_(Port.port.notin_(ports), (Port.protocol != protocol)), (Port.state == state)))])
'Filters records with open port number between minn and maxn'
@staticmethod def searchcountopenports(minn=None, maxn=None, neg=False):
assert ((minn is not None) or (maxn is not None)) req = select([column('scan')]).select_from(select([Port.scan.label('scan'), func.count().label('count')]).where((Port.state == 'open')).group_by(Port.scan).alias('pcnt')) if (minn == maxn): req = req.where((column('count') == minn)) else: ...
'Filters records with at least one open port.'
@staticmethod def searchopenport(neg=False):
return NmapFilter(port=[((not neg), (Port.state == 'open'))])
'Search an open port with a particular service.'
@classmethod def searchservice(cls, srv, port=None, protocol=None):
req = cls._searchstring_re(Port.service_name, srv) if (port is not None): req = and_(req, (Port.port == port)) if (protocol is not None): req = and_(req, (Port.protocol == protocol)) return NmapFilter(port=[(True, req)])
'Search a port with a particular `product`. It is (much) better to provide the `service` name and/or `port` number since those fields are indexed.'
@classmethod def searchproduct(cls, product, version=None, service=None, port=None, protocol=None):
req = cls._searchstring_re(Port.service_product, product) if (version is not None): req = and_(req, cls._searchstring_re(Port.service_version, version)) if (service is not None): req = and_(req, cls._searchstring_re(Port.service_name, service)) if (port is not None): req = and_(r...
'Search a particular content in the scripts results.'
@classmethod def searchscript(cls, name=None, output=None, values=None):
req = True if (name is not None): req = and_(req, cls._searchstring_re(Script.name, name, neg=False)) if (output is not None): req = and_(req, cls._searchstring_re(Script.output, output, neg=False)) if values: if (name is None): raise TypeError('.searchscript() nee...
'Search shared files from a file name (either a string or a regexp), only from scripts using the "ls" NSE module.'
@classmethod def searchfile(cls, fname=None, scripts=None):
if (fname is None): req = Script.data.op('@>')('{"ls": {"volumes": [{"files": []}]}}') elif isinstance(fname, utils.REGEXP_T): base1 = select([Script.port, func.jsonb_array_elements(func.jsonb_array_elements(Script.data['ls']['volumes']).op('->')('files')).op('->>')('filename').label('f...
'Queries the passive database with the provided filter "flt", and returns a generator.'
def _get(self, flt, limit=None, skip=None, sort=None):
req = flt.query(select([Host.addr, Passive.sensor, Passive.count, Passive.firstseen, Passive.lastseen, Passive.info, Passive.port, Passive.recontype, Passive.source, Passive.targetval, Passive.value, Passive.moreinfo]).select_from(flt.select_from)) for (key, way) in (sort or []): req = req.order_by((key...
'Queries the passive database with the provided filter "flt", and returns the first result, or None if no result exists.'
def get_one(self, flt, skip=None):
return self._get(flt, limit=1, skip=skip).fetchone()
'Like `.insert_or_update()`, but `specs` parameter has to be an iterable of `(timestamp, spec)` (if `separated_timestamps` is True) or `spec` (if it is False) values. This will perform PostgreSQL COPY FROM inserts with the major drawback that the `getinfos` parameter will be called (if it is not `None`) for each spec, ...
def insert_or_update_bulk(self, specs, getinfos=None, separated_timestamps=True):
more_to_read = True tmp = self.create_tmp_table(Passive, extracols=[Column('addr', postgresql.INET), Column('context', String(32))]) if config.DEBUG_DB: total_upserted = 0 total_start_time = time.time() while more_to_read: if config.DEBUG_DB: start_time = time.time() ...
'This function uses a MongoDB backup file as a source to feed the passive table.'
def migrate_from_mongodb_backup(self, backupfdesc):
def _backupgen(fdesc): for line in fdesc: try: line = line.decode() except AttributeError: pass try: line = json.loads(line) except ValueError: utils.LOGGER.warning('ignoring line [%r]', lin...
'This method makes use of the aggregation framework to produce top values for a given field. If `distinct` is True (default), the top values are computed by distinct events. If it is False, they are computed based on the "count" field.'
def topvalues(self, field, flt=None, topnbr=10, sort=None, limit=None, skip=None, least=False, distinct=True):
if isinstance(field, basestring): field = self.fields[field] outputproc = None if (flt is None): flt = PassiveFilter() base = flt.query(select([Passive.id]).select_from(flt.select_from)).cte('base') order = ('count' if least else desc('count')) req = flt.query(select([(func.count...
'Filters (if `neg` == True, filters out) one particular host (IP address).'
@staticmethod def searchhost(addr, neg=False):
return PassiveFilter(main=PostgresDB.searchhost(addr, neg=neg))
'Decides whether we should log a record'
def filter(self, record):
if (record.levelno < logging.INFO): if record.msg.startswith('DB:'): return config.DEBUG_DB return config.DEBUG if (record.levelno != logging.WARNING): return True if (record.msg in self.warnings): return False if (len(self.warnings) > self.MAX_WARNINGS_STORED...
'Stores parent\'s arguments for latter (manual) processing.'
def add_argument(self, *args, **kargs):
self.args.append((args, kargs))
'ranges must be given in the "correct" order *and* not overlap.'
def __init__(self, ranges=None):
self.ranges = {} self.length = 0 if (ranges is not None): for rnge in ranges: self.append(*rnge)
'Creates the Argus object. fdesc: a file-like object or a filename pcap_filter: a PCAP filter to use with racluster'
def __init__(self, fdesc, pcap_filter=None):
cmd = ['racluster', '-u', '-n', '-c', ',', '-m'] cmd.extend(self.aggregation) cmd.append('-s') cmd.extend(self.fields) cmd.extend(['-r', (fdesc if isinstance(fdesc, basestring) else '-')]) if (pcap_filter is not None): cmd.extend(['-', pcap_filter]) super(Argus, self).__init__(cmd, (...
'Creates the NetFlow object. fdesc: a file-like object or a filename pcap_filter: a PCAP filter to use with nfdump'
def __init__(self, fdesc, pcap_filter=None):
cmd = ['nfdump', '-aq', '-o', self.fmt] cmdkargs = {} if isinstance(fdesc, basestring): with open(fdesc) as fde: if (fde.read(2) not in utils.FileOpener.FILE_OPENERS_MAGIC): cmd.extend(['-r', fdesc]) else: cmdkargs['stdin'] = utils.open_file(fd...
'Builds an Agent instance from a description string of the form [tor:][hostname:]path.'
@classmethod def from_string(cls, string, localbase='', maxwaiting=60):
string = string.split(':', 1) if (string[0].lower() == 'tor'): string = string[1].split(':', 1) usetor = True else: usetor = False if (len(string) == 1): return cls(None, string[0], os.path.join(localbase, string[0].replace('/', '_')), maxwaiting=maxwaiting) return cl...
'Get local storage path for directory `dirname`.'
def get_local_path(self, dirname):
return (os.path.join(self.localpathbase, dirname) + '/')
'Get remote storage path for directory `dirname` as an rsync address.'
def get_remote_path(self, dirname):
if (dirname and (dirname[(-1)] != '/')): dirname += '/' return (self.rsyncbase + dirname)
'Create local directories used to manage the agent'
def create_local_dirs(self):
for dirname in ['input', 'remoteinput', 'remotecur', 'remoteoutput']: utils.makedirs(self.get_local_path(dirname))
'Get the number of targets that can be sent to the agent (based on the total number of targets currently on hold and the `maxwaiting` attribute value).'
def may_receive(self):
curwaiting = sum((len(os.listdir(self.get_local_path(p))) for p in ['input', 'remoteinput'])) return (self.maxwaiting - curwaiting)
'Add a new target (locally), given its category and address (technically, addr can be a network or a hostname that can be resolved from the agent).'
def add_target(self, category, addr):
with open(os.path.join(self.get_local_path('input'), ('%s.%s' % (category, addr.replace('/', '_')))), 'w') as fdesc: fdesc.write(('%s\n' % addr)) return True return False
'Synchronize the local and remote directories, and the relevant `Campaign`s.'
def sync(self):
subprocess.call((self.rsync + ['-a', self.get_local_path('input'), self.get_local_path('remoteinput')])) subprocess.call((self.rsync + ['-a', '--remove-source-files', self.get_local_path('input'), self.get_remote_path('input')])) subprocess.call((self.rsync + ['-a', '--delete', self.get_remote_path('input')...
'This function should only be called from `agent.sync()` method. It stores the results of terminated scans according to the target status.'
def sync(self, agent):
remout = agent.get_local_path('remoteoutput') for remfname in glob.glob(os.path.join(remout, (self.visiblecategory + '.*.xml'))): locfname = os.path.basename(remfname).split('.', 4) locfname[0] = self.category status = 'unknown' with open(remfname) as remfdesc: remfco...
'Send targets to scan to `agent`, depending on how many it can receive.'
def feed(self, agent, maxnbr=None):
for _ in range(max(agent.may_receive(), (maxnbr or 0))): addr = utils.int2ip(next(self.targiter)) with open(os.path.join(agent.get_local_path('input'), ('%s.%s' % (self.visiblecategory, addr))), 'w') as fdesc: fdesc.write(('%s\n' % addr))
'Feed periodically the agents affected to the `Campaign` (`self.agents`).'
def feedloop(self):
while True: for agent in self.agents: try: self.feed(agent, maxnbr=self.maxfeed) except StopIteration: return time.sleep(self.sleep)
'Prepare binary data. Subclasses may want to do some kind of conversion here.'
@staticmethod def _to_binary(data):
return data
'Executed before _addhost for host object post-treatment'
def _pre_addhost(self):
if ('cpes' in self._curhost): cpes = self._curhost['cpes'] self._curhost['cpes'] = list(viewvalues(cpes))
'Subclasses may store self._curhost here.'
def _addhost(self):
pass
'Subclasses may store self._curscan here.'
def _storescan(self):
pass
'Subclasses may add scan information (first argument) to self._curscan here.'
def _addscaninfo(self, _):
pass
'Adds the cpe in self._curdata to the host-wide cpe list, taking port/script/osmatch context into account.'
def _add_cpe_to_host(self):
cpe = self._curdata self._curdata = None path = None if (self._curport is not None): if ((self._curscript is not None) and ('id' in self._curscript)): path = ('ports{port:%s, scripts.id:%s}' % (self._curport['port'], self._curscript['id'])) else: path = ('ports...
'Save a clip\'s frame to an image file. Saves the frame of clip corresponding to time ``t`` in \'filename\'. ``t`` can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: \'01:03:05.35\'. If ``withmask`` is ``True`` the mask is saved in the alpha layer of the picture (only works with PN...
@convert_to_seconds(['t']) @convert_masks_to_RGB def save_frame(self, filename, t=0, withmask=True):
im = self.get_frame(t) if (withmask and (self.mask is not None)): mask = (255 * self.mask.get_frame(t)) im = np.dstack([im, mask]).astype('uint8') else: im = im.astype('uint8') imsave(filename, im)
'Write the clip to a videofile. Parameters filename Name of the video file to write in. The extension must correspond to the "codec" used (see below), or simply be \'.avi\' (which will work with any codec). fps Number of frames per second in the resulting video file. If None is provided, and the clip has an fps attribu...
@requires_duration @use_clip_fps_by_default @convert_masks_to_RGB def write_videofile(self, filename, fps=None, codec=None, bitrate=None, audio=True, audio_fps=44100, preset='medium', audio_nbytes=4, audio_codec=None, audio_bitrate=None, audio_bufsize=2000, temp_audiofile=None, rewrite_audio=True, remove_temp=True, wri...
(name, ext) = os.path.splitext(os.path.basename(filename)) ext = ext[1:].lower() if (codec is None): try: codec = extensions_dict[ext]['codec'][0] except KeyError: raise ValueError("MoviePy couldn't find the codec associated with the filename. ...
'Writes the videoclip to a sequence of image files. Parameters nameformat A filename specifying the numerotation format and extension of the pictures. For instance "frame%03d.png" for filenames indexed with 3 digits and PNG format. Also possible: "some_folder/frame%04d.jpeg", etc. fps Number of frames per second to con...
@requires_duration @use_clip_fps_by_default @convert_masks_to_RGB def write_images_sequence(self, nameformat, fps=None, verbose=True, withmask=True, progress_bar=True):
verbose_print(verbose, ('[MoviePy] Writing frames %s.' % nameformat)) tt = np.arange(0, self.duration, (1.0 / fps)) filenames = [] total = (int((self.duration / fps)) + 1) for (i, t) in tqdm(enumerate(tt), total=total, disable=(not progress_bar)): name = (nameformat % i) fil...
'Write the VideoClip to a GIF file. Converts a VideoClip into an animated GIF using ImageMagick or ffmpeg. Parameters filename Name of the resulting gif file. fps Number of frames per second (see note below). If it isn\'t provided, then the function will look for the clip\'s ``fps`` attribute (VideoFileClip, for instan...
@requires_duration @convert_masks_to_RGB def write_gif(self, filename, fps=None, program='imageio', opt='nq', fuzz=1, verbose=True, loop=0, dispose=False, colors=None, tempfiles=False):
if (program == 'imageio'): write_gif_with_image_io(self, filename, fps=fps, opt=opt, loop=loop, verbose=verbose, colors=colors) elif tempfiles: opt1 = opt if (opt1 == 'nq'): opt1 = 'optimizeplus' else: opt1 = 'OptimizeTransparency' write_gif_with_t...
'Apply a transformation to a part of the clip. Returns a new clip in which the function ``fun`` (clip->clip) has been applied to the subclip between times `ta` and `tb` (in seconds). Examples >>> # The scene between times t=3s and t=6s in ``clip`` will be >>> # be played twice slower in ``newclip`` >>> newclip = clip.s...
def subfx(self, fx, ta=0, tb=None, **kwargs):
left = (None if (ta == 0) else self.subclip(0, ta)) center = self.subclip(ta, tb).fx(fx, **kwargs) right = (None if (tb is None) else self.subclip(t_start=tb)) clips = [c for c in [left, center, right] if (c is not None)] from moviepy.video.compositing.concatenate import concatenate_videoclips r...
'Modifies the images of a clip by replacing the frame `get_frame(t)` by another frame, `image_func(get_frame(t))`'
def fl_image(self, image_func, apply_to=None):
if (apply_to is None): apply_to = [] return self.fl((lambda gf, t: image_func(gf(t))), apply_to)
'Returns the result of the blit of the clip\'s frame at time `t` on the given `picture`, the position of the clip being given by the clip\'s ``pos`` attribute. Meant for compositing.'
def blit_on(self, picture, t):
(hf, wf) = framesize = picture.shape[:2] if (self.ismask and (picture.max() != 0)): return np.minimum(1, (picture + self.blit_on(np.zeros(framesize), t))) ct = (t - self.start) img = self.get_frame(ct) mask = (None if (self.mask is None) else self.mask.get_frame(ct)) if (mask is not None...
'Add a mask VideoClip to the VideoClip. Returns a copy of the clip with a completely opaque mask (made of ones). This makes computations slower compared to having a None mask but can be useful in many cases. Choose Set ``constant_size`` to `False` for clips with moving image size.'
def add_mask(self):
if self.has_constant_size: mask = ColorClip(self.size, 1.0, ismask=True) return self.set_mask(mask.set_duration(self.duration)) else: make_frame = (lambda t: np.ones(self.get_frame(t).shape[:2], dtype=float)) mask = VideoClip(ismask=True, make_frame=make_frame) return sel...
'Place the clip on a colored background. Returns a clip made of the current clip overlaid on a color clip of a possibly bigger size. Can serve to flatten transparent clips. Parameters size Size (width, height) in pixels of the final clip. By default it will be the size of the current clip. color Background color of the...
def on_color(self, size=None, color=(0, 0, 0), pos=None, col_opacity=None):
from .compositing.CompositeVideoClip import CompositeVideoClip if (size is None): size = self.size if (pos is None): pos = 'center' colorclip = ColorClip(size, color) if (col_opacity is not None): colorclip = ColorClip(size, color, duration=self.duration).set_opacity(col_opac...
'Change the clip\'s ``get_frame``. Returns a copy of the VideoClip instance, with the make_frame attribute set to `mf`.'
@outplace def set_make_frame(self, mf):
self.make_frame = mf self.size = self.get_frame(0).shape[:2][::(-1)]
'Attach an AudioClip to the VideoClip. Returns a copy of the VideoClip instance, with the `audio` attribute set to ``audio``, which must be an AudioClip instance.'
@outplace def set_audio(self, audioclip):
self.audio = audioclip
'Set the clip\'s mask. Returns a copy of the VideoClip with the mask attribute set to ``mask``, which must be a greyscale (values in 0-1) VideoClip'
@outplace def set_mask(self, mask):
assert ((mask is None) or mask.ismask) self.mask = mask
'Set the opacity/transparency level of the clip. Returns a semi-transparent copy of the clip where the mask is multiplied by ``op`` (any float, normally between 0 and 1).'
@add_mask_if_none @outplace def set_opacity(self, op):
self.mask = self.mask.fl_image((lambda pic: (op * pic)))
'Set the clip\'s position in compositions. Sets the position that the clip will have when included in compositions. The argument ``pos`` can be either a couple ``(x,y)`` or a function ``t-> (x,y)``. `x` and `y` mark the location of the top left corner of the clip, and can be of several types. Examples >>> clip.set_pos(...
@apply_to_mask @outplace def set_position(self, pos, relative=False):
self.relative_pos = relative if hasattr(pos, '__call__'): self.pos = pos else: self.pos = (lambda t: pos)
'Returns an ImageClip made out of the clip\'s frame at time ``t``, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: \'01:03:05.35\'.'
@convert_to_seconds(['t']) def to_ImageClip(self, t=0, with_mask=True):
newclip = ImageClip(self.get_frame(t), ismask=self.ismask) if (with_mask and (self.mask is not None)): newclip.mask = self.mask.to_ImageClip(t) return newclip
'Return a mask a video clip made from the clip.'
def to_mask(self, canal=0):
if self.ismask: return self else: newclip = self.fl_image((lambda pic: ((1.0 * pic[:, :, canal]) / 255))) newclip.ismask = True return newclip
'Return a non-mask video clip made from the mask video clip.'
def to_RGB(self):
if self.ismask: f = (lambda pic: np.dstack((3 * [(255 * pic)])).astype('uint8')) newclip = self.fl_image(f) newclip.ismask = False return newclip else: return self
'Remove the clip\'s audio. Return a copy of the clip with audio set to None.'
@outplace def without_audio(self):
self.audio = None
'Transform the clip\'s audio. Return a new clip whose audio has been transformed by ``fun``.'
@outplace def afx(self, fun, *a, **k):
self.audio = self.audio.fx(fun, *a, **k)
'General transformation filter. Equivalent to VideoClip.fl . The result is no more an ImageClip, it has the class VideoClip (since it may be animated)'
def fl(self, fl, apply_to=None, keep_duration=True):
if (apply_to is None): apply_to = [] newclip = VideoClip.fl(self, fl, apply_to=apply_to, keep_duration=keep_duration) newclip.__class__ = VideoClip return newclip
'Image-transformation filter. Does the same as VideoClip.fl_image, but for ImageClip the tranformed clip is computed once and for all at the beginning, and not for each \'frame\'.'
@outplace def fl_image(self, image_func, apply_to=None):
if (apply_to is None): apply_to = [] arr = image_func(self.get_frame(0)) self.size = arr.shape[:2][::(-1)] self.make_frame = (lambda t: arr) self.img = arr for attr in apply_to: if hasattr(self, attr): a = getattr(self, attr) if (a is not None): ...
'Time-transformation filter. Applies a transformation to the clip\'s timeline (see Clip.fl_time). This method does nothing for ImageClips (but it may affect their masks or their audios). The result is still an ImageClip.'
@outplace def fl_time(self, time_func, apply_to=None, keep_duration=False):
if (apply_to is None): apply_to = ['mask', 'audio'] for attr in apply_to: if hasattr(self, attr): a = getattr(self, attr) if (a is not None): new_a = a.fl_time(time_func) setattr(self, attr, new_a)
'Returns the list of all valid entries for the argument of ``TextClip`` given (can be ``font``, ``color``, etc...)'
@staticmethod def list(arg):
popen_params = {'stdout': sp.PIPE, 'stderr': DEVNULL, 'stdin': DEVNULL} if (os.name == 'nt'): popen_params['creationflags'] = 134217728 process = sp.Popen([get_setting('IMAGEMAGICK_BINARY'), '-list', arg], **popen_params) result = process.communicate()[0] lines = result.splitlines() if (...
'Returns the of all valid entries which contain ``string`` for the argument ``arg`` of ``TextClip``, for instance >>> # Find all the available fonts which contain "Courier" >>> print ( TextClip.search(\'Courier\', \'font\') )'
@staticmethod def search(string, arg):
string = string.lower() names_list = TextClip.list(arg) return [name for name in names_list if (string in name.lower())]
'Returns a sequence of [(t1,t2), txt] covering all the given subclip from t_start to t_end. The first and last times will be cropped so as to be exactly t_start and t_end if possible.'
def in_subclip(self, t_start=None, t_end=None):
def is_in_subclip(t1, t2): try: return ((t_start <= t1 < t_end) or (t_start < t2 <= t_end)) except: return False def try_cropping(t1, t2): try: return (max(t1, t_start), min(t2, t_end)) except: return (t1, t2) return [(try_cropp...
'Returns a FramesMatches object obtained by filtering out the FramesMatch which do not satistify the condition ``cond``. ``cond`` is a function (FrameMatch -> bool). Examples >>> # Only keep the matches corresponding to (> 1 second) sequences. >>> new_matches = matches.filter( lambda match: match.time_span > 1)'
def filter(self, cond):
return FramesMatches(filter(cond, self))
'Loads a FramesMatches object from a file. >>> matching_frames = FramesMatches.load("somefile")'
@staticmethod def load(filename):
arr = np.loadtxt(filename) mfs = [FramesMatch(*e) for e in arr] return FramesMatches(mfs)
'Finds all the frames tht look alike in a clip, for instance to make a looping gif. This teturns a FramesMatches object of the all pairs of frames with (t2-t1 < max_d) and whose distance is under dist_thr. This is well optimized routine and quite fast. Examples We find all matching frames in a given video and turn the...
@staticmethod def from_clip(clip, dist_thr, max_d, fps=None):
N_pixels = ((clip.w * clip.h) * 3) dot_product = (lambda F1, F2: ((F1 * F2).sum() / N_pixels)) F = {} def distance(t1, t2): uv = dot_product(F[t1]['frame'], F[t2]['frame']) (u, v) = (F[t1]['|F|sq'], F[t2]['|F|sq']) return np.sqrt(((u + v) - (2 * uv))) matching_frames = [] ...
'match_thr The smaller, the better-looping the gifs are. min_time_span Only GIFs with a duration longer than min_time_span (in seconds) will be extracted. nomatch_thr If None, then it is chosen equal to match_thr'
def select_scenes(self, match_thr, min_time_span, nomatch_thr=None, time_distance=0):
if (nomatch_thr is None): nomatch_thr = match_thr dict_starts = defaultdict((lambda : [])) for (start, end, d_min, d_max) in self: dict_starts[start].append([end, d_min, d_max]) starts_ends = sorted(dict_starts.items(), key=(lambda k: k[0])) result = [] min_start = 0 for (sta...