U
    a+dy                     @   s\   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 G dd dZ
e	dddZdS )    N)DEVNULL)get_setting)requires_durationc                   @   sB   e Zd ZdZdddZdd Zd	d
 Zdd Zdd Zdd Z	dS )FFMPEG_AudioWritera+  
    A class to write an AudioClip into an audio file.

    Parameters
    ------------

    filename
      Name of any video or audio file, like ``video.mp4`` or ``sound.wav`` etc.

    size
      Size (width,height) in pixels of the output video.

    fps_input
      Frames per second of the input audio (given by the AUdioClip being
      written down).

    codec
      Name of the ffmpeg codec to use for the output.

    bitrate:
      A string indicating the bitrate of the final video. Only
      relevant for codecs which accept a bitrate.

       
libfdk_aacNc
                 C   s   || _ || _|d krtj}tddd|tjkr2dndddd|  d	d
d|  dd| dd| ddg|d krpdgn
d|ddg d	|g dd| g ddg |d k	rd|gng  |	r|	ng  |g }
t|tjd}tjdkrd|d< tj|
f|| _	d S )NZFFMPEG_BINARYz-yz	-loglevelerrorinfoz-fzs%dle   z-acodecz	pcm_s%dlez-arz%dz-acz-i-z-vnz-vcodeccopyz-strictz-2z-ab)stdoutstderrstdinnti   creationflags)
filenamecodecspPIPEr   r   osnamePopenproc)selfr   Z	fps_inputnbytes	nchannelsr   bitrateZinput_videologfileffmpeg_paramscmdZpopen_params r!   G/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/audio/io/ffmpeg_audiowriter.py__init__%   sN      
 
   	



zFFMPEG_AudioWriter.__init__c              
   C   s   zBz| j j|  W n& tk
r>   | j j|  Y nX W n tk
r } z| j j }t	|d| j
 d t	|  }d|kr|d| j  }n8d|kr|d| j| jf  }nd|kr|d }n|d	 }t|W 5 d }~X Y nX d S )
NzN

MoviePy error: FFMPEG encountered the following error while writing file %s:z

s   Unknown encodera  

The audio export failed because FFMPEG didn't find the specified codec for audio encoding (%s). Please install this codec or change the codec when calling to_videofile or to_audiofile. For instance for mp3:
   >>> to_videofile('myvid.mp4', audio_codec='libmp3lame')s   incorrect codec parameters ?z

The audio export failed, possibly because the codec specified for the video (%s) is not compatible with the given extension (%s). Please specify a valid 'codec' argument in to_videofile. This would be 'libmp3lame' for mp3, 'libvorbis' for ogg...s   encoder setup failedzq

The audio export failed, possily because the bitrate you specified was two high or too low for the video codec.z

In case it helps, make sure you are using a recent version of FFMPEG (the versions in the Ubuntu/Debian repos are deprecated).)r   r   writetobytes	NameErrortostringIOErrorr   readstrr   r   ext)r   Zframes_arrayerrZffmpeg_errorr   r!   r!   r"   write_framesG   sD    	
zFFMPEG_AudioWriter.write_framesc                 C   sX   t | drT| jrT| jj  d | j_| jjd k	rD| jj  d | j_| j  d | _d S )Nr   )hasattrr   r   closer   Zstdeewaitr   r!   r!   r"   r/   w   s    
zFFMPEG_AudioWriter.closec                 C   s   |    d S Nr/   r1   r!   r!   r"   __del__   s    zFFMPEG_AudioWriter.__del__c                 C   s   | S r2   r!   r1   r!   r!   r"   	__enter__   s    zFFMPEG_AudioWriter.__enter__c                 C   s   |    d S r2   r3   )r   exc_type	exc_value	tracebackr!   r!   r"   __exit__   s    zFFMPEG_AudioWriter.__exit__)r   r   r   NNNN)
__name__
__module____qualname____doc__r#   r-   r/   r4   r5   r9   r!   r!   r!   r"   r      s              
"0r   	libvorbisFTbarc              
   C   s   |rt |d d}nd}t|
}
|
d| d t|||| j||||	d}| j|d|||
dD ]}|| q^|  |r|  |
d	d dS )
z}
    A function that wraps the FFMPEG_AudioWriter to write an AudioClip
    to a file.

    NOTE: verbose is deprecated.
    z.logzw+NzMoviePy - Writing audio in %s)message)r   r   r   r   T)	chunksizequantizer   fpsloggerzMoviePy - Done.)openproglogZdefault_bar_loggerr   r   Ziter_chunksr-   r/   )Zclipr   rC   r   Z
buffersizer   r   Zwrite_logfileverboser   rD   r   writerchunkr!   r!   r"   ffmpeg_audiowrite   s,    
  
rJ   )r>   NFTNr?   )r   
subprocessr   rF   Zmoviepy.compatr   Zmoviepy.configr   Zmoviepy.decoratorsr   r   rJ   r!   r!   r!   r"   <module>   s             