I have a custom post type that accepts a YouTube embed HTML snippet, title, and a featured image. Currently, I’m going and finding an image for the featured image, but ideally I would be able to automatically download and resize the first frame of the video itself based on the video URL during the saving process of the post itself. If done right, my post type could require nothing more than the link and could get the image and the embed code from that.
For instance, if the video link is http://www.youtube.com/watch?v=2Jnpi-uBiIg the value of v would be extracted and used to download the the image at http://img.youtube.com/vi/2Jnpi-uBiIg/0.jpg.
I’m very new to wordpress development, but something tells me I would be looking into hooks (if I understand those correctly).
Hi @Jonathan Sampson:
While this is not exactly what you asked for it might actually be a viable solution and it comes for free from WordPress.com thanks to @yoast‘s tweet this morning when he referenced this blog post:
Basically you can use WordPress.com’ screenshot generator URL in this form (which according to the blog post Matt seemed to bless free usage):
https://s.wordpress.com/mshots/v1/{URL-encoded URL}?w={width}
So taking your URL from above:
And then URL encoding so you could use with the screenshot generator you get a URL that looks like this (for a width of 400px):
And of course what follows is what the screenshot looks like (I copied the screenshot for posterity so it won’t change even if the WordPress service is modified rather than include a direct link to the service to display the image.) Not sure why it snapped the video and not the entire page, but the video is even better for what you want:
(source: mikeschinkel.com)
Of course the first time you HTTP GET request the URL it will not return a screenshot because WordPress.com’s servers need to first capture the screenshot, which takes too much time to let your HTTP GET request wait. So on first request your HTTP GET will just be redirected to this URL:
https://s.wordpress.com/wp-content/plugins/mshots/default.gif
And that URL displays this image:
But if you wait a minute after your first HTTP GET request and issue the request again you’ll find your screenshot. My thoughts are that what you’ll want to do is to call it to cache it, wait to download it, and then cache it locally on your own server to minimize the amount of load on WordPress.com’s servers so they won’t rethink offering this for free (or heck, if there’s enough traffic they could even offer it as a paid service and maybe add paid API features too!)
P.S. By the way, to prove that it actually works within a web page here is the screenshot as requested directly from WordPress.com. Note that it may be different from the screenshot I saved and linked above, or it it’s been a while since someone viewed this page and thus their cache has clear it might even be the “Generating Preview” image. If so, wait a minute and then refresh this page and it should return:
My video blog post generator (http://v.leau.co/) does this but not in wp context.
You supply a query e.g. “superman” (then wait (without notice that it is doing something) (since im the only user)), then click the vids you like to post, click generate code and you have the code where the thumbs are hosted on my site because it downloaded these in the meanwhile. This code can be copy and pasted in a post.
In other words if you would put the code in a function call it would return the piece of code e.g. a href with a link to the video that gets added to the content or e.g. the link to the featured image that is downloaded locally.
Is that the piece of code you are looking for ? I think the core is:
A function to retrieve more results (ergo if you just want to display more than 1 video in the resulting code instead of 1 specific) :
A function to get the initial results page:
A function to display download the thumbs in a certain directory (variable) and to return a piece of code to put in the post:
A function to call the above:
(the $key is you Google API key) (http://code.google.com/intl/nl-NL/more/)
IMHO I think the rest is more just “getting the returned stuff and adding it to the content of a post + setting the downloaded cached thumb as featured?
P.S. IMHO it is always better to post a thumbnail in a video when referring to YouTube videos since often older videos are removed by YouTube and they leave ugly postings as a result. With your own thumbs at least the image stays there forever so that afterwards you do have a clue what you posted there originally.
I don’t know if I understood exactaly what you mean, but I’ve found this workaround:
http://wpworks.wordpress.com/2010/12/23/display-youtube-thumbnail-with-wordpress-custom-field/
Best Regards
Function to display the image based on the URL? Is this the kind of thing you had in mind?
YouTube seems to serve up images for a few servers for the thumbnails..
Where N is usually a numeric value from 1-4(5 sometimes, but wasn’t consistent in testing).
They also use
img.youtube.com
but i liked the idea of fetching from alternating servers, so i’ve coded the function to randomly choose from 1 of 4 servers to display the image from.NOTE: There’s not always an image for each of the sizes for every video, some will just come up blank, however the default image seems to consistently work in the video URLs i tested.
Let me know if that helps..