File size: 1,691 Bytes
81bcd2a
 
7d8fcf6
81bcd2a
 
 
fc187ac
81bcd2a
 
e7222de
fc187ac
81bcd2a
 
 
e7222de
81bcd2a
ef4c690
e7222de
546d2ec
7d8fcf6
e7222de
546d2ec
 
 
 
 
 
 
 
ef4c690
546d2ec
 
ef4c690
546d2ec
 
 
81bcd2a
546d2ec
 
 
 
7d8fcf6
81bcd2a
 
 
 
 
e7222de
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
from flask import Flask, request, send_file, jsonify
import os
import subprocess

app = Flask(__name__)

@app.route('/download', methods=['GET'])
def download_video():
    try:
        # URLの取得
        video_url = request.args.get('url')
        if not video_url:
            return jsonify({"error": "URL is required"}), 400

        # ダウンロード先ディレクトリ
        output_path = "downloads"
        os.makedirs(output_path, exist_ok=True)  # ダウンロード先ディレクトリが存在しない場合、作成する

        # 出力ファイル名テンプレート
        output_file = os.path.join(output_path, '%(title)s.%(ext)s')

        # yt-dlpコマンドの設定
        cookies_file = 'cookies.txt'  # エクスポートしたクッキーを指定
        cmd = [
            'yt-dlp',
            '--cookies', cookies_file,
            '--output', output_file,  # 出力ファイルのパスを指定
            video_url
        ]

        # yt-dlpコマンドを実行
        result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        # yt-dlpが実行された結果を表示(デバッグ用)
        print(result.stdout.decode())
        print(result.stderr.decode())

        # ダウンロードされたファイルのパスを取得
        downloaded_file = output_file % {'title': video_url.split('=')[-1], 'ext': 'mp4'}

        # ファイルをクライアントに送信
        return send_file(downloaded_file, as_attachment=True)

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

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