File size: 2,699 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
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"  # Výstupní složka
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(OUTPUT_FOLDER):
    os.makedirs(OUTPUT_FOLDER)

API_KEY = "MY_SECRET_API_KEY"

# Endpoint pro stahování souborů
@app.route("/downloads/<path:filename>", methods=["GET"])
def download_file(filename):
    return send_from_directory(OUTPUT_FOLDER, filename, as_attachment=True)

@app.route("/process_video", methods=["POST"])
def process_video():
    # Ověření API klíče
    api_key = request.headers.get("Authorization")
    if api_key != f"Bearer {API_KEY}":
        return jsonify({"status": "error", "message": "Invalid API key"}), 403

    # Získání vstupních parametrů
    video_file = request.files.get("video")
    youtube_url = request.form.get("youtube_url")
    target_language = request.form.get("target_language")

    if not target_language:
        return jsonify({"status": "error", "message": "Missing target language"}), 400

    if not video_file and not youtube_url:
        return jsonify({"status": "error", "message": "Missing video or YouTube URL"}), 400

    file_path = None

    try:
        # Pokud je nahraný soubor, ulož ho
        if video_file:
            filename = secure_filename(video_file.filename)
            file_path = os.path.join(UPLOAD_FOLDER, filename)
            video_file.save(file_path)

        # Spusť překlad pomocí SoniTranslate
        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,
        )

        # Vytvoření URL odkazů ke stažení
        result_urls = []
        for result_file in result_files:
            filename = os.path.basename(result_file)
            result_urls.append(f"http://{request.host}/downloads/{filename}")

        return jsonify({"status": "success", "result": result_urls}), 200

    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500

    finally:
        # Smazání dočasného souboru, pokud bylo použito nahrané video
        if file_path and os.path.exists(file_path):
            os.remove(file_path)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)