Doctranslate.io Translation API
  • Introduction
    • Welcome to Doctranslate.io
    • What is DocTranslate.io
    • User Manual
  • Document Translation API
    • System requirement
    • Upload API
      • Upload multiple files and calculate usage cost
    • User API
      • User History
    • Translation API
      • API V1
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V2
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V3
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Audio Translation
        • Text Translation
        • Get result
    • Presentation Maker API
      • API V1
        • Presentation Maker
        • Presentation Maker For Multiple Files
      • API V2
        • Presentation Maker
        • Presentation Maker For Multiple Files
  • Presentation Video API
    • API V1
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
    • API V2
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
  • Others
    • Code examples
      • API V1
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
      • API V2
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
    • Support
Powered by GitBook
On this page
  1. Others
  2. Code examples
  3. API V1

Audio Translation

import datetime
import requests
from time import sleep

# Endpoints
BASE_URL = "https://doctranslate-api.doctranslate.io"
TRANSLATE_IMAGES_ENDPOINT = f"{BASE_URL}/v1/translate/audio"
GET_RESULT_ENDPOINT = f"{BASE_URL}/v1/result/"

# Configuration variables for the request
SOURCE_LANG = 'vi'
DEST_LANG = 'vi'
FILE_PATH = '/path/to/your/file.mp3'  # Use a generic path or explain how to set this
OUTPUT_FOLDER = '/path/to/output/folder/'

# Authentication token
auth_token = "<YOUR_API_TOKEN>"

# Organize your script into functions to improve readability and reusability
def upload_file_for_translation(file_path, token):
    form_data = {
        'source_lang': SOURCE_LANG,
        'dest_lang': DEST_LANG
    }
    
    with open(file_path, 'rb') as file:
        files = {'files': file}
        headers = {'Authorization': f'Bearer {token}'}
        response = requests.post(TRANSLATE_IMAGES_ENDPOINT, data=form_data, files=files, headers=headers)
    return response.json()

def check_translation_status(task_id, token):
    start_time = datetime.datetime.now()
    timeout = 60 * 10  # 10 minutes
    while True:
        elapsed_time = datetime.datetime.now() - start_time
        if elapsed_time.seconds >= timeout:
            print('Error: Process timed out')
            return None, None

        headers = {'Authorization': f'Bearer {token}'}
        response = requests.get(f"{GET_RESULT_ENDPOINT}{task_id}", headers=headers)
        response_data = response.json()

        if response_data.get('status') == 'success' and response_data.get('data', {}).get('status') == 'done':
            return response_data['data']['transcript_urls'], response_data['data']['translated_transcribe_urls']
        sleep(5)

def download_file(url_download, output_folder, filename):
    response = requests.get(url_download)
    _filename = url_download.split('/')[-1].split('?')[0]
    output_file_path = f"{output_folder}{filename}-{_filename}"
    with open(output_file_path, 'wb') as file:
        file.write(response.content)
    return output_file_path

# Main execution flow
response_data = upload_file_for_translation(FILE_PATH, auth_token)
if response_data['status'] == 'success':
    task_id = response_data['data']['task_id']
    print(f'Task ID: {task_id}')
    transcript_urls,translated_transcribe_urls = check_translation_status(task_id, auth_token)
    if transcript_urls and translated_transcribe_urls:
        transcript_file = download_file(transcript_urls, OUTPUT_FOLDER, 'transcript')
        translated_transcribe_file = download_file(translated_transcribe_urls, OUTPUT_FOLDER, 'translated')
        print(f'Transcript file: {transcript_file}')
        print(f'Translated file: {translated_transcribe_file}')
    else:
        print('Error: missing url_download')
else:
    print('Error: Failed to initiate translation task')
PreviousImage TranslationNextVideo Translation

Last updated 1 year ago