GPTfree api commited on
Commit
546d2ec
·
verified ·
1 Parent(s): ef4c690

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -1,7 +1,6 @@
1
  from flask import Flask, request, send_file, jsonify
2
  import os
3
  import subprocess
4
- from yt_dlp import YoutubeDL
5
 
6
  app = Flask(__name__)
7
 
@@ -17,25 +16,29 @@ def download_video():
17
  output_path = "downloads"
18
  os.makedirs(output_path, exist_ok=True) # ダウンロード先ディレクトリが存在しない場合、作成する
19
 
20
- # 出力ファイルパスのテンプレート
21
  output_file = os.path.join(output_path, '%(title)s.%(ext)s')
22
 
23
- # クッキーのファイルパス(事前にブラウザからエクスポートしたクッキーを指定)
24
- cookies_file = 'cookies.txt'
 
 
 
 
 
 
25
 
26
- # yt-dlpのオプション設定
27
- ydl_opts = {
28
- 'cookies': cookies_file, # クッキーを指定
29
- 'outtmpl': output_file, # 出力ファイルのパスとテンプレート
30
- 'format': 'bestvideo+bestaudio/best', # 最高画質で動画と音声を選択
31
- }
32
 
33
- # yt-dlpで動画をダウンロード
34
- with YoutubeDL(ydl_opts) as ydl:
35
- info = ydl.extract_info(video_url, download=True) # ダウンロード実行
36
- downloaded_file = ydl.prepare_filename(info) # ダウンロードされたファイルのパスを取得
37
 
38
- # ダウンロードされたファイルを返す
 
 
 
39
  return send_file(downloaded_file, as_attachment=True)
40
 
41
  except Exception as e:
 
1
  from flask import Flask, request, send_file, jsonify
2
  import os
3
  import subprocess
 
4
 
5
  app = Flask(__name__)
6
 
 
16
  output_path = "downloads"
17
  os.makedirs(output_path, exist_ok=True) # ダウンロード先ディレクトリが存在しない場合、作成する
18
 
19
+ # 出力ファイル名テンプレート
20
  output_file = os.path.join(output_path, '%(title)s.%(ext)s')
21
 
22
+ # yt-dlpコマンドの設定
23
+ cookies_file = 'cookies.txt' # エクスポートしたクッキーを指定
24
+ cmd = [
25
+ 'yt-dlp',
26
+ '--cookies', cookies_file,
27
+ '--output', output_file, # 出力ファイルのパスを指定
28
+ video_url
29
+ ]
30
 
31
+ # yt-dlpコマンドを実行
32
+ result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
 
 
33
 
34
+ # yt-dlpが実行された結果を表示(デバッグ用)
35
+ print(result.stdout.decode())
36
+ print(result.stderr.decode())
 
37
 
38
+ # ダウンロードされたファイルのパスを取得
39
+ downloaded_file = output_file % {'title': video_url.split('=')[-1], 'ext': 'mp4'}
40
+
41
+ # ファイルをクライアントに送信
42
  return send_file(downloaded_file, as_attachment=True)
43
 
44
  except Exception as e: