
    gZ*                     v    d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
  G d d      Z	 	 	 	 	 	 	 	 	 d
dZdd	Zy)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                   B    e Zd ZdZ	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Zy)	FFMPEG_VideoWritera  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.

    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                 v   |	t         j                  }	|	| _        || _        || _        | j                  j                  d      d   | _        |rdnd}t        dd|	t         j                  k(  rdndd	d
dd
dd|d   |d   fz  d|dd|z  dddg}||j                  d|ddg       |j                  d|d|g       ||j                  |       ||j                  d|g       |
|j                  dt        |
      g       |dk(  r)|r'|j                  ddg       |j                  ddg       n.|dk(  r)|d   d z  dk(  r|d   d z  dk(  r|j                  ddg       |j                  t        |      g       t        t         j                  |	t         j                  d!      }t        j                  |fi || _        y )"N.rgbargb24-yz	-loglevelerrorinfo-frawvideoz-vcodec-s%dx%dr      -pix_fmtz-rz%.02fz-an-i-z-acodeccopyz-presetz-bz-threadslibvpxyuva420pz-auto-alt-ref0libx264   stdoutstderrstdin)spPIPElogfilefilenamecodecsplitextr   extendstrr   r   DEVNULLPopenproc)selfr&   sizefpsr'   	audiofilepresetbitrate	with_maskr%   threadsffmpeg_paramspixel_formatcmdpopen_paramss                  g/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/video/io/ffmpeg_writer.py__init__zFFMPEG_VideoWriter.__init__M   s    ?ggG 
==&&s+B/!*v "'')GvtAwQ((cM#
&  JJiF;<

Iui89$JJ}%JJg'JJ
CL12 HJJ
J/0JJ-.y tAw{a'7d1gkQ>NJJ
J/0

*84562zzWrwwG
 HHS1L1	    c                 r   	 | j                   j                  j                  |j                                y# t        $ r}| j                   j                         \  }}||j                         }n5| j                  j                  d       | j                  j                         }| d| j                   d| }d|v r|d| j                   dz  }t	        |      d|v r+|d	| j                   d
| j                   dz  }t	        |      d|v r|dz  }t	        |      d|v r|dz  }t	        |      d}~ww xY w)zWrites one frame in the file.Nr   K

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

 zUnknown encoderz\

The video export failed because FFMPEG didn't find the specified codec for video encoding z. Please install this codec or change the codec when calling write_videofile.
For instance:
  >>> clip.write_videofile('myvid.webm', codec='libvpx')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)r.   r"   writetobytesIOErrorcommunicatedecoder%   seekreadr&   r'   r)   )r/   	img_arrayerr_ffmpeg_errorr   s         r;   write_framezFFMPEG_VideoWriter.write_frame   s{   4	!IIOO!!)"3"3"56 2	!"ii335OA|'+224 !!!$#||002 %   $f\ND 
 !L0::>** FOOD %. 5 0<?//3zzl ;++/88* 5#	#2 %.  )L8M %.  (<7O
 %. e2	!s   36 	D6C2D11D6c                    | j                   r| j                   j                  j                          | j                   j                  $| j                   j                  j                          | j                   j	                          d| _         yy)z@Closes the writer, terminating the subprocess if is still alive.N)r.   r"   closer!   waitr/   s    r;   rN   zFFMPEG_VideoWriter.close   s]    99IIOO!!#yy+		  &&(IINNDI r=   c                     | S N rP   s    r;   	__enter__zFFMPEG_VideoWriter.__enter__   s    r=   c                 $    | j                          y rR   )rN   )r/   exc_type	exc_value	tracebacks       r;   __exit__zFFMPEG_VideoWriter.__exit__   s    

r=   )	r   NmediumNFNNNN)	__name__
__module____qualname____doc__r<   rL   rN   rT   rY   rS   r=   r;   r   r      sA    ;D G2R6!pr=   r   c                 L   t        j                  |
      }
|rt        |dz   d      }nd} |
d|z         | j                  du}t	        || j
                  |||||||||	|      5 }| j                  |
d|d	      D ]w  \  }}| j                  Ud
| j                  j                  |      z  }|j                  dk7  r|j                  d      }t        j                  ||g      }|j                  |       y 	 ddd       |r|j                           |
d       y# 1 sw Y   %xY w)zdWrite the clip to a videofile. See VideoClip.write_videofile for details
    on the parameters.
    .logw+NzMoviePy - Writing video %s
)message)	r'   r3   r4   r5   r%   r2   r6   r7   r8   Tuint8)logger
with_timesr1   dtype   zMoviePy - Done !)r   default_bar_loggeropenmaskr   r0   iter_frames	get_framerf   astypenpdstackrL   rN   )clipr&   r1   r'   r4   r3   write_logfiler2   r6   r7   rd   r8   r%   has_maskwritertframerj   s                     r;   ffmpeg_write_videorv      s5   " ''/Fx&($/
1H<=yy$H			#!
 & 
((d7 ) 
 		&HAu yy$TYY0033::(;;w/D		5$-0u%		&&2 
%&7& &s   %BDD#c                 D   |j                   dk7  r|j                  d      }|s|j                  d   dk(  rdnd}t        ddd|j                  d	d d	d	d
   z  ddd|ddt	        |       g}|rt        | dz   d      }nt        j                  }t        t        j                  |t        j                  d      }t        j                  |fi |}|j                  |j                               \  }}	|j                  r&|	 d|  d| d|	j                          }
t        |
      ~y	)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"``).
    rc   r      r   r   r   r   r   Nr   r   r   r   r   r   r`   ra   r   r?   z with command r@   )rf   rm   shaper   r   ri   r#   r$   r   r,   r-   rD   rB   
returncoderE   rC   )r&   imager%   r8   r9   log_filer:   r.   outrI   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r=   )	r   NrZ   FNNNbarN)FN)r^   
subprocessr#   numpyrn   r   moviepy.configr   moviepy.toolsr   r   r   rv   r~   rS   r=   r;   <module>r      sP   
    ( MO Ol 7't=r=   