koala2 / serverapi+yt+tit.py
root
Initial commit
f3adb3d
raw
history blame
6.83 kB
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)