Spaces:
Paused
Paused
File size: 6,827 Bytes
f3adb3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import os
from werkzeug.utils import secure_filename
from app_rvc import SoniTranslate # Importuj SoniTranslate z app_rvc.py
app = Flask(__name__)
CORS(app)
UPLOAD_FOLDER = "uploads"
OUTPUT_FOLDER = "outputs"
TRANSLATION_FOLDER = "translations"
# Zajištění existence složek
for folder in [UPLOAD_FOLDER, OUTPUT_FOLDER, TRANSLATION_FOLDER]:
if not os.path.exists(folder):
os.makedirs(folder)
API_KEY = "MY_SECRET_API_KEY"
# Endpoint pro stahování souborů
@app.route("/downloads/<path:filename>", methods=["GET"])
def download_file(filename):
print(f"Download requested for file: {filename}")
return send_from_directory(OUTPUT_FOLDER, filename, as_attachment=True)
# Endpoint pro uložení a zobrazení překladu
@app.route("/translations/<video_id>", methods=["GET", "POST"])
def manage_translation(video_id):
translation_file = os.path.join(TRANSLATION_FOLDER, f"{video_id}.txt")
print(f"Manage translation for video_id: {video_id}, file path: {translation_file}")
if request.method == "GET":
if os.path.exists(translation_file):
with open(translation_file, "r", encoding="utf-8") as file:
return jsonify({"translation": file.read()})
return jsonify({"error": "Translation not found"}), 404
if request.method == "POST":
data = request.json.get("edited_translation")
print(f"Saving edited translation for video_id: {video_id}")
with open(translation_file, "w", encoding="utf-8") as file:
file.write(data)
return jsonify({"status": "success", "message": "Translation updated"})
# Endpoint pro překlad
@app.route("/translate_video", methods=["POST"])
def translate_video():
api_key = request.headers.get("Authorization")
if api_key != f"Bearer {API_KEY}":
print("Invalid API key")
return jsonify({"status": "error", "message": "Invalid API key"}), 403
video_file = request.files.get("video")
youtube_url = request.form.get("youtube_url")
target_language = request.form.get("target_language")
if not target_language:
print("Missing target language")
return jsonify({"status": "error", "message": "Missing target language"}), 400
if not video_file and not youtube_url:
print("Missing video or YouTube URL")
return jsonify({"status": "error", "message": "Missing video or YouTube URL"}), 400
file_path = None
try:
if video_file:
filename = secure_filename(video_file.filename)
file_path = os.path.join(UPLOAD_FOLDER, filename)
video_file.save(file_path)
print(f"Uploaded video saved at: {file_path}")
translator = SoniTranslate(cpu_mode=False)
result_files = translator.multilingual_media_conversion(
media_file=file_path if video_file else None,
link_media=youtube_url if youtube_url else "",
target_language=target_language,
is_gui=False,
)
print("Result files:", result_files)
# Najít a uložit SRT soubor
video_id = os.path.splitext(os.path.basename(file_path or youtube_url))[0]
srt_file = os.path.join(OUTPUT_FOLDER, f"{video_id}__cs.srt")
print(f"Looking for SRT file at: {srt_file}")
if os.path.exists(srt_file):
with open(srt_file, "r", encoding="utf-8") as file:
translation = file.read()
translation_file = os.path.join(TRANSLATION_FOLDER, f"{video_id}.txt")
with open(translation_file, "w", encoding="utf-8") as file:
file.write(translation)
print(f"Translation saved at: {translation_file}")
return jsonify({
"status": "success",
"translation_url": f"http://{request.host}/translations/{video_id}",
"message": "Translation completed and ready for editing."
}), 200
except Exception as e:
print(f"Error during translation: {str(e)}")
return jsonify({"status": "error", "message": str(e)}), 500
# finally:
# if file_path and os.path.exists(file_path):
# os.remove(file_path)
# print(f"Temporary file removed: {file_path}")
# Nový endpoint pro dabing po úpravě titulků
@app.route("/start_dubbing/<video_id>", methods=["POST"])
def start_dubbing(video_id):
print(f"Starting dubbing for video_id: {video_id}")
language_code = "cs"
translated_video_file = os.path.join(OUTPUT_FOLDER, f"{video_id}__{language_code}.mp4")
srt_file = os.path.join(OUTPUT_FOLDER, f"{video_id}__{language_code}.srt")
translation_file = os.path.join(TRANSLATION_FOLDER, f"{video_id}.txt")
print(f"Checking files for dubbing:\nTranslated video: {translated_video_file}\nSRT file: {srt_file}\nTranslation file: {translation_file}")
if not os.path.exists(translated_video_file):
print("Translated video not found")
return jsonify({"status": "error", "message": f"Translated video not found: {translated_video_file}"}), 404
if not os.path.exists(srt_file):
print("Subtitle file not found")
return jsonify({"status": "error", "message": f"Subtitle file not found: {srt_file}"}), 404
if not os.path.exists(translation_file):
print("Translation file not found")
return jsonify({"status": "error", "message": f"Translation file not found: {translation_file}"}), 404
try:
# Aktualizace titulků
with open(translation_file, "r", encoding="utf-8") as file:
updated_translation = file.read()
with open(srt_file, "w", encoding="utf-8") as file:
file.write(updated_translation)
print(f"Updated subtitles saved at: {srt_file}")
# Spustit dabing znovu pomocí SoniTranslate
translator = SoniTranslate(cpu_mode=False)
result_files = translator.multilingual_media_conversion(
media_file=translated_video_file,
link_media="",
target_language="Czech (cs)",
is_gui=False,
)
print("Result files from dubbing:", result_files)
# Vrátit URL k nově vytvořeným souborům
result_urls = [
f"http://{request.host}/downloads/{os.path.basename(file)}"
for file in result_files
]
return jsonify({
"status": "success",
"result": result_urls,
"message": "Dubbing completed successfully."
}), 200
except Exception as e:
print(f"Error during dubbing: {str(e)}")
return jsonify({"status": "error", "message": str(e)}), 500
# Spuštění aplikace
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
|