GPTfree api commited on
Commit
81bcd2a
·
verified ·
1 Parent(s): badee3c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, send_file, jsonify
2
+ import os
3
+ import youtube_dl
4
+
5
+ app = Flask(__name__)
6
+
7
+ @app.route('/download', methods=['POST'])
8
+ def download_video():
9
+ try:
10
+ # リクエストからURLを取得
11
+ video_url = request.json.get('url')
12
+ if not video_url:
13
+ return jsonify({"error": "URL is required"}), 400
14
+
15
+ # 保存するファイル名とパスを設定
16
+ output_path = "downloads"
17
+ os.makedirs(output_path, exist_ok=True)
18
+ output_file = os.path.join(output_path, '%(title)s.%(ext)s')
19
+
20
+ # youtube-dlのオプション設定
21
+ ydl_opts = {
22
+ 'format': 'bestvideo+bestaudio/best', # 最高画質を指定
23
+ 'outtmpl': output_file, # 保存先テンプレート
24
+ 'merge_output_format': 'mp4', # 出力フォーマット
25
+ }
26
+
27
+ # 動画のダウンロード
28
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
29
+ info = ydl.extract_info(video_url, download=True)
30
+ downloaded_file = ydl.prepare_filename(info)
31
+ mp4_file = downloaded_file.rsplit('.', 1)[0] + '.mp4'
32
+
33
+ # ファイルをクライアントに送信
34
+ return send_file(mp4_file, as_attachment=True)
35
+
36
+ except Exception as e:
37
+ return jsonify({"error": str(e)}), 500
38
+
39
+
40
+ if __name__ == '__main__':
41
+ app.run(debug=True, host="0.0.0.0", port=7860)