B
    N$d                 @   s   d dl mZmZ d dlmZ ddlmZ d dlZd dlmZ d dl	m
Z
 d dlZd dlmZ d d	lmZ d dlZd dlZd dlZdd
lT dd ZdddZdd Zdd Zdd Zdd Zdd ZdS )    )renderredirect)auth   )adminN)datetime)VideoFileClip)AudioSegment)detect_nonsilent)*c          
   C   s   t  }t | }||}W d Q R X y4|j|ddd}|rZd|krZ|d d d }|S d S  t jk
rz   td d S  t jk
r } ztd|  d S d }~X Y nX d S )	Nzar-MAT)languageZshow_allalternativer   Z
transcriptzCould not understand the audiozBCould not request results from Google Speech Recognition service; )srZ
RecognizerZ	AudioFilerecordZrecognize_googleZUnknownValueErrorprintZRequestError)Z
audio_fileZ
recognizersourceaudioresultZbest_transcriptione r   :/home/fefo/Documents/transcribe_darija/transcribe/views.pytranscribe_audio   s    r        Tc             C   sR  t | }|j}|d td}td t|||}tj	tj
| d }	|	 d}
tj|
spt|
 g }d}x|D ]\}}|d }|d }|| }|r||k rq~|d7 }tj|
|	 d| d}|||}|| |j}|d td}td |||||d	 q~W t|}|jtj|
|	 d
dd td d S )Nztemp_audio1.wavr   _outputi  r   Z_part_z.mp4ztemp_audio_chunk.wav)filenamestart_atend_attranscriptionz_metadata.xlsxF)indexzVideo processing completed)r   r   Zwrite_audiofiler	   Zfrom_wavosremover
   pathsplitextbasenameexistsmakedirsjoinZsubclipZwrite_videofiler   appendpdZ	DataFrameZto_excelr   )Z
input_filemin_silence_lensilence_threshmin_durationuse_min_durationvideor   Zaudio_segmentZnonsilent_intervals
video_nameZoutput_foldermetadataZpart_numberstartendZ
start_timeZend_timedurationoutput_fileZvideo_chunkZaudio_chunkr   dfr   r   r   process_video#   sD    








r7   c             C   s
   t | dS )Nz	home.html)r   )requestr   r   r   home]   s    r9   c             C   s   | j dkrt }| jd }| jd }td| td| ytjj|dj|d}W n& tj	k
rz   d}t
| d	d
|iS X td| |rt|j| jd< t|dd |_|  tdS t
| d	d
diS n
t
| d	S d S )NPOSTusernamepasswordzthe username is :zthe password is :)r;   )r<   zWrong username or password!zadmin_login.html	error_msgzthe data is id_admin.r   uploadzError detected.)methodr   nowr:   r   r   objectsfiltergetDoesNotExistr   strr>   sessionsplit	last_timesaver   )r8   rB   r;   r<   datar=   r   r   r   admin_logino   s&    





rM   c          	   C   sR   t |d>}t| tkr(||   nx|  D ]}|| q2W W d Q R X d S )Nzwb+)opentyperG   writeencodechunks)filer#   r   Zdestinationchunkr   r   r   handle_uploaded_file   s
    rU   c             C   sl   |}|}t |}|j}|dd }tjjdd}t }t|dd }	t	||	||||d}
|

  d S )Nr?   r   )id_userr   )r0   when_uploadedrW   r#   r4   	extension)r   r4   rI   userrC   rE   r   rB   rG   r/   rK   )contentr   r0   r#   Zclipr4   rY   Zuser_instancerB   rX   Z	new_videor   r   r   
save_video   s    r\   c             C   s   | j dkrz| jd j}|}| jd }td| d}tj|sHt| t||| t	|| t
|ddddd	 t| d
S t| dS )Nr:   r   zthe file name is :/i  ir   T)r+   r,   r-   r.   z	list.htmlzupload.html)rA   FILESnamer   r!   r#   r&   r'   rU   r\   r7   r   )r8   Zmy_strr   r[   r#   r   r   r   r@      s    





r@   )r   r   r   T)Zdjango.shortcutsr   r   django.contribr   modelsr   r!   r   Zmoviepy.editorr   sysZpydubr	   Zpydub.silencer
   Zpandasr*   Z	audiofileZspeech_recognitionr   r   r7   r9   rM   rU   r\   r@   r   r   r   r   <module>   s&   
: