File size: 4,144 Bytes
f725879 76bc4e4 f725879 48781d2 4a5a3f2 76bc4e4 f725879 76bc4e4 67e0627 76bc4e4 f725879 76bc4e4 bf1a231 76bc4e4 f725879 bf1a231 76bc4e4 5fd66ad f725879 bf1a231 f725879 bf1a231 76bc4e4 bf1a231 f725879 76bc4e4 f725879 76bc4e4 4a5a3f2 e7f1089 fb55b5a 76bc4e4 f725879 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
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/<path:path>')
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) |