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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I return the MP4 URL for download using PHP? My function is below:
`function getVideoInfo($video_id){
}
$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;`