
    i                         d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ  e       Z	 ej                         a G d d      Zdej                  deded	dfd
Zded	efdZd Zdeded	efdZy)    N)Console)internet_managerc                   "    e Zd ZdZd Zd Zd Zy)ProgressDataz%Class to store the last progress datac                 D    d | _         t        j                         | _        y N)	last_data	threadingLocklockselfs    _/home/nidran/Documenti/script/streaming-community/StreamingCommunity/core/processors/capture.py__init__zProgressData.__init__   s    NN$	    c                 T    | j                   5  || _        d d d        y # 1 sw Y   y xY wr   r   r	   )r   datas     r   updatezProgressData.update   s%    YY 	"!DN	" 	" 	"s   'c                 ^    | j                   5  | j                  cd d d        S # 1 sw Y   y xY wr   r   r   s    r   getzProgressData.get!   s%    YY 	">>	" 	" 	"s   #,N)__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    /%""r   r   processdescriptionprogress_datareturnc                    	 d}t        j                          }t        | j                  j                  d      D ]  }	 |j	                         }|st
        j                         r nd|v r	 t        j                          |z
  }t        |      }d|v rZt        |j                  dd            dk(  }|rdnd}	t        t        j                  d|j                  |	d	            d         d
z  }
n5t        t        j                  d|j                  dd	            d         d
z  }
|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }t        j                  |d      }||||d}|j                  |       | d| d| dt        j                   |
       d| d| d| d}t#        |t%        |            }t&        j)                  |j+                  |      d        	 t3        |        y# t,        $ r%}t/        j0                  d| d|        Y d}~9d}~ww xY w# t,        $ r#}t/        j0                  d|        Y d}~.d}~ww xY w# t,        $ r"}t/        j0                  d |        Y d}~d}~ww xY w# t,        $ r"}t/        j0                  d!|        Y d}~yd}~ww xY w# 	 t3        |        w # t,        $ r"}t/        j0                  d!|        Y d}~w d}~ww xY wxY w)"a9  
    Function to capture and print output from a subprocess.

    Parameters:
        - process (subprocess.Popen): The subprocess whose output is captured.
        - description (str): Description of the command being executed.
        - progress_data (ProgressData): Object to store the last progress data.
    r    zsize=qg      Lsizesizez\d+0i  fpszN/AtimebitratespeedT)	add_hours)r'   r*   r(   r)   z [white]: ([green]'fps': [yellow]z![white], [green]'speed': [yellow]z [white], [green]'size': [yellow]z [white], [green]'time': [yellow]z#[white], [green]'bitrate': [yellow]z#[white], [green]'elapsed': [yellow]z[white]))endzError parsing output line:  - Nz'Error processing line from subprocess: zError in capture_output: zError terminating process: )r(   iterstdoutreadlinestripterminate_flagis_setparse_output_linefloatr   intrefindallr   format_timer   format_file_sizemaxlenconsoleprintljust	Exceptionloggingerrorterminate_process)r   r   r   
max_length
start_timelineelapsed_timer   is_endsize_key	byte_sizer'   time_processedr)   r*   elapsed_formatted	json_dataprogress_stringes                      r   capture_outputrQ   &   s   D=
YY[
00"5 7	MD6Mzz| "((*d?(R'+yy{Z'?06$;&+DHHS$,?&@D&HF28wfH(+BJJvtxxRU?V,WXY,Z([^b(bI(+BJJvtxxPS?T,UVW,X(Y\`(`I #hhue4)-&%)@"&((9e"< $% 8 -=,H,Hae,f) -0bi$j	%,,Y7  +m ,669U ;77<g >66F6W6WXa6b5c d66D5E F99@	 B99J8K8U ( &)S5I%J
  o&;&;J&GTRc7	Mz	=g& % R(CD6QC&PQQR  M GsKLLM  71!5667  	=MM7s;<<	=	=g& 	=MM7s;<<	=s   :I. H?I. H?$I. 'H?-FH>I. J 	H<H72H?7H<<H??	I+I& I. &I++I. .	J7JK
 JK
 	K%KK
LKL	L!K>9L>LLrG   c                    	 i }| j                  dd      j                  dd      j                         }|D ]^  }|j                  d      }t        |      dk(  s#|d   }|d   }|dk(  r(t        |t              rd	|v r|j                  d	      d   }|||<   ` |S # t
        $ r'}t        j                  d
|  d|        i cY d}~S d}~ww xY w)z
    Function to parse the output line and extract relevant information.

    Parameters:
        - line (str): The output line to parse.

    Returns:
        dict: A dictionary containing parsed information.
    z  r"   z= =   r      r(   .zError parsing line: r.   N)replacesplitr=   
isinstancestrrA   rB   rC   )rG   r   partspart	key_valuekeyvaluerP   s           r   r5   r5   v   s    T2&..tS9??A 
	"D

3I9~"l!! &=Zs%;u!KK,Q/E!S	
	"  ,TF#aS9:	s$   AB ?B 	C	"C>C	C	c                     	 | j                         | j                          yy# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z
    Function to terminate a subprocess if it's still running.

    Parameters:
        - process (subprocess.Popen): The subprocess to terminate.
    NzFailed to terminate process: )pollkillrA   rB   rC   )r   rP   s     r   rD   rD      sH    ;<<>!LLN " ;5aS9::;s    $ 	AA

Affmpeg_commandc                    t         j                          t               }	 t        j                  | t        j
                  t        j                  d      }t        j                  t        |||f      }|j                          	 |j                          t         j!                          |j#                          	 |j%                         S # t        $ r t        j                  d       Y Ut        $ r"}t        j                  d|        Y d}~zd}~ww xY w# t         j!                          |j#                          w xY w# t        $ r1}t        j                  d|        Y d}~|j%                         S d}~ww xY w)a/  
    Function to capture real-time output from ffmpeg process.

    Parameters:
        - ffmpeg_command (list): The command to execute ffmpeg.
        - description (str): Description of the command being executed.

    Returns:
        dict: JSON dictionary with the last progress data containing
    T)r0   stderruniversal_newlines)targetargszTerminating ffmpeg process...zError in ffmpeg process: Nz Failed to start ffmpeg process: )r3   clearr   
subprocessPopenPIPESTDOUTr
   ThreadrQ   startwaitKeyboardInterruptrB   rC   rA   setjoinr   )rc   r   r   r   output_threadrP   s         r   capture_ffmpeg_real_timeru      s3     !NM> "">*//R\RcRcx|} "((g{\iEjk	!LLN     ! 	;MM9: 	;MM5aS9::	;    >8<== 	>sZ   A#D? C	 $D? 	D'D )D1D	D DD &D<<D? ?	E9E44E9)r8   r(   rB   r
   rj   rich.consoler   StreamingCommunity.utils.osr   r>   Eventr3   r   rk   rZ   rQ   dictr5   rD   listru   r   r   r   <module>r{      s    
     ! 9 ) "" "M=J,, M=3 M=| M=`d M=`C D B;.T . . .r   