
    i,                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlZddlZ ej        e          Zi Zded	ej        fd
Zded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZded	ej        fdZdS )z
Video processing handlers
    )Update)ContextTypes)VideoProcessor)Config)dbNupdatecontextc                 v   K   | j                             d           d{V  ddit          | j        j        <   dS )zHandle /compress commandu   📹 **Video Compressor**

Please send me the video you want to compress.
Then choose compression mode:
• HEVC (better compression, slower)
• Fast (faster, good compression)Nactioncompressmessage
reply_textuser_stateseffective_useridr   r	   s     /app/handlers/video_handlers.pycompress_handlerr      s`      
.
#
#	.         .6z,BK%()))    c                 0  K   t          |j                  dk     r"| j                            d           d{V  dS |j        d         }|j        d         }d||dt          | j        j        <   | j                            d| d	| d
           d{V  dS )zHandle /trim command   zUsage: /trim <start_time> <duration>

Example: /trim 00:00:10 00:00:30
(Trim from 10 seconds, duration 30 seconds)

Then send the video file.Nr      trim)r   
start_timedurationu#   ✂️ Ready to trim video!
Start: z
Duration: 

Please send the video file.)lenargsr   r   r   r   r   )r   r	   r   r   s       r   trim_handlerr       s      
7<1n''(
 
 	
 	
 	
 	
 	
 	
 	
 	aJ|AH  - -K%() .
#
#	'	' 	'	' 	' 	'          r   c                 x   K   | j                             d           d{V  dg dt          | j        j        <   dS )zHandle /merge commandua   🔗 **Video Merger**

Send me 2 or more videos to merge.
Send /done when you've sent all videos.Nmerge)r   filesr   r   s     r   merge_handlerr$   7   sh      
.
#
#	2         - -K%()))r   c                   K   |j         s"| j                            d           d{V  dS d                    |j                   }d|dt          | j        j        <   | j                            d| d           d{V  dS )zHandle /rename commandzRUsage: /rename <new_name>

Example: /rename MyVideo.mp4

Then send the video file.N rename)r   new_nameu   📝 Ready to rename to: r   )r   r   r   joinr   r   r   )r   r	   r(   s      r   rename_handlerr*   C   s      < n''(
 
 	
 	
 	
 	
 	
 	
 	

 	xx%%H- -K%()
 .
#
#	'H 	' 	' 	'          r   c                 v   K   | j                             d           d{V  ddit          | j        j        <   dS )zHandle /encode commanduZ   🔄 **Video Encoder**

Send me the video to encode.
Supported formats: MP4, MKV, AVI, MOVNr   encoder   r   s     r   encode_handlerr-   X   s`      
.
#
#	0        
 .6x,@K%()))r   c                    K   |j         r|j         d         nd}| j                            d| d           d{V  d|dt          | j        j        <   dS )zHandle /screenshot commandr   00:00:01u*   📸 **Screenshot Generator**

Timestamp: z

Send me the video file.N
screenshot)r   	timestamp)r   r   r   r   r   r   )r   r	   r1   s      r   screenshot_handlerr2   a   s      #*<?QZI
.
#
#	#	# 	# 	#         - -K%()))r   c                 v   K   | j                             d           d{V  ddit          | j        j        <   dS )zHandle /metadata commanduG   ℹ️ **Video Metadata Viewer**

Send me a video to view its metadata.Nr   metadatar   r   s     r   metadata_handlerr5   o   s`      
.
#
#	0         .6z,BK%()))r   c                 v   K   | j                             d           d{V  ddit          | j        j        <   dS )zHandle /extract_audio commandu?   🎵 **Audio Extractor**

Send me a video to extract its audio.Nr   extract_audior   r   s     r   extract_audio_handlerr8   w   s`      
.
#
#	0         .6,GK%()))r   c                 v   K   | j                             d           d{V  ddit          | j        j        <   dS )zHandle /extract_subs commanduB   📝 **Subtitle Extractor**

Send me a video to extract subtitles.Nr   extract_subsr   r   s     r   extract_subs_handlerr;      s`      
.
#
#	0         .6~,FK%()))r   c                 x   K   | j                             d           d{V  dddt          | j        j        <   dS )zHandle /add_watermark commanduU   🏷️ **Watermark Adder**

First, send me the watermark image.
Then send the video.Nadd_watermark	watermark)r   stepr   r   s     r   add_watermark_handlerr@      sh      
.
#
#	         "- -K%()))r   c           	      ^  K   | j         j        }t          j        |           d{V }t	          j        |          }t          j        dz  dz  }t	          j        |          }|dk    r||z  dz  nd}d| d| d|dd	|d
k    rdnd d	}| j        	                    |d           d{V  dS )zHandle /storage commandNi   r   d   u%   
💾 **Storage Information**

Used: z
Limit: z
Usage: z.1fz%

P   u   ⚠️ Storage almost full!u   ✅ Storage OK
Markdown
parse_mode)
r   r   r   get_storager   format_sizer   MAX_FILE_SIZEr   r   )	r   r	   user_idstoragestorage_formattedmax_storagemax_formatted
percentagestorage_texts	            r   storage_handlerrR      s     #&GN7++++++++G&27;;&-4K".{;;M2=//'K'#--qJ   		 
 	   #-r//7G  L .
#
#LZ
#
H
HHHHHHHHHHr   c                 .  K   | j         j        }|t          vr"| j                            d           d{V  dS t          |         }|                    d          }| j        j        p| j        j        }|s"| j                            d           d{V  dS | j                            d           d{V }	 |j        	                    |j
                   d{V }t          j                            t          j        | d|j        pd           }|                    |           d{V  |dk    rt          j                            t          j        d	|j        pd           }	t'          j        ||	d
           d{V }
|
rkt+          |	d          5 }|j                            | j        j        |d           d{V  ddd           n# 1 swxY w Y   t          j        |	           n|                    d           d{V  n|dk    rt          j                            t          j        d|j        pd           }	t'          j        ||	|d         |d                    d{V }
|
rkt+          |	d          5 }|j                            | j        j        |d           d{V  ddd           n# 1 swxY w Y   t          j        |	           n|                    d           d{V  n|dk    rt          j                            t          j        d| d          }	t'          j        ||	|                    dd                     d{V }
|
rkt+          |	d          5 }|j                            | j        j        |d           d{V  ddd           n# 1 swxY w Y   t          j        |	           n|                    d           d{V  n|dk    rt'          j        |           d{V }|rd|v r|d         }d|                    d d!           d"t=          |                    dd#                    d$d%t'          j        tA          |                    d&d#                               d'|                    d(d!           d)tA          |                    d*d#                    d+z   d,}|                    |d-.           d{V  n|                    d/           d{V  n|d0k    rt          j                            t          j        d1| d2          }	t'          j!        ||	           d{V }
|
rjt+          |	d          5 }|j        "                    | j        j        |d34           d{V  ddd           n# 1 swxY w Y   t          j        |	           n|                    d5           d{V  n|d6k    r|d7         }t          j                            t          j        |          }	d#dl#}|$                    ||	           t+          |	d          5 }|j                            | j        j        |d8|            d{V  ddd           n# 1 swxY w Y   t          j        |	           t          j        %                    |          rt          j        |           |j&        pd#}tO          j(        ||           d{V  |d9k    rt          )                    |d           |d:vr|*                                 d{V  dS dS # tV          $ rS}tX          -                    d;|            |                    d<t]          |                      d{V  Y d}~dS d}~ww xY w)=zHandle incoming video fileszCPlease use a command first!
Example: /compress, /trim, /merge, etc.Nr   zPlease send a valid video file!u   ⏳ Processing your video..._z	video.mp4r   compressed_fastrbu"   ✅ Video compressed successfully!)chat_idvideocaptionu   ❌ Failed to compress video!r   trimmed_r   r   u   ✅ Video trimmed successfully!u   ❌ Failed to trim video!r0   screenshot_z.jpgr1   r/   u   ✅ Screenshot generated!)rX   photorZ   u"   ❌ Failed to generate screenshot!r4   formatu,   
ℹ️ **Video Metadata:**

📁 Filename: filenamezN/Au   
⏱️ Duration: r   z.2fu   s
📦 Size: sizeu   
🎬 Format: format_nameu   
📊 Bitrate: bit_ratei  zkbps
rE   rF   u   ❌ Failed to get metadata!r7   audio_z.mp3u!   ✅ Audio extracted successfully!)rX   audiorZ   u   ❌ Failed to extract audio!r'   r(   u   ✅ Renamed to: r"   )r4   zError processing video: u   ❌ Error: )/r   r   r   r   r   getrY   documentbotget_filefile_idospathr)   r   DOWNLOAD_PATH	file_namedownload_to_driveUPLOAD_PATHr   compress_videoopen
send_videoeffective_chatremove	edit_text
trim_videogenerate_screenshot
send_photoget_video_infofloatrI   intr7   
send_audioshutilcopyexists	file_sizer   update_storagepopdelete	Exceptionloggererrorstr)r   r	   rK   stater   rY   
status_msgfile
input_pathoutput_pathsuccess
video_file
photo_fileinfofmtmetadata_text
audio_filer(   r}   r   es                        r   video_handlerr      s
     #&Gk!!n''6
 
 	
 	
 	
 	
 	
 	
 	
 	 EYYx  F N ;FN$;E n''(IJJJJJJJJJ~001OPPPPPPPPJ|;[))%-88888888W\\&"678e8eU_EcXc8e8eff
$$Z000000000 Z',,v'9;iIg\g;i;ijjK*9*kSYZZZZZZZZG 	L+t,, 
!+00 & 5 8( D 1                        	+&&&& **+JKKKKKKKKKKv',,v'9;feoFdYd;f;fggK*5Kl#U:%6       G
  	H+t,, 
!+00 & 5 8( A 1                        	+&&&& **+FGGGGGGGGGG|##',,v'9;V;V;V;VWWK*>K		+z22       G
  	Q+t,, 
!+00 & 5 8( ; 1                        	+&&&& **+OPPPPPPPPPPz!!'6zBBBBBBBBD JD((8n! 
E**! ! 
A..//	! ! !
 &s37761+=+='>'>??! ! ggmU++! ! 377:q))**d2! ! ! !**=Z*PPPPPPPPPP **+HIIIIIIIIII&&',,v'9;QG;Q;Q;QRRK*8[QQQQQQQQG 	K+t,, 
!+00 & 5 8( C 1                        	+&&&& **+IJJJJJJJJJJxZ(H',,v'98DDK MMMKK
K000k4(( Jk,,"14$9x99 -                        Ik""" 7>>*%% 	"Ij!!! O(q	333333333 WOOGT***%%##%%%%%%%%%%% &%  ; ; ;333444""#9Q#9#9:::::::::::::::;s   9C\7 .G\7 G\7 GB+\7 .J>2\7 >K\7 KB,\7 2.N, \7 ,N00\7 3N04F5\7 ).V#\7 #V''\7 *V'+B\7 1Y>2\7 >Z\7 ZB-\7 7
^A^^)__doc__telegramr   telegram.extr   utils.video_utilsr   configr   databaser   rj   logging	getLogger__name__r   r   DEFAULT_TYPEr   r    r$   r*   r-   r2   r5   r8   r;   r@   rR   r    r   r   <module>r      s          % % % % % % , , , , , ,             				 		8	$	$ 	C6 	CL4M 	C 	C 	C 	Cv 0I    6
 
1J 
 
 
 
 ,2K    *A A,2K A A A AV l6O    C6 CL4M C C C CH H9R H H H HGv G8Q G G G G
 
9R 
 
 
 
I& I<3L I I I I.R; R;1J R; R; R; R; R; R;r   