Presentation Video
import requests
import datetime
import time
# Authentication token
auth_token = "<YOUR_API_TOKEN>"
headers = {'Authorization': f'Bearer {auth_token}'}
# Constants for URLs and content type
BASE_URL = "https://doctranslate-api.doctranslate.io"
CREATE_TRANSCRIPTS_URL = f"{BASE_URL}/v2/create/transcripts"
UPDATE_TRANSCRIPTS_URL = f"{BASE_URL}/v2/update/transcripts"
CREATE_VIDEO_URL = f"{BASE_URL}/v2/create/video"
GET_RESULT_ENDPOINT = f"{BASE_URL}/v1/result/"
UPLOAD_FILE_URL = f"{BASE_URL}/v1/upload"
# File paths
input_file_paths = ['Template_2.pptx'] # Input file PPTX, PDF
# UPLOAD FILE PART
TASK_TYPE = 'create_video'
form_data = {
'task_type': TASK_TYPE,
}
# add files to the form data
files = [('files', open(file_path, 'rb')) for file_path in input_file_paths]
meta_files = []
try:
response = requests.post(UPLOAD_FILE_URL, data=form_data, files=files,
headers=headers)
response.raise_for_status() # Proper error handling
response_data = response.json()
meta_files = response_data.get('data', {})
for metadata in meta_files:
if metadata["task_id"]:
print(f'Upload file success Task ID: {metadata["task_id"]}')
else:
print('Failed to get the task ID from the response.')
except requests.exceptions.RequestException as e:
print(f'An error occurred: {e}')
# CREATE TRANCRIPTS PART
# create transcripts settings
destination_language = 'vi'
translate_type = 'Professional'
custom_prompt = "" # Edit the translation as desired. For example, 'Uppercase the nouns in the text.'
voice = 'john'
domain = 'Default'
# Corrected variable names according to their declaration at the top
json_data = {
'task_type': TASK_TYPE,
'dest_lang': destination_language,
'translate_type': translate_type,
'meta_files': meta_files,
'custom_prompt': custom_prompt,
'voice': voice,
'domain': domain,
}
# Corrected file upload part to match the expected key 'file' in the API
try:
response = requests.post(CREATE_TRANSCRIPTS_URL, json=json_data,
headers=headers)
response.raise_for_status() # Proper error handling
response_data = response.json()
parent_task_id = response_data.get('data', {})
if parent_task_id:
print(f'Processing... create transcript with Task ID: {parent_task_id}')
else:
print('Failed to get the parent task ID from the response.')
except requests.exceptions.RequestException as e:
print(f'An error occurred: {e}')
# Check for task_id before proceeding
if 'parent_task_id' not in locals():
print('Error: missing parent_task_id')
exit()
def get_result(parent_task_id, is_get_url=False):
start_time = datetime.datetime.now()
url_download = ''
is_success = False
while True:
if (datetime.datetime.now() - start_time).seconds >= 600: # 10 minutes timeout
print('Error: Processing timed out.')
break
try:
response = requests.get(GET_RESULT_ENDPOINT + parent_task_id,
headers=headers) # Use GET and append parent_task_id to URL
response_data = response.json()
if not is_get_url:
if response_data.get('status') == 'success' and response_data.get('data', {}).get('transcripts', {}):
results = response_data['data']
for key, result in results.items():
if isinstance(result, dict) and key == 'transcripts':
for index, transcript in result['data'].items():
print(f'slide {index}th: {transcript}')
is_success = True
break
else:
if response_data.get('status') == 'success' and response_data.get('data', {}).get('url_download', {}):
url_download = response_data['data']['url_download']
print(f'URL Download file {response_data.get("data", {}).get("filename")}: {url_download}')
is_success = True
break
except requests.exceptions.RequestException as e:
print(f'An error occurred while checking task status: {e}')
break
time.sleep(5) # Wait before the next check to avoid flooding the server
if not is_success:
print('Error: failed to get the result.')
return is_success
is_success = get_result(parent_task_id)
if is_success:
print('Create transcripts successfully')
while True:
is_update_transcript = input('Do you want to update the transcript? (y/n): ')
if is_update_transcript.lower() == 'y':
# UPDATE TRANCRIPTS PART
# update transcripts settings
while True:
transcript_number = input('Please enter the transcript number: ')
if transcript_number.isdigit():
break
transcript_content = input('Please enter the transcript content: ')
json_data = {
"task_id": parent_task_id,
"transcript_number": int(transcript_number),
"transcript_content": transcript_content,
}
try:
response = requests.post(UPDATE_TRANSCRIPTS_URL, json=json_data,
headers=headers)
response.raise_for_status() # Proper error handling
response_data = response.json()
if response_data.get('status') == True:
print(f'Update transcript successfully')
else:
print('Failed to get the parent task ID from the response.')
except requests.exceptions.RequestException as e:
print(f'An error occurred: {e}')
is_success = get_result(parent_task_id)
elif is_update_transcript == 'n':
break
else:
print('Invalid input. Please enter "y" for yes or "n" for no.')
# CREATE VIDEO PART
# create video settings
json_data = {
"task_ids": [parent_task_id],
}
try:
response = requests.post(CREATE_VIDEO_URL, json=json_data,
headers=headers)
response.raise_for_status() # Proper error handling
response_data = response.json()
parent_task_id = response_data.get('data', {}).get('task_id', {})
if parent_task_id:
print(f'Processing... video with Task ID: {parent_task_id}')
else:
print('Failed to get the parent task ID from the response.')
except requests.exceptions.RequestException as e:
print(f'An error occurred: {e}')
is_success = get_result(parent_task_id, is_get_url=True)
if is_success:
print('Create video successfully')
Last updated