
    W|h,                     |    S r SSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
   " S S5      r          S
S jrSS	 jrg)zW
On the long term this will implement several methods to make videos
out of VideoClips
    N)proglog)FFMPEG_BINARY)cross_platform_popen_paramsffmpeg_escape_filenamec                   N    \ rS rSrSr          S
S jrS rS rS rS r	S	r
g)FFMPEG_VideoWriter   a  A class for FFMPEG-based video writing.

Parameters
----------

filename : str
  Any filename like ``"video.mp4"`` etc. but if you want to avoid
  complications it is recommended to use the generic extension ``".avi"``
  for all your videos.

size : tuple or list
  Size of the output video in pixels (width, height).

fps : int
  Frames per second in the output video file.

codec : str, optional
  FFMPEG codec. It seems that in terms of quality the hierarchy is
  'rawvideo' = 'png' > 'mpeg4' > 'libx264'
  'png' manages the same lossless quality as 'rawvideo' but yields
  smaller files. Type ``ffmpeg -codecs`` in a terminal to get a list
  of accepted codecs.

  Note for default 'libx264': by default the pixel format yuv420p
  is used. If the video dimensions are not both even (e.g. 720x405)
  another pixel format is used, and this can cause problem in some
  video readers.

audiofile : str, optional
  The name of an audio file that will be incorporated to the video.

audio_codec : str, optional
  FFMPEG audio codec. If None, ``"copy"`` codec is used.

preset : str, optional
  Sets the time that FFMPEG will take to compress the video. The slower,
  the better the compression rate. Possibilities are: ``"ultrafast"``,
  ``"superfast"``, ``"veryfast"``, ``"faster"``, ``"fast"``,  ``"medium"``
  (default), ``"slow"``, ``"slower"``, ``"veryslow"``, ``"placebo"``.

bitrate : str, optional
  Only relevant for codecs which accept a bitrate. "5000k" offers
  nice results in general.

with_mask : bool, optional
  Set to ``True`` if there is a mask in the video to be encoded.

pixel_format : str, optional
  Optional: Pixel format for the output video file. If is not specified
  ``"rgb24"`` will be used as the default format unless ``with_mask`` is
  set as ``True``, then ``"rgba"`` will be used.

logfile : int, optional
  File descriptor for logging output. If not defined, ``subprocess.PIPE``
  will be used. Defined using another value, the log level of the ffmpeg
  command will be "info", otherwise "error".

threads : int, optional
  Number of threads used to write the output with ffmpeg.

ffmpeg_params : list, optional
  Additional parameters passed to ffmpeg command.
Nc                    U
c  [         R                  n
Xl        Xl        X@l        X`l        U R                  R                  S5      S   U l        U	(       a  SOSn[        SSU
[         R                  :X  a  SOSS	S
SS
SSUS   US   4-  SUSSU-  SSS/nUb  Uc  SnUR                  SUSU/5        US:X  a  UR                  SU/5        OUR                  SU/5        UR                  SU/5        Ub  UR                  U5        Ub  UR                  SU/5        Ub  UR                  S[        U5      /5        US:X  a.  U	(       a'  UR                  SS/5        UR                  SS /5        O7US!:X  d  US:X  a+  US   S"-  S:X  a  US   S"-  S:X  a  UR                  SS/5        UR                  [        U5      /5        [        [         R                  U
[         R                  S#.5      n[         R                  " U40 UD6U l        g )$N.rgbargb24-yz	-loglevelerrorinfo-frawvideoz-vcodec-s%dx%dr      -pix_fmtz-rz%.02fz-an-i-copyz-acodec
h264_nvencz-c:vz-presetz-bz-threadslibvpxyuva420pz-auto-alt-ref0libx264   stdoutstderrstdin)spPIPElogfilefilenamecodecaudio_codecsplitextr   extendstrr   r   DEVNULLPopenproc)selfr(   sizefpsr)   	audiofiler*   presetbitrate	with_maskr'   threadsffmpeg_paramspixel_formatcmdpopen_paramss                   X/home/james-whalen/.local/lib/python3.13/site-packages/moviepy/video/io/ffmpeg_writer.py__init__FFMPEG_VideoWriter.__init__P   s     ?ggG 
&==&&s+B/!*v "'')GvtAwQ((cM#
&  "$JJiK@AL JJ'JJ	5)*

Iv&'$JJ}%JJg'JJ
CL12 HJJ
J/0JJ-.i5L#8a1!a1!JJ
J/0

*84562zzWrwwG
 HHS1L1	    c                 8    U R                   R                  R                  UR                  5       5        g! [         a  nU R                   R                  5       u  p4Ub  UR                  5       nO5U R                  R                  S5        U R                  R                  5       nU SU R                   SU 3nSU;   d  SU;   a  US-  nO>SU;   a!  US	U R                   S
U R                   S3-  nOSU;   a  US-  nOSU;   a  US-  n[	        U5      eSnAff = f)zWrites one frame in the file.Nr   K

MoviePy error: FFMPEG encountered the following error while writing file :

 zUnknown encoderzUnknown decodera  

The video export failed because FFMPEG didn't find the specified codec for video or audio. Please install this codec or change the codec when calling write_videofile.
For instance:
  >>> clip.write_videofile('myvid.webm', audio='myaudio.mp3', codec='libvpx', audio_codec='aac')zincorrect codec parameters ?zN

The video export failed, possibly because the codec specified for the video z, is not compatible with the given extension aO  .
Please specify a valid 'codec' argument in write_videofile.
This would be 'libx264' or 'mpeg4' for mp4, 'libtheora' for ogv, 'libvpx for webm.
Another possible reason is that the audio codec was not compatible with the video codec. For instance, the video extensions 'ogv' and 'webm' only allow 'libvorbis' (default) as avideo codec.zbitrate not specifiedzn

The video export failed, possibly because the bitrate specified was too high or too low for the video codec.zInvalid encoder typezd

The video export failed because the codec or file extension you provided is not suitable for video)r1   r$   writetobytesIOErrorcommunicatedecoder'   seekreadr(   r)   r,   )r2   	img_arrayerr_ffmpeg_errorr   s         r>   write_frameFFMPEG_VideoWriter.write_frame   sF   5	!IIOO!!)"3"3"56 3	!"ii335OA'+224 !!!$#||002 %   $f\ND 
 !L04E4U9 0<?//3zzl ;++/88* 5#	# )L8M
 (<7O
 %. g3	!s   36 
D CDDc                 (   U R                   (       a  U R                   R                  R                  5         U R                   R                  b$  U R                   R                  R                  5         U R                   R	                  5         SU l         gg)z@Closes the writer, terminating the subprocess if is still alive.N)r1   r$   closer#   waitr2   s    r>   rS   FFMPEG_VideoWriter.close   s]    99IIOO!!#yy+		  &&(IINNDI rA   c                     U $ N rU   s    r>   	__enter__FFMPEG_VideoWriter.__enter__   s    rA   c                 $    U R                  5         g rX   )rS   )r2   exc_type	exc_value	tracebacks       r>   __exit__FFMPEG_VideoWriter.__exit__   s    

rA   )r*   r)   r,   r(   r'   r1   )
r   NNmediumNFNNNN)__name__
__module____qualname____firstlineno____doc__r?   rP   rS   rZ   r`   __static_attributes__rY   rA   r>   r   r      sD    >J T2l7!rrA   r   c                 ^   [         R                  " U5      nU(       a  [        US-   S5      nOSnU" SU-  S9  U R                  SLn[	        UU R
                  UUUUUUUUU	U
US9 nU R                  USUSS	9 H{  u  nnU R                  bW  S
U R                  R                  U5      -  nUR                  S:w  a  UR                  S5      n[        R                  " UU/5      nUR                  U5        M}     SSS5        U(       a  UR                  5         U" SS9  g! , (       d  f       N,= f)z\Write the clip to a videofile. See VideoClip.write_videofile for details
on the parameters.
.logw+NzMoviePy - Writing video %s
)message)
r)   r6   r7   r8   r'   r5   r*   r9   r:   r;   Tuint8)logger
with_timesr4   dtype   zMoviePy - Done !)r   default_bar_loggeropenmaskr   r3   iter_frames	get_framerp   astypenpdstackrP   rS   )clipr(   r4   r)   r7   r6   write_logfiler5   r*   r9   r:   rn   r;   r'   has_maskwritertframert   s                      r>   ffmpeg_write_videor      s.   $ ''/Fx&($/
1H<=yy$H			#!
 
((d7 ) 
HAu yy$TYY0033::(;;w/D		5$-0u%

4 
%&9
 
s   &BD
D,c                 f   UR                   S:w  a  UR                  S5      nU(       d  UR                  S   S:X  a  SOSn[        SSSUR                  S	S S	S	S
2   -  SSSUSS[	        U 5      /nU(       a  [        U S-   S5      nO[        R                  n[        [        R                  U[        R                  S.5      n[        R                  " U40 UD6nUR                  UR                  5       5      u  pUR                  (       a&  U	 SU  SU SU	R                  5        3n
[        U
5      eAg	)a  Writes an image (HxWx3 or HxWx4 numpy array) to a file, using ffmpeg.

Parameters
----------

filename : str
    Path to the output file.

image : np.ndarray
    Numpy array with the image data.

logfile : bool, optional
    Writes the ffmpeg output inside a logging file (``True``) or not
    (``False``).

pixel_format : str, optional
    Pixel format for ffmpeg. If not defined, it will be discovered checking
    if the image data contains an alpha channel (``"rgba"``) or not
    (``"rgb24"``).
rm   r       r   r   r   r   r   Nr   r   r   r   r   r   rj   rk   r!   rC   z with command rD   )rp   rw   shaper   r   rs   r%   r&   r   r/   r0   rH   rF   
returncoderI   rG   )r(   imager'   r;   r<   log_filer=   r1   outrM   r   s              r>   ffmpeg_write_imager   .  s(   * {{gW%"'++a.A"5vG 	5;;r?4R4()x(C 6)4077.::BGGDL 88C(<(D0HCe $:^C5szz|nN 	
 enrA   )
r   Nrb   FNNNNbarN)FN)rg   
subprocessr%   numpyrx   r   moviepy.configr   moviepy.toolsr   r   r   r   r   rY   rA   r>   <module>r      sS   
    ( M` `N 9'x=rA   