Search code examples
phpyoutubeyoutube-dl

Is there a way to get the audio/video file size for a particular format using youtube-dl


<?php
    $youtubeUrl =  $_GET['url'];
    $content = shell_exec("youtube-dl -j $youtubeUrl "); 
    $meta=json_decode($content);  
    $file= $meta->{'_filename'};
    $fileWithoutExtension = explode(".",$file)[0];
    $extension = ".m4a";
    $file = $fileWithoutExtension . $extension;   

    header("Content-Disposition: attachment; filename=\"$file\"" );
    header("Content-Type: application/octet-stream");
// $fize = shell_exec("youtube-dl -f 141 --get-size $URL");
// header("Content-Length: " . $fsize);
    passthru("youtube-dl -f 140 -o - $youtubeUrl");

?>

See the commented lines. I need those Two lines. rest of the code is working fine. This file downloads the audio from YouTube. But I am not able to see any file size while downloading


Solution

  • Because youtube-dl doesn't have an option to display the size, you might want to look into ytdl instead, it can give you all the meta-data, like filesizes.

    to install:

    $ pip install pafy
    ...
    $ ytdl [url]
    Stream Type    Format Quality         Size            
    ------ ----    ------ -------         ----            
    1      normal  webm   [640x360]       21 MB           
    2      normal  mp4    [640x360]       24 MB           
    3      normal  flv    [320x240]       19 MB           
    4      normal  3gp    [320x240]       15 MB           
    5      normal  3gp    [176x144]        5 MB 
    ...
    

    (https://pypi.python.org/pypi/pafy )