Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvcleave/9462c6f648b53ed1b9b2 to your computer and use it in GitHub Desktop.
Save jvcleave/9462c6f648b53ed1b9b2 to your computer and use it in GitHub Desktop.
Get YouTube urls for download/streaming in openFrameworks
struct YouTubeVideo
{
string url;
int itag;
map<string, string> valueMap;
vector<string> valueMapNames;
YouTubeVideo()
{
url = "";
itag = -1;
}
void print()
{
stringstream info;
info << "url: " << url << "\n";
info << "itag: " << itag << "\n";
info << "valueMap key/values START \n";
for(size_t i=0; i<valueMapNames.size(); i++)
{
info << valueMapNames[i] << " : " << valueMap[valueMapNames[i]] << "\n";
}
ofLogVerbose(__func__) << info.str();
}
};
YouTubeVideo createYouTubeVideoInfo(string url)
{
YouTubeVideo youtubeVideo;
youtubeVideo.url = url;
vector <string> params = ofSplitString(url, "&");
for(size_t i=0; i<params.size(); i++)
{
string currentParam = params[i];
vector <string> keyValues = ofSplitString(currentParam, "=");
if(keyValues.size()>=2)
{
if(keyValues[0] == "itag")
{
youtubeVideo.itag = ofToInt(keyValues[1]);
}
youtubeVideo.valueMap[keyValues[0]] = keyValues[1];
youtubeVideo.valueMapNames.push_back(keyValues[0]);
}
}
return youtubeVideo;
}
/*
string INFO_URL = "http://www.youtube.com/get_video_info?&video_id=zlbXf3kg5zM";
getVideoInfo(INFO_URL);
*/
void getVideoInfo(string url)
{
ofHttpResponse httpResponse = ofLoadURL(url);
if(httpResponse.status > 0)
{
string encoded = httpResponse.data.getText();
string decoded;
Poco::URI::decode (encoded, decoded) ;
//ofLogVerbose(__func__) << "decoded: \n" << decoded << "\n\n\n\n\n\n";
vector <string> params = ofSplitString(decoded, "&");
vector<string> decodedURLs;
for(size_t i=0; i<params.size(); i++)
{
if (params[i].find("=") != string::npos)
{
string key = params[i].substr(0, params[i].find("=", 1));
string value = params[i].substr(params[i].find("=", 1)+1, params[i].length());
if(key == "url_encoded_fmt_stream_map")
{
continue;
}
if(key == "url")
{
if (value.find(",") != string::npos)
{
value = value.substr(0, value.find(",", 1));
}
string decodedURL;
Poco::URI::decode(value, decodedURL) ;
decodedURLs.push_back(decodedURL);
}
}
}
vector<YouTubeVideo> youtubeVideos;
for(size_t i=0; i<decodedURLs.size(); i++)
{
//ofLogVerbose(__func__) << i << " url: " << decodedURLs[i];
YouTubeVideo youtubeVideo = createYouTubeVideoInfo(decodedURLs[i]);
youtubeVideos.push_back(youtubeVideo);
}
for(size_t i=0; i<youtubeVideos.size(); i++)
{
if(youtubeVideos[i].itag == 17)
{
ofLogVerbose() << "youtubeVideo optimal url: " << youtubeVideos[i].url;
}else
{
youtubeVideos[i].print();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment