Picking a strategy for playing video from Youtube [Solved]

Dear Guys&Gals,

I’m looking into loading / streaming Youtube video into an OF App (OS X), and have thought of the two following scenarios.

Idea 1: Use ofxAwesomium + custom HTML full-screen Youtube player
Idea 2: Control ffmpeg through command line and pipe with ofxVideoPipe into App.

Both ideas would also include some code to talk to the API to get the clips.

But I’m wondering if there are other options. Any other suggestions of how I could do this?
Maybe a C++ lib / addon I’m missing?


Solved! This example from Jeff-Crouse’s Code-for-Art repo works perfectly, and is amazingly simple.
It should probably be mixed with some threading, and could then become a valuable addon!


My response to myself got flagged as spam, but anyway, I found this example and it works great: https://github.com/jefftimesten/CodeForArt/tree/master/Chapter011-web/005-youtube

The link returns 404. Can you update it? Alternatively, can you describe / share the code?


@eight It seems the code has moved here: https://github.com/jefftimesten/CodeForArt/tree/master/Chapter009-web/006-youtube

However, I am having difficulties playing a lot of the video’s from Youtube as of now, even with the latest updates of youtube-dl.

If you look inside that link, you will see that there is no code behind the boilerplate. Would you care to share what you have?


Oh alright. The general idea is to use Youtube-DL to get the current (and temporary) URL to a video. I do this in a separate thread, to prevent hiccups in my program.

You do this as a terminal-command (note: I’m on OS X), so you need to catch the result from that, and feed it to a video player. Because my project also involved RTSP camera’s I used the ofGstVideoPlayer as a base class, something I now regret. It seems you can use the regular ofVideoPlayer just fine.

Also the current version of Youtube-DL generates errors with video’s that have age restrictions and/or DRM. The version that works for me is the 2014.11.13 tag. (Which you need to build yourself after downloading, ie. unpack, go to the folder in your terminal and type sudo make. After this you need to allow the program to be executed, by typing sudo chmod a+x youtube-dl. Finally you can move it whereever you want, I have it in my bin/data folder.

After doing this you first want to test if the application actually runs fine / catch firewall issues etc.
Do this by (in terminal) going to the containing folder, and run something like this: ./youtube-dl --get-url https://www.youtube.com/watch?v=dQw4w9WgXcQ. This should (after some time) return a valid URL to a clip on youtube. You can copy and paste the result in Quicktime > Open Location.

When this works, all you need to do is run it from openframeworks and catch the result. This is what I do:

string getYoutubeDlStream(string _link) //_link is the complete url, just like above. 
    string command = youtube_dl + " --get-url --format 18 " + _link; //youtube_dl is my local path to the youtube-dl executable.
    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    result.erase(remove(result.begin(), result.end(), '\n'), result.end());
    string dlStream = result;
    ofLogNotice(command + "---> " + dlStream); // Check result
    bool success = (ofIsStringInString(dlStream, "http://") || ofIsStringInString(dlStream, "https://"));
    return ( success? dlStream : ""); // return valid URL when found, else return empty string. 

Good luck!

1 Like

@еелке: Thanks. This is much appreciated.


I have used this method with pretty good results, but it stopped working when I jumped to OFv0.9.0.
Has anybody used it with OF 0.9.0? I am trying to figure out if it is a OF problem or an issue with my OSX+XCode versions.

Did you manage to get it working with OFv0.9.0? I’m currently looking into doing youtube with v0.9.8 and would love to hear your findings in case it would save me (and others to come) a ton of time. Thanks in advance

Hi! The truth is that I gave up and decided to download the contents with youtube-dl, covert them to HAP with ffmpeg and then play them back.
It is suboptimal, since that is not streaming :frowning:
If somebody found something, that would be great to share!

Well that’s great because that’s what I’ve decided to do instead but I’m having trouble lol. I’m just trying to get the vid with a system() call like so:

string cmd = "youtube-dl -o /Users/.../of_v0.9.8_osx_release/apps/myAppFolder/myApp/bin/data/mycoolvideo.mp4 \"https://www.youtube.com/watch?v=dQw4w9WgXcQ\" &";

but I’m getting a sh: youtube-dl: command not found error. I’ve tried all sorts of vids in case it had to do with permissions, drm, etc and the command runs fine from Terminal. I also confirmed that youtube-dl exists inside /usr/local/bin and that /usr/local/bin is in /etc/paths…

I guess I’m just wondering if you could share a bit about how you did it please?

Thank you so much for coming back to a 2 year old thread :slight_smile:

No worries!
Trying using the whole path to youtube-dl when calling it in system(). It should be in your PATH but some times it doesn’t work.
I also recommend to update youtube-dl when you run the program -for example in setup().

YES thanks Diego.

My pleasure @a__ . Let me know if I can be of any further help.