U
    Eh\1                     @   s.  d dl mZ d dlmZ ddlmZ ddlmZmZ d dl	m
Z
 d dlZdd	 Zd
d ZG dd de
Zdd Zdd Zdd ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZehdfd$d%Zd*d&d'Zd+d(d)ZdgZdS ),    )unicode_literals)
basestring   )KwargReprNode)escape_charsget_hash_int)objectNc                 C   s$   d}|D ]}t | |rd} q q|S )NFT)
isinstance)objtypesZvalidZstream_type r   >/var/www/html/venv/lib/python3.8/site-packages/ffmpeg/nodes.py_is_of_types
   s    
r   c                 C   s   d dd | D S )Nz, c                 S   s   g | ]}d  |j|jqS )z{}.{})format
__module____name__.0xr   r   r   
<listcomp>   s     z"_get_types_str.<locals>.<listcomp>)join)r   r   r   r   _get_types_str   s    r   c                   @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Ze	dd Z
e	dd ZdS )Streamz^Represents the outgoing edge of an upstream node; may be used to create more downstream nodes.Nc                 C   s8   t ||s"tdt|t||| _|| _|| _d S )NzHExpected upstream node to be of one of the following type(s): {}; got {})r   	TypeErrorr   r   typenodelabelselector)selfupstream_nodeupstream_labelZ
node_typesupstream_selectorr   r   r   __init__   s    
 zStream.__init__c                 C   s   t t| jt| jgS N)r   hashr   r   r   r   r   r   __hash__'   s    zStream.__hash__c                 C   s   t | t |kS r#   )r$   )r   otherr   r   r   __eq__*   s    zStream.__eq__c                 C   s>   | j jdd}d}| jr$d| j}d|| j|| j j}|S )NF)Zinclude_hash z:{}z{}[{!r}{}] <{}>)r   Z	long_reprr   r   r   Z
short_hash)r   Z	node_reprr   outr   r   r   __repr__-   s       zStream.__repr__c                 C   sD   | j dk	rtd| nt|ts2td|| jj| j|dS )a  
        Select a component (audio, video) of the stream.

        Example:
            Process the audio and video portions of a stream independently::

                input = ffmpeg.input('in.mp4')
                audio = input['a'].filter("aecho", 0.8, 0.9, 1000, 0.3)
                video = input['v'].hflip()
                out = ffmpeg.output(audio, video, 'out.mp4')
        Nz!Stream already has a selector: {}z*Expected string index (e.g. 'a'); got {!r}r   r   )	r   
ValueErrorr   r	   r   r   r   streamr   )r   indexr   r   r   __getitem__7   s
    

zStream.__getitem__c                 C   s   | d S )a  Select the audio-portion of a stream.

        Some ffmpeg filters drop audio streams, and care must be taken
        to preserve the audio in the final output.  The ``.audio`` and
        ``.video`` operators can be used to reference the audio/video
        portions of a stream so that they can be processed separately
        and then re-combined later in the pipeline.  This dilemma is
        intrinsic to ffmpeg, and ffmpeg-python tries to stay out of the
        way while users may refer to the official ffmpeg documentation
        as to why certain filters drop audio.

        ``stream.audio`` is a shorthand for ``stream['a']``.

        Example:
            Process the audio and video portions of a stream independently::

                input = ffmpeg.input('in.mp4')
                audio = input.audio.filter("aecho", 0.8, 0.9, 1000, 0.3)
                video = input.video.hflip()
                out = ffmpeg.output(audio, video, 'out.mp4')
        ar   r%   r   r   r   audioI   s    zStream.audioc                 C   s   | d S )a  Select the video-portion of a stream.

        Some ffmpeg filters drop audio streams, and care must be taken
        to preserve the audio in the final output.  The ``.audio`` and
        ``.video`` operators can be used to reference the audio/video
        portions of a stream so that they can be processed separately
        and then re-combined later in the pipeline.  This dilemma is
        intrinsic to ffmpeg, and ffmpeg-python tries to stay out of the
        way while users may refer to the official ffmpeg documentation
        as to why certain filters drop audio.

        ``stream.video`` is a shorthand for ``stream['v']``.

        Example:
            Process the audio and video portions of a stream independently::

                input = ffmpeg.input('in.mp4')
                audio = input.audio.filter("aecho", 0.8, 0.9, 1000, 0.3)
                video = input.video.hflip()
                out = ffmpeg.output(audio, video, 'out.mp4')
        vr   r%   r   r   r   videob   s    zStream.video)N)r   r   __qualname____doc__r"   r&   r(   r+   r0   propertyr2   r4   r   r   r   r   r      s    


r   c                 C   sP   | d kri }n>t | tr"d | i}n*t | ttfr>tt| }nt | trL| }|S r#   )r	   r   listtupledict	enumeratestream_spec
stream_mapr   r   r   get_stream_map|   s    


r?   c                 C   sB   g }t |  D ],}t|ts0tdt|||j q|S )NzExpected Stream; got {})	r8   valuesr	   r   r   r   r   appendr   )r>   Znodesr.   r   r   r   get_stream_map_nodes   s    
rB   c                 C   s   t | }t|S r#   )r?   rB   r<   r   r   r   get_stream_spec_nodes   s    rC   c                       s\   e Zd ZdZedd Zedd Zedd Zg i f fdd		ZdddZ	dd Z
  ZS )Nodez	Node basec                 C   sV   |d k	r*t ||k r*td|t |n(|d k	rRt ||krRtd|t |d S )Nz,Expected at least {} input stream(s); got {}z+Expected at most {} input stream(s); got {})lenr-   r   )clsr>   
min_inputs
max_inputsr   r   r   Z__check_input_len   s      zNode.__check_input_lenc                 C   s8   t | D ]&}t||stdt|t|qd S )NzKExpected incoming stream(s) to be of one of the following types: {}; got {})r8   r@   r   r   r   r   r   )rF   r>   incoming_stream_typesr.   r   r   r   Z__check_input_types   s    
 zNode.__check_input_typesc                 C   s2   i }t | D ]\}}|j|j|jf||< q|S r#   )r8   itemsr   r   r   )rF   r>   incoming_edge_mapZdownstream_labelZupstreamr   r   r   Z__get_incoming_edge_map   s    
zNode.__get_incoming_edge_mapc	                    sR   t |}	| |	|| | |	| | |	}
tt| |
||| || _|| _d S r#   )	r?   _Node__check_input_len_Node__check_input_types_Node__get_incoming_edge_mapsuperrD   r"   _Node__outgoing_stream_typeZ_Node__incoming_stream_types)r   r=   namerI   outgoing_stream_typerG   rH   argskwargsr>   rK   	__class__r   r   r"      s    
zNode.__init__Nc                 C   s   | j | ||dS )z|Create an outgoing stream originating from this node.

        More nodes may be attached onto the outgoing stream.
        r!   )rP   )r   r   r   r   r   r   r.      s    zNode.streamc                 C   s,   t |tr| j|j|jdS | j|dS dS )aj  Create an outgoing stream originating from this node; syntactic sugar for ``self.stream(label)``.
        It can also be used to apply a selector: e.g. ``node[0:'a']`` returns a stream with label 0 and
        selector ``'a'``, which is the same as ``node.stream(label=0, selector='a')``.

        Example:
            Process the audio and video portions of a stream independently::

                input = ffmpeg.input('in.mp4')
                audio = input[:'a'].filter("aecho", 0.8, 0.9, 1000, 0.3)
                video = input[:'v'].hflip()
                out = ffmpeg.output(audio, video, 'out.mp4')
        r,   )r   N)r	   slicer.   startstop)r   itemr   r   r   r0      s    
zNode.__getitem__)NN)r   r   r5   r6   classmethodrL   rM   rN   r"   r.   r0   __classcell__r   r   rU   r   rD      s   

	

rD   c                       s   e Zd Zd fdd	Z  ZS )FilterableStreamNc                    s   t t| ||tth| d S r#   )rO   r^   r"   	InputNode
FilterNoder   r   r    r!   rU   r   r   r"      s    
   zFilterableStream.__init__)Nr   r   r5   r"   r]   r   r   rU   r   r^      s   r^   c                       s2   e Zd ZdZg i f fdd	Zedd Z  ZS )r_   zInputNode typec              
      s$   t t| jd |i tdd||d d S )Nr   r=   rQ   rI   rR   rG   rH   rS   rT   )rO   r_   r"   r^   )r   rQ   rS   rT   rU   r   r   r"      s    
zInputNode.__init__c                 C   s   t j| jd S NfilenameospathbasenamerT   r%   r   r   r   
short_repr  s    zInputNode.short_repr)r   r   r5   r6   r"   r7   rj   r]   r   r   rU   r   r_      s   r_   c                       s,   e Zd Zdg i f fdd	Zdd Z  ZS )r`   r   c              
      s&   t t| j||thtd|||d d S Nr   rc   )rO   r`   r"   r^   )r   r=   rQ   rH   rS   rT   rU   r   r   r"   	  s    
zFilterNode.__init__c                    s   | j }| j}| jdkr t|g}dd |D }i  t| D ]$\}}t|d}t|d}| |< q>dd |D } fddt D }|| }	t| jd}
|	r|
dd	|	7 }
t|
d	S )
N)splitZasplitc                 S   s   g | ]}t |d qS \'=:r   r   r   r   r   r     s     z*FilterNode._get_filter.<locals>.<listcomp>rn   c                 S   s   g | ]}t |d qS rm   ro   )r   r3   r   r   r   r   $  s     c                    s   g | ]}d  | | qS )z{}={})r   )r   kZ
out_kwargsr   r   r   %  s     z={}:z\'[],;)
rS   rT   rQ   rE   r8   rJ   r   sortedr   r   )r   Zoutgoing_edgesrS   rT   Zout_argsrp   r3   Z
arg_paramsZkwarg_paramsparamsZparams_textr   rq   r   _get_filter  s"    




zFilterNode._get_filter)r   r   r5   r"   ru   r]   r   r   rU   r   r`     s   r`   c                       s.   e Zd Zg i f fdd	Zedd Z  ZS )
OutputNodec              
      s&   t t| j||thtdd ||d d S rk   )rO   rv   r"   r^   OutputStreamr   r.   rQ   rS   rT   rU   r   r   r"   1  s    
zOutputNode.__init__c                 C   s   t j| jd S rd   rf   r%   r   r   r   rj   =  s    zOutputNode.short_repr)r   r   r5   r"   r7   rj   r]   r   r   rU   r   rv   0  s   rv   c                       s   e Zd Zd fdd	Z  ZS )rw   Nc                    s"   t t| j||ttth|d d S )NrW   )rO   rw   r"   rv   
GlobalNodeMergeOutputsNodera   rU   r   r   r"   C  s    
zOutputStream.__init__)Nrb   r   r   rU   r   rw   B  s   rw   c                       s   e Zd Z fddZ  ZS )rz   c                    s"   t t| j||thtdd d d S )Nr   )r=   rQ   rI   rR   rG   rH   )rO   rz   r"   rw   )r   streamsrQ   rU   r   r   r"   N  s    
zMergeOutputsNode.__init__rb   r   r   rU   r   rz   M  s   rz   c                       s"   e Zd Zg i f fdd	Z  ZS )ry   c              
      s&   t t| j||thtdd||d d S rk   )rO   ry   r"   rw   rx   rU   r   r   r"   [  s    
zGlobalNode.__init__rb   r   r   rU   r   ry   Z  s   ry   c                    s    fdd}|S )Nc                    s"   p j  fddD   S )Nc                    s   g | ]}t | qS r   )setattr)r   Zstream_classfunc	func_namer   r   r   k  s     z6stream_operator.<locals>.decorator.<locals>.<listcomp>)r   )r~   rQ   stream_classesr}   r   	decoratori  s    
z"stream_operator.<locals>.decoratorr   )r   rQ   r   r   r   r   stream_operatorh  s    r   c                 C   s   t th| dS N)r   rQ   )r   r^   rQ   r   r   r   filter_operatorq  s    r   c                 C   s   t th| dS r   )r   rw   r   r   r   r   output_operatoru  s    r   )N)N)
__future__r   Zpast.builtinsr   Zdagr   Z_utilsr   r   builtinsr   rg   r   r   r   r?   rB   rC   rD   r^   r_   r`   rv   rw   rz   ry   r   r   r   __all__r   r   r   r   <module>   s.   	e	U(	

