
    Vji"                     d    d dl mZ d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z
  G d d          ZdS )    )Path)AnyDictOptionalUnionN)Tensor)_corec                      e Zd ZdZdedefdZdddddddeee	f         de
e         d	e
e         d
e
eeef                  de
eeef                  de
eeef                  ddfdZdddddddede
e         d	e
e         d
e
eeef                  de
eeef                  de
eeef                  defdZdddddddede
e         d	e
e         d
e
eeef                  de
eeef                  de
eeef                  ddfdZdS )VideoEncodera  A video encoder.

    Args:
        frames (``torch.Tensor``): The frames to encode. This must be a 4D
            tensor of shape ``(N, C, H, W)`` where N is the number of frames,
            C is 3 channels (RGB), H is height, and W is width.
            Values must be uint8 in the range ``[0, 255]``.
        frame_rate (float): The frame rate of the **input** ``frames``. Also defines the encoded **output** frame rate.
    frames
frame_ratec                   t           j                            d           t          |t                    s t          dt          |          d          |j        dk    rt          d|j        d          |j	        t           j
        k    rt          d|j	        d          |dk    rt          d|d	          || _        || _        d S )
Nz torchcodec.encoders.VideoEncoderz3Expected frames to be a Tensor, got type(frames) = .   z'Expected 4D frames, got frames.shape = z*Expected uint8 frames, got frames.dtype = r   zframe_rate = z must be > 0.)torch_C_log_api_usage_once
isinstancer   
ValueErrortypendimshapedtypeuint8_frames_frame_rate)selfr   r   s      \/root/voice-cloning/.venv/lib/python3.11/site-packages/torchcodec/encoders/_video_encoder.py__init__zVideoEncoder.__init__   s    $$%GHHH&&)) 	XVDLLVVVWWW;!JJJJKKK<5;&&M6<MMMNNN??<
<<<===%    N)codecpixel_formatcrfpresetextra_optionsdestr!   r"   r#   r$   r%   returnc                    t          |t                    rt          |          n|}t          j        | j        | j        t          |          ||||d |pi                                 D                        dS )a>  Encode frames into a file.

        Args:
            dest (str or ``pathlib.Path``): The path to the output file, e.g.
                ``video.mp4``. The extension of the file determines the video
                container format.
            codec (str, optional): The codec to use for encoding (e.g., "libx264",
                "h264"). If not specified, the default codec
                for the container format will be used.
                See :ref:`codec_selection` for details.
            pixel_format (str, optional): The pixel format for encoding (e.g.,
                "yuv420p", "yuv444p"). If not specified, uses codec's default format.
                See :ref:`pixel_format` for details.
            crf (int or float, optional): Constant Rate Factor for encoding quality. Lower values
                mean better quality. Valid range depends on the encoder (e.g.  0-51 for libx264).
                Defaults to None (which will use encoder's default).
                See :ref:`crf` for details.
            preset (str or int, optional): Encoder option that controls the tradeoff between
                encoding encoding speed and compression (output size). Valid on the encoder (commonly
                a string: "fast", "medium", "slow"). Defaults to None
                (which will use encoder's default).
                See :ref:`preset` for details.
            extra_options (dict[str, Any], optional): A dictionary of additional
                encoder options to pass, e.g. ``{"qp": 5, "tune": "film"}``.
                See :ref:`extra_options` for details.
        c                 @    g | ]\  }}||fD ]}t          |          S  str.0kvxs       r   
<listcomp>z(VideoEncoder.to_file.<locals>.<listcomp>P   G       1a1a& EFA   r    )r   r   filenamer!   r"   r#   r$   r%   N)r   intr,   r	   encode_video_to_filer   r   items)r   r&   r!   r"   r#   r$   r%   s          r   to_filezVideoEncoder.to_file#   s    H !+63 7 7CVV"<'YY% $1$7R#>#>#@#@  	
 	
 	
 	
 	
 	
r    formatc                    t          |t                    rt          |          n|}t          j        | j        | j        |||||d |pi                                 D                       S )a  Encode frames into raw bytes, as a 1D uint8 Tensor.

        Args:
            format (str): The container format of the encoded frames, e.g. "mp4", "mov",
                    "mkv", "avi", "webm", "flv", etc.
            codec (str, optional): The codec to use for encoding (e.g., "libx264",
                "h264"). If not specified, the default codec
                for the container format will be used.
                See :ref:`codec_selection` for details.
            pixel_format (str, optional): The pixel format to encode frames into (e.g.,
                "yuv420p", "yuv444p"). If not specified, uses codec's default format.
                See :ref:`pixel_format` for details.
            crf (int or float, optional): Constant Rate Factor for encoding quality. Lower values
                mean better quality. Valid range depends on the encoder (e.g.  0-51 for libx264).
                Defaults to None (which will use encoder's default).
                See :ref:`crf` for details.
            preset (str or int, optional): Encoder option that controls the tradeoff between
                encoding encoding speed and compression (output size). Valid on the encoder (commonly
                a string: "fast", "medium", "slow"). Defaults to None
                (which will use encoder's default).
                See :ref:`preset` for details.
            extra_options (dict[str, Any], optional): A dictionary of additional
                encoder options to pass, e.g. ``{"qp": 5, "tune": "film"}``.
                See :ref:`extra_options` for details.

        Returns:
            Tensor: The raw encoded bytes as 1D uint8 Tensor.
        c                 @    g | ]\  }}||fD ]}t          |          S r*   r+   r-   s       r   r2   z*VideoEncoder.to_tensor.<locals>.<listcomp>   r3   r    )r   r   r9   r!   r"   r#   r$   r%   )r   r5   r,   r	   encode_video_to_tensorr   r   r7   )r   r9   r!   r"   r#   r$   r%   preset_values           r   	to_tensorzVideoEncoder.to_tensorU   s    L '1&=&=Is6{{{6+<'% $1$7R#>#>#@#@  
 
 
 	
r    c                    t          |t                    rt          |          n|}t          j        | j        | j        ||||||d |pi                                 D             	  	         dS )am  Encode frames into a file-like object.

        Args:
            file_like: A file-like object that supports ``write()`` and
                ``seek()`` methods, such as io.BytesIO(), an open file in binary
                write mode, etc. Methods must have the following signature:
                ``write(data: bytes) -> int`` and ``seek(offset: int, whence:
                int = 0) -> int``.
            format (str): The container format of the encoded frames, e.g. "mp4", "mov",
                "mkv", "avi", "webm", "flv", etc.
            codec (str, optional): The codec to use for encoding (e.g., "libx264",
                "h264"). If not specified, the default codec
                for the container format will be used.
                See :ref:`codec_selection` for details.
            pixel_format (str, optional): The pixel format for encoding (e.g.,
                "yuv420p", "yuv444p"). If not specified, uses codec's default format.
                See :ref:`pixel_format` for details.
            crf (int or float, optional): Constant Rate Factor for encoding quality. Lower values
                mean better quality. Valid range depends on the encoder (e.g.  0-51 for libx264).
                Defaults to None (which will use encoder's default).
                See :ref:`crf` for details.
            preset (str or int, optional): Encoder option that controls the tradeoff between
                encoding encoding speed and compression (output size). Valid on the encoder (commonly
                a string: "fast", "medium", "slow"). Defaults to None
                (which will use encoder's default).
                See :ref:`preset` for details.
            extra_options (dict[str, Any], optional): A dictionary of additional
                encoder options to pass, e.g. ``{"qp": 5, "tune": "film"}``.
                See :ref:`extra_options` for details.
        c                 @    g | ]\  }}||fD ]}t          |          S r*   r+   r-   s       r   r2   z-VideoEncoder.to_file_like.<locals>.<listcomp>   r3   r    )	r   r   r9   	file_liker!   r"   r#   r$   r%   N)r   r5   r,   r	   encode_video_to_file_liker   r   r7   )r   rA   r9   r!   r"   r#   r$   r%   s           r   to_file_likezVideoEncoder.to_file_like   s    R !+63 7 7CVV'<'% $1$7R#>#>#@#@  	
 	
 	
 	
 	
 	
r    )__name__
__module____qualname____doc__r   floatr   r   r,   r   r   r5   r   r   r8   r>   rC   r*   r    r   r   r   
   s6        &v &e & & & &$  $&*+/,0260
 0
 0
CI0
 }	0

 sm0
 eCJ'(0
 sCx)0
  S#X/0
 
0
 0
 0
 0
l  $&*+/,0262
 2
 2
2
 }	2

 sm2
 eCJ'(2
 sCx)2
  S#X/2
 
2
 2
 2
 2
r  $&*+/,0266
 6
 6
 6

 }6
 sm6
 eCJ'(6
 sCx)6
  S#X/6
 
6
 6
 6
 6
 6
 6
r    r   )pathlibr   typingr   r   r   r   r   r   
torchcodecr	   r   r*   r    r   <module>rL      s          - - - - - - - - - - - -             u
 u
 u
 u
 u
 u
 u
 u
 u
 u
r    