youtube video downloader in php

You can download YouTube videos by utilizing the YouTube API and using cURL in PHP. Here's a sample code that uses the YouTube API and cURL to download videos:

main.php
// video ID of the YouTube video to download
$video_id = 'insert_youtube_video_id_here';

// create a cURL handle
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// set the YouTube API URL with video ID
$url = "https://www.youtube.com/get_video_info?video_id=$video_id";

// set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// execute the cURL request and retrieve video info
$result = curl_exec($ch);
parse_str($result, $video_info);

// retrieve the video title and download URL
$title = $video_info['title'];
$download_url = urldecode($video_info['url_encoded_fmt_stream_map']);
$download_url = explode(',', $download_url)[0];
$download_url = str_replace('url=', '', $download_url);

// set the file name to the video title
$filename = "$title.mp4";

// download the video using cURL
$fp = fopen($filename, 'wb');
curl_setopt($ch, CURLOPT_URL, $download_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
1025 chars
35 lines

This code downloads the video with the given video ID and saves it to your local directory with the same name as the video title. Note that this code only downloads the first video stream available from YouTube. You may need to modify the code to handle different video resolutions and formats.

related categories

gistlibby LogSnag