Chrunos commited on
Commit
f725879
·
verified ·
1 Parent(s): bf1a231

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +18 -20
srv.py CHANGED
@@ -1,12 +1,14 @@
1
- from flask import Flask, request, jsonify, send_file, render_template
2
  from yt_dlp import YoutubeDL
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
7
- @app.route('/')
8
- def home():
9
- return render_template('index.html')
 
10
 
11
  @app.route('/get-info', methods=['POST'])
12
  def get_info():
@@ -20,7 +22,7 @@ def get_info():
20
  ydl_opts = {
21
  'cookiefile': 'www.youtube.com_cookies.txt'
22
  }
23
-
24
  with YoutubeDL(ydl_opts) as ydl:
25
  info = ydl.extract_info(url, download=False)
26
  return jsonify({
@@ -42,38 +44,34 @@ def download_audio():
42
  return jsonify({'error': 'URL is required'}), 400
43
 
44
  try:
 
 
 
 
45
  # Set up yt-dlp options to download only audio
46
  ydl_opts = {
47
  'format': 'bestaudio/best', # Download the best available audio
48
- 'outtmpl': '%(title)s.%(ext)s', # Output filename format
49
  'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed
50
  'postprocessors': [{
51
  'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg
52
  'preferredcodec': 'mp3', # Convert to MP3
53
- 'preferredquality': '192', # Set audio quality
54
  }],
55
  'noplaylist': True, # Avoid downloading entire playlists
56
  }
57
 
58
  with YoutubeDL(ydl_opts) as ydl:
59
  # Extract video info and download the audio
60
- info = ydl.extract_info(url, download=True)
61
- file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
62
 
63
- # Send the downloaded audio file to the client
64
- return send_file(
65
- file_name,
66
- as_attachment=True,
67
- download_name=os.path.basename(file_name)
68
- )
69
 
70
  except Exception as e:
71
  return jsonify({'error': str(e)}), 500
72
 
73
- finally:
74
- # Clean up the file after sending
75
- if 'file_name' in locals() and os.path.exists(file_name):
76
- os.remove(file_name)
77
 
78
  if __name__ == '__main__':
79
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ from flask import Flask, request, jsonify
2
  from yt_dlp import YoutubeDL
3
  import os
4
+ import uuid
5
 
6
  app = Flask(__name__)
7
 
8
+ # 存储下载文件的临时目录
9
+ DOWNLOAD_DIR = 'downloads'
10
+ if not os.path.exists(DOWNLOAD_DIR):
11
+ os.makedirs(DOWNLOAD_DIR)
12
 
13
  @app.route('/get-info', methods=['POST'])
14
  def get_info():
 
22
  ydl_opts = {
23
  'cookiefile': 'www.youtube.com_cookies.txt'
24
  }
25
+
26
  with YoutubeDL(ydl_opts) as ydl:
27
  info = ydl.extract_info(url, download=False)
28
  return jsonify({
 
44
  return jsonify({'error': 'URL is required'}), 400
45
 
46
  try:
47
+ # 生成唯一的文件名
48
+ unique_id = str(uuid.uuid4())
49
+ file_name = os.path.join(DOWNLOAD_DIR, f'{unique_id}.mp3')
50
+
51
  # Set up yt-dlp options to download only audio
52
  ydl_opts = {
53
  'format': 'bestaudio/best', # Download the best available audio
54
+ 'outtmpl': file_name.rsplit('.', 1)[0] + '.%(ext)s', # Output filename format
55
  'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed
56
  'postprocessors': [{
57
  'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg
58
  'preferredcodec': 'mp3', # Convert to MP3
59
+ 'preferredquality': '128', # Set audio quality
60
  }],
61
  'noplaylist': True, # Avoid downloading entire playlists
62
  }
63
 
64
  with YoutubeDL(ydl_opts) as ydl:
65
  # Extract video info and download the audio
66
+ ydl.extract_info(url, download=True)
 
67
 
68
+ # 返回可下载的地址
69
+ download_url = f'{request.host_url}{DOWNLOAD_DIR}/{os.path.basename(file_name)}'
70
+ return jsonify({'download_url': download_url})
 
 
 
71
 
72
  except Exception as e:
73
  return jsonify({'error': str(e)}), 500
74
 
 
 
 
 
75
 
76
  if __name__ == '__main__':
77
+ app.run(host='0.0.0.0', port=7860, debug=True)