yt-dl-7 / index.php
soiz1's picture
Update index.php
7428675 verified
<?php
require __DIR__ . '/vendor/autoload.php';
use YoutubeDl\YoutubeDl;
use YoutubeDl\Process\ProcessBuilder;
use YoutubeDl\Options;
// URLパラメータ確認
if (empty($_GET['url'])) {
exit('URLを指定してください');
}
$url = $_GET['url'];
try {
// yt-dlp のパスに合わせて指定(Dockerfile で /usr/local/bin に入れたなら OK)
$yt = new YoutubeDl(new ProcessBuilder('/usr/local/bin/yt-dlp'));
$collection = $yt->download(
Options::create()
->url($url)
->noPlaylist() // プレイリスト無効化
->extractAudio(false)
);
$video = $collection->getVideos()[0];
if ($video->getError()) {
throw new \Exception($video->getError());
}
$filename = basename($video->getTitle()) . '.mp4';
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($video->getFile());
} catch (Exception $e) {
exit('エラー: ' . $e->getMessage());
}