How to get & set the video format in youtube-dl?

sky picture sky · Mar 17, 2015 · Viewed 12.7k times · Source

getaudio.php

  <?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");
        passthru("youtube-dl -o - $youtubeUrl");

    ?>

I want to download mp3 or any other audio file available through youtube-dl . this file downloads the video file. before downloading I need to confirm all the audio formats availale. How to do that

Answer

Alvin Sim picture Alvin Sim · Oct 28, 2019

By default, youtube-dl downloads a YouTube video in mp4 format.

There is an option to have youtube-dl convert the video to an audio file. In their documentation for "Post Processing Options", you can pass in the -x option to convert the video file to audio (this requires ffmpeg installed) and youtube-dl will download the "best" format available. If you need a specified format, then you can use the --audio-format option.

I doubt youtube-dl can confirm all audio formats available before downloading because it still downloads the video in mp4 format but uses ffmpeg to convert it to the specified audio format and in the end removes the mp4 file.