o
    r›Xjò  ã                   @  sR   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 G dd„ dƒZdS )é    )ÚannotationsN)ÚPath)ÚOpenAI)ÚretryÚstop_after_attemptÚwait_exponentialc                   @  sr   e Zd ZdZ	d2ddddœd3dd„Zddœd4dd„Zd5d6d d!„Zeed"ƒe	d#d$d%�d&d'�dddd(œd7d0d1„ƒZ
dS )8ÚSoraVideoGeneratoru
  OpenAI Sora (Videos API) â€” original short-form marketing clips.

    Generates fully new motion footage from a storyboard prompt.
    Does NOT animate static images. Audio is intentionally omitted
    (see src/generators/audio.py for a future modular add-on).
    úsora-2Ú12g      @g      Œ@)Údefault_secondsÚpoll_intervalÚtimeoutÚapi_keyÚstrÚmodelr   r   Úfloatr   c                C  s,   t |d�| _|| _t|ƒ| _|| _|| _d S )N)r   )r   Úclientr   r   r   r   r   )Úselfr   r   r   r   r   © r   úF/home/esfera/Documents/content_generation/src/generators/sora_video.pyÚ__init__   s
   	

zSoraVideoGenerator.__init__N)ÚbeatsÚhookÚtopicÚwhy_it_mattersÚkey_factÚcreative_directionÚvideo_styleÚnews_categoryr   úlist[dict] | NoneÚreturnc                C  s¶   g }	|r0|D ])}
|	  d|
 dd¡› d|
 dd¡› d|
 d¡p$|
 d	¡p$d› d
|
 dd¡› �¡ q|	r7d |	¡nd|› d|› d|› d|› d�	}d|pJ|› d|› d|› d|› d�	dd… S )z8Compress cinematic reel storyboard into one Sora prompt.z- ÚbeatÚscenez: on-screen feel 'Ú	on_screenÚ z
'; visual ÚvisualÚ	voiceoverz; camera/motion Úmotionzcinematic moveÚ
z- hook: z	
- news: z
- why it matters: z
- key fact: z1
- ending CTA energy: follow for daily tech dropsuÖ  Premium vertical short-form marketing reel for an AI/tech media brand (Instagram Reels / TikTok / YouTube Shorts quality). Cinematic, original motion footage â€” NOT a slideshow, NOT animating a still poster, NOT PowerPoint. Multiple dynamic scenes with camera moves, parallax, light streaks, kinetic energy, product/UI showcases, futuristic AI environments. NO readable text overlays, NO real company logos, NO watermarks, NO voiceover requirement. Creative direction: z. Visual genre: z	. Style: z. Narrative arc:
z³
Pacing: punchy hook first 2 seconds, then explanatory visuals, then impactful ending. Photoreal + high-end motion graphics hybrid, rich lighting, depth, professional color grade.Né   )ÚappendÚgetÚjoin)r   r   r   r   r   r   r   r   r   Ú
beat_linesÚbÚbeats_blockr   r   r   Úbuild_prompt!   s<   ÿ
þÿÿþýÿ	úùùøôzSoraVideoGenerator.build_promptÚportraitÚaspectc                 C  s   |dkrdS dS )NÚ	landscapeÚ1280x720Ú720x1280r   )r   r2   r   r   r   Úsize_for_aspectL   s   z"SoraVideoGenerator.size_for_aspecté   é   é   )ÚminÚmaxT)ÚstopÚwaitÚreraise)ÚsecondsÚsizer2   ÚpromptÚout_pathr   r?   ú
str | Noner@   Údictc                C  sD  |j jddd� |p| j}|dvrd}|p|  |¡}| jjj| j|d d… ||d�}|j}	t	|ddƒ}
t
 
¡ }|
d	vrgt
 
¡ | | jkrPtd
|	› d| j› d�ƒ‚t
 | j¡ | jj |	¡}t	|ddƒ}
|
d	vs;|
dkrzt	|dd ƒ}td|pv|
› �ƒ‚| jj |	¡}t|dƒrŠ| ¡ nt|ƒ}| d¡}| |¡ ||	| j|||
dœS )NT)ÚparentsÚexist_ok>   Ú4Ú8r
   r
   r)   )r   rA   r?   r@   ÚstatusÚqueued>   ÚfailedÚ	completedz	Sora job z timed out after ÚsÚunknownrL   ÚerrorzSora generation failed: Úreadz.mp4)ÚpathÚvideo_idr   r?   r@   rI   )ÚparentÚmkdirr   r6   r   ÚvideosÚcreater   ÚidÚgetattrÚtimer   ÚTimeoutErrorÚsleepr   ÚretrieveÚRuntimeErrorÚdownload_contentÚhasattrrP   ÚbytesÚwith_suffixÚwrite_bytes)r   rA   rB   r?   r@   r2   ÚsecsÚszÚvideorR   rI   ÚstartedÚerrÚcontentÚdatar   r   r   ÚgenerateR   sF   


üû

úzSoraVideoGenerator.generate)r	   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   )r1   )r2   r   r    r   )rA   r   rB   r   r?   rC   r@   rC   r2   r   r    rD   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r0   r6   r   r   r   rj   r   r   r   r   r   
   s     
ýùö+ùr   )Ú
__future__r   rY   Úpathlibr   Úopenair   Útenacityr   r   r   r   r   r   r   r   Ú<module>   s    