Update srv.py
Browse files
srv.py
CHANGED
@@ -84,5 +84,43 @@ def download_audio():
|
|
84 |
return jsonify({'error': str(e)}), 500
|
85 |
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
if __name__ == '__main__':
|
88 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
84 |
return jsonify({'error': str(e)}), 500
|
85 |
|
86 |
|
87 |
+
@app.route('/audio', methods=['POST'])
|
88 |
+
def download_high_audio():
|
89 |
+
data = request.json
|
90 |
+
url = data.get('url')
|
91 |
+
|
92 |
+
if not url:
|
93 |
+
return jsonify({'error': 'URL is required'}), 400
|
94 |
+
|
95 |
+
try:
|
96 |
+
# 鐢熸垚鍞竴鐨勬枃浠跺悕
|
97 |
+
unique_id = str(uuid.uuid4())
|
98 |
+
file_name = os.path.join(DOWNLOAD_DIR, f'{unique_id}.mp3')
|
99 |
+
|
100 |
+
# Set up yt-dlp options to download only audio
|
101 |
+
ydl_opts = {
|
102 |
+
'format': 'bestaudio/best', # Download the best available audio
|
103 |
+
'outtmpl': file_name.rsplit('.', 1)[0] + '.%(ext)s', # Output filename format
|
104 |
+
'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed
|
105 |
+
'postprocessors': [{
|
106 |
+
'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg
|
107 |
+
'preferredcodec': 'mp3', # Convert to MP3
|
108 |
+
'preferredquality': '320', # Set audio quality
|
109 |
+
}],
|
110 |
+
'noplaylist': True, # Avoid downloading entire playlists
|
111 |
+
}
|
112 |
+
|
113 |
+
with YoutubeDL(ydl_opts) as ydl:
|
114 |
+
# Extract video info and download the audio
|
115 |
+
ydl.extract_info(url, download=True)
|
116 |
+
|
117 |
+
# 杩斿洖鍙笅杞界殑鍦板潃
|
118 |
+
download_url = f'{request.host_url}{DOWNLOAD_DIR}/{os.path.basename(file_name)}'
|
119 |
+
return jsonify({'download_url': download_url})
|
120 |
+
|
121 |
+
except Exception as e:
|
122 |
+
return jsonify({'error': str(e)}), 500
|
123 |
+
|
124 |
+
|
125 |
if __name__ == '__main__':
|
126 |
app.run(host='0.0.0.0', port=7860, debug=True)
|