Download YouTube video in server

后端 未结 1 1339
忘了有多久
忘了有多久 2021-02-06 16:32

I have created a YouTube search engine + download + MP3 convert script. I have used Jeckman\'s YouTube Downloader for creating this script. Everything is OK, except, I want to d

相关标签:
1条回答
  • 2021-02-06 16:55

    I've found out a solution to store the YouTube file to server. I have removed the header stuff and put $download_video_file = file_put_contents($file_path, fopen($url, 'r')); in place of readfile($url); and it worked like a magic! ^_^

    Here's the complete code:

    <?php
    // Check download token
    if (empty($_GET['mime']) OR empty($_GET['token']))
    {
    exit('Invalid download token 8{');
    }
    // Set operation params
    $mime = filter_var($_GET['mime']);
    $ext  = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
    $url  = base64_decode(filter_var($_GET['token']));
    $name = urldecode($_GET['title']). '.' .$ext; 
    // Fetch and serve
    if ($url)
    {
    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
    {/*
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
    */}
    else
    {/*
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
    */}
    $download_video_file = file_put_contents($file_path, fopen($url, 'r'));
    exit;
    }
    
    // Not found
    exit('File not found 8{');
    ?>
    
    0 讨论(0)
提交回复
热议问题