
    
3j                     ^    S SK r S SKrS SKrS SKrS SKrS SKJr  SSKJr   " S S5      r	\	/r
g)    N)Image   )logc                   D    \ rS rSrSr\S 5       rSrSrSr	Sr
S rS	 rS
rg)ImageH264Compression   zAEncodes the input with h264 compression using a configurable CRF.c           	      (    SSSS04SSSSS	S
S.4S.0$ )NrequiredIMAGEtooltipz9The input image tensor to be compressed and decompressed.INT   r   3      zJConstant Rate Factor for h264 encoding (lower values mean higher quality).)defaultminmaxstepr   )imagecrf )clss    @/home/wildlama/comfy/ComfyUI/custom_nodes/comfy_mtb/nodes/ltx.pyINPUT_TYPES ImageH264Compression.INPUT_TYPES   sG     !#^ #% ! !#o	
 	
    )r   compress_and_decompressr   a"  
**Encodes the input with h264 compression using a configurable CRF**.

> [!IMPORTANT]
> This node is not really needed with the latest version of LTXVideo.

> [!NOTE]
> This was recommended by the creators of LTX over banodoco's discord.

*Orginal code from [mix](https://github.com/XmYx)*c                 Z   [         R                  " 5        n[        R                  R	                  US5      n[        R                  R	                  US5      n[        R                  R	                  US5      n[
        R                  " U5      R                  U5        SSSUSSS	[        U5      S
SSSU/n[        R                  " USS9  SSSUSSU/n[        R                  " USS9  [        R                  " [
        R                  " U5      5      n	U	sS S S 5        $ ! , (       d  f       g = f)Nz	input.pngz
output.mp4zdecoded.pngffmpegz-yz-iz-c:vlibx264z-crfz-pix_fmtyuv420pz	-frames:v1T)capture_output)tempfileTemporaryDirectoryospathjoinr   	fromarraysavestr
subprocessrunnparrayopen)
self	img_arrayr   temp_dir
input_pathoutput_pathdecoded_pathencode_commanddecode_commanddecoded_imgs
             r   _compress_decompress_ffmpeg0ImageH264Compression._compress_decompress_ffmpeg5   s    ((*hh<J'',,x>K77<<-@LOOI&++J7 CN NN>$? N NN>$?((5::l#;<KI +**s   C<D
D*c                 F   SS K n/ n SS KnU GH  nUR                  5       R                  5       nUS-  R	                  [
        R                  5      nUR                  SS9nUR                  5       nUR                  USSS9n	U	R                  SS	S
9n
UR                  S	   U
l        UR                  S   U
l        SU
l        S[        U5      0U
l        UR"                  R%                  USS9nU
R'                  U5       H  nU	R)                  U5        M     U
R'                  5        H  nU	R)                  U5        M     U	R+                  5         UR-                  S5        UR                  USSS9n	/ nU	R/                  SS9 H#  nUR1                  SS9nUR3                  U5        M%     U	R+                  5         [5        U5      S:  aO  US   n[6        R8                  " UR	                  [
        R:                  5      S-  5      nUR3                  U5        GM  UR3                  U5        GM     [6        RD                  " U5      RG                  URH                  5      nU4$ ! [<         a    [>        R@                  " S5        U H  nUR                  5       R                  5       S-  R	                  [
        R                  5      nU RC                  Xr5      n[6        R8                  " UR	                  [
        R:                  5      S-  5      nUR3                  U5        M      Nf = f)Nr      C)orderwmp4)modeformath264r   )rater!   r   rgb24)rC   r)videog     o@z7PyAv is not installed... Falling back to the ffmpeg cli)%ioavcpunumpyastyper.   uint8copyBytesIOr0   
add_streamshapewidthheightpix_fmtr+   options
VideoFramefrom_ndarrayencodemuxcloseseekdecode
to_ndarrayappendlentorch
from_numpyfloat32ImportErrorr   warningr:   stacktodevice)r1   r   r   rI   output_imagesrJ   
img_tensorr2   output	containerstreamframepacketdecoded_framesimg_decodedr9   s                   r   r   ,ImageH264Compression.compress_and_decompress\   s   7	2#
&NN,224	&_44RXX>	%NN + 	  GGFUGC	"--f1-=(q1 ) 2!*"'S!2229W2M$mmE2FMM&) 3$mmoFMM&) .! AGGFUGC	!#&--A-6E"'"2"2'"2"BK"))+6 7 !~&*"0"3K"'"2"2#**2::6>#K "((5 "((4S $l M255ellC  
	2KKI $
'^^-335;CCBHHM	">>yN#..&&rzz2U: $$[1 $	
	2s   H$I B?L L r   N)__name__
__module____qualname____firstlineno____doc__classmethodr   RETURN_TYPESFUNCTIONCATEGORYDESCRIPTIONr:   r   __static_attributes__r   r   r   r   r      s:    K
 
, L(HH	6K%N? r   r   )r&   r,   r$   rL   r.   ra   PILr   r   r   	__nodes__r   r   r   <module>r      s3    	      O  O h 	r   