from flask import Flask, request, jsonify from yt_dlp import YoutubeDL import os import uuid from threading import Thread import hashlib app = Flask(__name__) # 存储下载文件的临时目录 DOWNLOAD_DIR = 'downloads' if not os.path.exists(DOWNLOAD_DIR): os.makedirs(DOWNLOAD_DIR) from flask import send_from_directory @app.route('/downloads/') def serve_downloads(path): return send_from_directory(DOWNLOAD_DIR, path) @app.route('/') def root(): return jsonify({'message': 'This is an API service. Please use the appropriate endpoints.'}), 404 @app.route('/get-info', methods=['POST']) def get_info(): data = request.json url = data.get('url') if not url: return jsonify({'error': 'URL is required'}), 400 try: ydl_opts = { 'cookiefile': 'www.youtube.com_cookies.txt' } with YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(url, download=False) return jsonify({ 'title': info['title'], 'thumbnail': info.get('thumbnail'), 'duration': info.get('duration'), 'channel': info.get('channel') }) except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/download', methods=['POST']) def download_audio(): data = request.json url = data.get('url') if not url: return jsonify({'error': 'URL is required'}), 400 try: # 生成唯一的文件名 unique_id = str(uuid.uuid4()) file_name = os.path.join(DOWNLOAD_DIR, f'{unique_id}.mp3') # Set up yt-dlp options to download only audio ydl_opts = { 'format': '140/bestaudio', # Download the best available audio 'outtmpl': file_name.rsplit('.', 1)[0] + '.%(ext)s', # Output filename format 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed 'postprocessors': [{ 'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg 'preferredcodec': 'mp3', # Convert to MP3 'preferredquality': '128', # Set audio quality }], 'noplaylist': True, # Avoid downloading entire playlists } with YoutubeDL(ydl_opts) as ydl: # Extract video info and download the audio ydl.extract_info(url, download=True) # 返回可下载的地址 download_url = f'{request.host_url}{DOWNLOAD_DIR}/{os.path.basename(file_name)}' return jsonify({'download_url': download_url}) except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/audio', methods=['POST']) def download_high_audio(): data = request.json url = data.get('url') if not url: return jsonify({'error': 'URL is required'}), 400 try: # 生成唯一的文件名 unique_id = str(uuid.uuid4()) file_name = os.path.join(DOWNLOAD_DIR, f'{unique_id}.mp3') # Set up yt-dlp options to download only audio ydl_opts = { 'format': 'bestaudio/best', # Download the best available audio 'outtmpl': file_name.rsplit('.', 1)[0] + '.%(ext)s', # Output filename format 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed 'postprocessors': [{ 'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg 'preferredcodec': 'mp3', # Convert to MP3 'preferredquality': '320', # Set audio quality }], 'noplaylist': True, # Avoid downloading entire playlists } with YoutubeDL(ydl_opts) as ydl: # Extract video info and download the audio ydl.extract_info(url, download=True) # 返回可下载的地址 download_url = f'{request.host_url}{DOWNLOAD_DIR}/{os.path.basename(file_name)}' return jsonify({'download_url': download_url}) except Exception as e: return jsonify({'error': str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=True)