Last active
July 25, 2024 03:38
-
-
Save xissy/3812653 to your computer and use it in GitHub Desktop.
Get a youtube video information from get_video_info.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
request = require 'request' | |
youTubeMovieInfo = | |
youTubeMovieId: 'videoId' | |
url = "http://www.youtube.com/get_video_info?video_id=#{youTubeMovieInfo.youTubeMovieId}" | |
request.get url, (err, res, body) -> | |
return callback(false) if err | |
return callback(false) if res.statusCode isnt 200 | |
formats = parseYoutubeInfoStringToFormats(body) | |
return callback(false) if formats is null | |
mp4StreamUrl = getMp4StreamUrlFromFormats(formats) | |
youTubeMovieInfo.mp4StreamUrl = mp4StreamUrl | |
# parse youtube api json response. | |
parseYoutubeInfoStringToFormats = (youtubeInfoString) -> | |
youtubeInfoArray = youtubeInfoString.split '&' | |
return null if youtubeInfoArray[0] is 'status=fail' | |
formatStreamArrayString = (element for element in youtubeInfoArray when element.split('=')[0] is 'url_encoded_fmt_stream_map')[0].split('=')[1] | |
formatStreamArray = decodeURIComponent(formatStreamArrayString).split(',') | |
formats = [] | |
for formatStreamString in formatStreamArray | |
formatInfoArray = formatStreamString.split '&' | |
formatInfoMap = {} | |
for formatInfoElement in formatInfoArray | |
formatInfoElementPair = formatInfoElement.split '=' | |
formatInfoMap[formatInfoElementPair[0]] = decodeURIComponent(formatInfoElementPair[1]) | |
formats.push formatInfoMap | |
return formats | |
# find mp4 stream url from parsed data. | |
getMp4StreamUrlFromFormats = (formats) -> | |
return format.url for format in formats when format.itag is '18' |
How do I return the MP4 URL for download using PHP? My function is below:
`function getVideoInfo($video_id){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "context": { "client": { "hl": "en", "clientName": "WEB", "clientVersion": "2.20210721.00.00", "clientFormFactor": "UNKNOWN_FORM_FACTOR", "clientScreen": "WATCH", "mainAppWebInfo": { "graftUrl": "/watch?v='.$video_id.'", } }, "user": { "lockedSafetyMode": false }, "request": { "useSsl": true, "internalExperimentFlags": [], "consistencyTokenJars": [] } }, "videoId": "'.$video_id.'", "playbackContext": { "contentPlaybackContext": { "vis": 0, "splay": false, "autoCaptionsDefaultOn": false, "autonavState": "STATE_NONE", "html5Preference": "HTML5_PREF_WANTS", "lactMilliseconds": "-1" } }, "racyCheckOk": false, "contentCheckOk": false}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
$video_link = 'https://www.youtube.com/watch?v='.$vid;
preg_match('%(?:youtube(?:-nocookie)?.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})%i', $video_link, $match);
$video_id = $vid;
$video = json_decode(getVideoInfo($video_id));
$formats = $video->streamingData->formats;
$adaptiveFormats = $video->streamingData->adaptiveFormats;
$thumbnails = $video->videoDetails->thumbnail->thumbnails;
$title = $video->videoDetails->title;
$short_description = $video->videoDetails->shortDescription;
$thumbnail = end($thumbnails)->url;`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙌 I used this method
URL :
Content Type :
Content :