import subprocess
import requests
import re
from wit import Wit


# Configuration
video_file = 'Ossrati.mp4'
extracted_audio = 'Ossrati.wav'
wit_ai_token = 'FVEW4JARY6ABZJL6MB3MFHJX7NFCIAUX'
srt_file_name = 'Ossrati.srt'


client = Wit(wit_ai_token)
# Extract audio from video
# subprocess.run(['ffmpeg', '-i', video_file, '-acodec', 'pcm_s16le', '-ar', '16000', '-y', extracted_audio], check=True)

# Send audio to Wit.ai for transcription
headers = {'Authorization': f'Bearer {wit_ai_token}', 'Content-Type': 'audio/wav'}
response = requests.post('https://api.wit.ai/speech', headers=headers, data=open(extracted_audio, 'rb'))
print(response.json())



with open('Ossrati.wav', 'rb') as f:
  resp = client.speech(f, {'Content-Type': 'audio/wav'})
print('Yay, got Wit.ai response: ' + str(resp))



# transcription = response.json().get('text')  # Adjust based on Wit.ai's response structure
# print(transcription)
#
# # Function to generate SRT content
# def generate_srt(transcription):
#     sentences = re.split(r'(?<=[.!?]) +', transcription)
#     srt_content = ""
#     for i, sentence in enumerate(sentences, start=1):
#         start_seconds = (i - 1) * 5
#         end_seconds = start_seconds + 5
#         start_time = f"00:00:{start_seconds:02},000"
#         end_time = f"00:00:{end_seconds:02},000"
#         srt_content += f"{i}\n{start_time} --> {end_time}\n{sentence}\n\n"
#     return srt_content
#
# # Generate SRT file
# srt_content = generate_srt(transcription)
# with open(srt_file_name, 'w') as file:
#     file.write(srt_content)
#
# print("SRT file has been generated.")
