index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="content-language" content="ja" /> <title>MEDL</title> <link rel="shortcut icon" href="/favicon.ico" /> <meta name="robots" content="INDEX,FOLLOW" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> </head> <body> <div id="cone"> <form action="download.php" method="post"> <label for="video_url">動画URL:</label> <input type="text" name="video_url" id="video_url" required><br> <label for="format">形式:</label> <select name="format" id="format" required> <option value="mp4">MP4</option> <option value="m4a">m4a</option> </select><br> <input type="submit" value="ダウンロード"> </form> </div> </body> </html>
download.php
<meta charset="UTF-8" /> <meta http-equiv="content-language" content="ja" /> <title>YTDL</title> <link rel="shortcut icon" href="/favicon.ico" /> <meta name="robots" content="INDEX,FOLLOW" /> <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $video_url = $_POST['video_url']; $format = $_POST['format']; $python_server_url = 'あなたのpythonファイルまたはURLに変えてください'; $data = array('video_url' => $video_url, 'format' => $format); $options = array('http' => array('header' => 'Content-type: application/x-www-form-urlencoded', 'method' => 'POST', 'content' => http_build_query($data))); $context = stream_context_create($options); $result = file_get_contents($python_server_url, false, $context); if ($result !== false) { $download_info = json_decode($result, true); $download_url = $download_info['download_url']; if (!empty($download_url)) { echo "ファイル元リンク: <a target='_blank' href='$download_url' download>こちら</a><br>"; $timestamp = date('Y-m-d-H-i-s'); $video_url_with_title = $download_url.'&title='.$timestamp; echo "直接今日の名前でダウンロード:<a target='_blank' href='$video_url_with_title'>元のYouTube URL</a>"; } else { echo 'ダウンロードリンクの取得に失敗しました。'; } } else { echo 'Pythonサーバーへのリクエストに失敗しました。'; } } ?>
main.py
必要パッケージ
flask
yt-dlp
from flask import Flask, request, jsonify import subprocess app = Flask(__name__) def embed_thumbnail_url(video_url, thumbnail_url): # 動画URLにサムネイルURLをクエリパラメータとして追加 embedded_url = f"{video_url}?thumbnail={thumbnail_url}" return embedded_url @app.route('/', methods=['POST']) def download_video(): if request.method == 'POST': video_url = request.form['video_url'] format = request.form['format'] # サムネイルURLをリクエストから取得 thumbnail_url = request.form.get('thumbnail_url', '') # 'thumbnail_url' を取得 try: # yt-dlpコマンドを実行してダウンロードURLを取得 command = ["yt-dlp", "--format", format, "--get-url", video_url] result = subprocess.run(command, capture_output=True, text=True, check=True) download_url = result.stdout.strip() if download_url: # サムネイルを埋め込んだURLを生成 embedded_url = embed_thumbnail_url(download_url, thumbnail_url) return jsonify({"download_url": download_url, "embedded_url": embedded_url}) else: return jsonify({"error": "ダウンロードURLの取得に失敗しました。"}) except subprocess.CalledProcessError as e: return jsonify({"error": str(e)}) if __name__ == '__main__': app.run(host='0.0.0.0', port=8080, debug=True)