Advertisement
Qugurun

Yandex Video Translate

Nov 29th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | Software | 0 0
  1. import os
  2. import requests
  3. import subprocess
  4.  
  5. directory = r'C:\Users\user\YandexDisk\A Gentle Intro To Game Dev Using C# and MonoGame'
  6. video_format = ".mp4"
  7.  
  8. def delete_public_link(file_path):
  9.     TOKEN = 'ВАШ ТОКЕН'
  10.  
  11.     headers = {
  12.         'Authorization': f'OAuth {TOKEN}',
  13.     }
  14.  
  15.     params = {
  16.         'path': file_path.replace("\\","/")
  17.     }
  18.  
  19.     url = "https://cloud-api.yandex.net/v1/disk/resources/unpublish"
  20.     response = requests.put(url, headers=headers, params=params)
  21.  
  22.  
  23.     if response.status_code == 200:
  24.         print(f"Файл {file_path} распубликован.")
  25.     else:
  26.         print(f"Ошибка при распубликации файла: {response.status_code}, {response.text}")
  27.         return "None"
  28.  
  29. def get_public_link(file_path):
  30.     TOKEN = 'ВАШ ТОКЕН'
  31.  
  32.     headers = {
  33.         'Authorization': f'OAuth {TOKEN}',
  34.     }
  35.  
  36.     params = {
  37.         'path': file_path.replace("\\","/"),
  38.     }
  39.  
  40.     url = "https://cloud-api.yandex.net/v1/disk/resources/publish"
  41.     response = requests.put(url, headers=headers, params=params)
  42.  
  43.  
  44.     if response.status_code == 200:
  45.         print(f"Файл {file_path} опубликован.")
  46.     else:
  47.         print(f"Ошибка при публикации файла: {response.status_code}, {response.text}")
  48.         return "None"
  49.  
  50.     get_url = "https://cloud-api.yandex.net/v1/disk/resources"
  51.     response = requests.get(get_url, headers=headers, params=params)
  52.  
  53.     if response.status_code == 200:
  54.         return response.json().get('public_url')
  55.     else:
  56.         print(f"Ошибка при получении ссылки: {response.status_code}, {response.text}")
  57.         return "None"
  58.  
  59. def list_files_in_directory(directory):
  60.     file_list = []
  61.  
  62.     for root, dirs, files in os.walk(directory):
  63.         for file in files:
  64.             if file.endswith(video_format):
  65.                 path = root.replace("C:\\Users\\user\\YandexDisk\\", "")
  66.                 link = get_public_link(os.path.join(path, file))
  67.                 file = file.replace(video_format, '.mp3')
  68.                 file_list.append([root, file, link])
  69.     return file_list
  70.  
  71.  
  72. def run_commands_in_batches(commands, batch_size=3):
  73.     for i in range(0, len(commands), batch_size):
  74.         batch = commands[i:i + batch_size]
  75.  
  76.         processes = [subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
  77.                      for command in batch]
  78.  
  79.         for process in processes:
  80.             stdout, stderr = process.communicate()
  81.             print(f"Вывод: {stdout}")
  82.             if stderr:
  83.                 print(f"Ошибки: {stderr}")
  84.  
  85. def translate():
  86.     files = list_files_in_directory(directory)
  87.     commands = []
  88.     for data in files:
  89.         command = 'vot-cli --output="' + data[0] +  '" --output-file "' + data[1] + '" --lang=en "' + data[2] + '"'
  90.         commands.append(command)
  91.  
  92.     run_commands_in_batches(commands, batch_size=5)
  93.  
  94. translate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement