【发布时间】:2015-05-18 10:01:52
【问题描述】:
<?php
$youtubeUrl = "https://www.youtube.com/watch?v=Ko2JcxecV2E";
$content = json_encode ($file = shell_exec("youtube-dl.exe $youtubeUrl "));
$input_string =$content;
$regex_pattern = "/Destination:(.*.mp4)/";
$boolean = preg_match($regex_pattern, $input_string, $matches_out);
$extracted_string=$matches_out[0];
$file =explode(': ',$extracted_string,2)[1];
// Quick check to verify that the file exists
if( !file_exists($file) ) die("File not found");
// Force the download
header("Content-Disposition: attachment; filename=\"$file\"" );
header("Content-Length: " . filesize($file));
header("Content-Type: application/octet-stream;");
readfile($file);
?>
当我运行此文件时,首先使用youtube-dl.exe 将相应的 YouTube 视频下载到此 PHP 文件所在的 localhost 服务器文件夹,然后将其从该文件夹推送到浏览器下载(强制下载)。
如何直接开始下载到用户的浏览器?
此外,该文件在本地主机上运行良好,但在远程服务器上却没有。
【问题讨论】:
-
问题是什么?你想让 PHP 代码在
youtube-dl.exe开始下载文件时立即开始将文件流式传输到浏览器,而无需等待下载完成? -
是的......但不是流媒体。下载应该在浏览器而不是流媒体中开始
-
一个问题,您接受了 Martin Prikryl 的回答,这对我也有用,但是您如何在客户端创建一个 mp4 文件?