
    A                     &   S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SrSrSrSrSr " S S\R,                  5      r " S S\5      r " S S\5      r " S S\5      rS rS rS rS rS rg)z4Utilities for gcloud ml video-intelligence commands.    )absolute_import)division)unicode_literalsN)storage_util)apis)
exceptions)log)files)iso_duration)timesvideointelligencev1zz[{}] is not a valid format for video input. Must be a local path or a Google Cloud Storage URI (format: gs://bucket/file).zl[{}] is not a valid format for result output. Must be a Google Cloud Storage URI (format: gs://bucket/file).zCould not get video segments from [{0}]. Please make sure you give the desired segments in the form: START1:END1,START2:END2, etc.: [{1}]c                       \ rS rSrSrSrg)Error2   z!Base error class for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       /lib/googlecloudsdk/command_lib/ml/video/util.pyr   r   2   s    )r   r   c                       \ rS rSrSrSrg)SegmentError6   z2Error for poorly formatted video segment messages.r   Nr   r   r   r   r   r   6   s    :r   r   c                       \ rS rSrSrSrg)VideoUriFormatError:   z(Error if the video input URI is invalid.r   Nr   r   r   r   r    r    :   s    0r   r    c                       \ rS rSrSrSrg)AudioTrackError>   z-Error if the audio tracks setting is invalid.r   Nr   r   r   r   r#   r#   >   s    5r   r#   c           	         U (       d  g[         R                  " [        [        5      nUR                  n/ nU  Vs/ s H  oDR                  S5      PM     nnU H  n[        U5      S:w  a.  [        [        R                  SR                  U 5      S5      5      eUS   US   p [        U5      n	[        U5      n
SnUR                  U" UR                  U
R                  5      UR                  U	R                  5      S	95        M     U$ s  snf ! [         a3  n[        [        R                  SR                  U 5      U5      5      eSnAff = f)
aZ  Get VideoSegment messages from string of form START1:END1,START2:END2....

Args:
  given_segments: [str], the list of strings representing the segments.

Raises:
  SegmentError: if the string is malformed.

Returns:
  [GoogleCloudVideointelligenceXXXVideoSegment], the messages
    representing the segments or None if no segments are specified.
N:   ,zMissing start/end segmentr      z{}s)endTimeOffsetstartTimeOffset)r   GetMessagesModule	VIDEO_APIVIDEO_API_VERSION*GoogleCloudVideointelligenceV1VideoSegmentsplitlenr   SEGMENT_ERROR_MESSAGEformatjoin_ParseSegmentTimestamp
ValueErrorappendtotal_seconds)given_segmentsmessagessegment_msgsegment_messagesssegmentssegmentstartendstart_durationend_durationvesec_fmts                r   ValidateAndParseSegmentsrF   B   s=    
##I/@A(CC+$23NqggclN(3g
7|q.55
((>
"$?A B BWQZ3)-e4n+C0l
 GKnn\%?%?@~'C'CDF G% * 
- 4  ).55
((>
"B( ) ))s   D"D
E
.EE
c                     [        U 5      n[        R                  " SR                  U 5      5        US:  a  [	        SR                  U 5      5      e[        R                  " US9$ ! [         a     [
        R                  " U 5      nUR                  S:  a  [
        R                  " 5       eUs $ ! [
        R                  [
        R                  4 a    [	        SR                  U 5      5      ef = ff = f)a  Parse duration formatted segment timestamp into a Duration object.

Assumes string with no duration unit specified (e.g. 's' or 'm' etc.) is
an int representing microseconds.

Args:
  timestamp_string: str, string to convert

Raises:
  ValueError: timestamp_string is not a properly formatted duration, not a
  int or int value is <0

Returns:
  Duration object represented by timestamp_string
zTTime unit missing ('s', 'm','h') for segment timestamp [{}], parsed as microseconds.r   zCould not parse timestamp string [{}]. Timestamp must be a properly formatted duration string with time amount and units (e.g. 1m3.456s, 2m, 14.4353s)zICould not parse duration string [{}]. Timestamp must begreater than >= 0))microseconds)intr	   warningr3   r6   r   ParseDurationr8   DurationValueErrorDurationSyntaxErrorr   Duration)timestamp_stringrH   durations      r   r5   r5   n   s    $D'(L KK **0&1A*BD A
 **0&1A*BD D 
		L	99' 
 
.	.$$%56h			!	#&&((o%%u'?'?@ . HHN+I-. ..
.s#   A& &
C31<B0-C30?C//C3c                     U (       aB  [         R                  R                  U 5      (       d  [        [        R                  U 5      5      eU $ )zValidates given output URI against validator function.

Args:
  output_uri: str, the output URI for the analysis.

Raises:
  VideoUriFormatError: if the URI is not valid.

Returns:
  str, The same output_uri.
)r   ObjectReferenceIsStorageUrlr    OUTPUT_ERROR_MESSAGEr3   )
output_uris    r   ValidateOutputUrirV      s:     44AA*MM
299*E
FF	r   c                 0   UR                   n[        R                  R                  U5      (       a  [        R
                  " U5      Ul        U$ [        R                  R                  U5      (       a  X2l
        U$ [        [        R                  U5      5      e)zEThe Python hook for yaml commands to inject content into the request.)
input_pathospathisfiler
   ReadBinaryFileContentsinputContentr   rR   rS   inputUrir    INPUT_ERROR_MESSAGEr3   )
unused_refargsrequestrZ   s       r   UpdateRequestWithInputrc      sw    	$WW^^D 77=G
 
.	 ##0066 
. 188>
??r   c                 :    [        U 5      S:  a  [        S5      eU $ )z1Verify at most two tracks, convert to [int, int].r'   z+Can not specify more than two audio tracks.)r1   r#   )trackss    r   AudioTrackProcessorrf      s    [1_
G
HH	-r   )r   
__future__r   r   r   rY   googlecloudsdk.api_lib.storager   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   googlecloudsdk.core.utilr
   r   r   r-   r.   r_   rT   r2   r   r   r    r#   rF   r5   rV   rc   rf   r   r   r   <module>rl      s     ; &  ' 	 7 , * # * 1 *	 5 6 - *J *;5 ;1% 16e 6)X':T"	r   