17

I am trying to embed Facebook video using the code below:

<object width="400" height="224" >
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://www.facebook.com/v/115316011865684" />
<embed src="http://www.facebook.com/v/115316011865684" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="224">
</embed>
</object>

It's working fine, but is there any similar way to show video thumbnail from video id?
For example: http://www.facebook.com/thumbnail/115316011865684 or something else?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Mohd Shahid
  • 1,538
  • 2
  • 33
  • 66

4 Answers4

32

You can get the thumbnail picture from the video id by going to this Graph API URL - https://graph.facebook.com/VIDEO_ID/picture, e.g. https://graph.facebook.com/115316011865684/picture

C Abernathy
  • 5,533
  • 1
  • 22
  • 27
  • 1
    thanks, it helped me :)... can i get video title in this way? – Mohd Shahid Aug 16 '11 at 19:44
  • 1
    To get the video title, make a call to https://graph.facebook.com/VIDEO_ID and look for the "name" field returned. The "picture" field will also be returned with this call and corresponds to the thumbnail. For more details see https://developers.facebook.com/docs/reference/api/video/ – C Abernathy Aug 22 '11 at 18:00
  • 2
    it was quite small picture, can i get bigger thumbnail picture like youtube? – TomSawyer Aug 01 '15 at 03:34
  • this url is not working for all of the videos. like for this one: – Manik Mittal Jan 01 '16 at 08:32
  • 1
    It seems that this only works for pages and not users. It works great for pages though. For users, they have to authenticate and give permission from what I can tell. If you are getting video from pages, the below method from @Eric Klien will give you access to bigger images. – Jake Feb 01 '16 at 04:06
  • 1
    How can we use this url to get large image directly? – Vivek Sancheti Mar 10 '16 at 07:15
  • Deprecated! `(#12) picture edge for this type is deprecated for versions v2.3 and higher` – Hamzeh Soboh Jun 20 '17 at 09:41
  • @TomSawyer If you want to get bigger images use 'format' field as well. – gihandilanka Jan 08 '18 at 07:36
9

https://graph.facebook.com/VIDEO_ID will give you a lot more info, including bigger thumbnails to pick from. (You can get a list of the available info at https://developers.facebook.com/docs/graph-api/reference/video.)

Here's some PHP code to dig up the biggest thumbnail:

$data = file_get_contents("https://graph.facebook.com/$video_id?fields=format");
if ($data !== FALSE)
{
 $result=json_decode($data);
 $count=count($result->format)-1;
 $thumbnail=$result->format[$count]->picture;
}

Update: The code above has been updated since Facebook changed their API on July 10, 2017. Here is some additional PHP code to get a big thumbnail for a video in case Facebook changes things again:

$data = file_get_contents("https://graph.facebook.com/$video_id/thumbnails?access_token=$facebook_access_token");
if ($data !== FALSE)
{
 $result=json_decode($data);
 $thumbnail=$result->data[0]->uri;
}

This second solution requires a Facebook access token. Here are some instructions on how to get a Facebook access token: https://smashballoon.com/custom-facebook-feed/access-token/

Update: Facebook is making it more and more difficult to even get access tokens with the necessary permissions for such a simple task. Here is how to get the info from the raw HTML:

$data = `curl -s -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' -L 'https://www.facebook.com/1706818892661729/'`;

if (preg_match('#<video [^>]+></video>\s*<div [^>]+><img [^>]+src="([^"]+)#s',$data,$matches))
 {
  $image = $matches[1]; $image = str_replace('&amp;','&',$image);
  if (strpos($image,'&')) {print "Answer: $image\n";}
 }

Note that if you download the page, Facebook also provides the meta property twitter:image but that image is only 200x200. If Facebook wasn't such a pain in the butt, they would also provide the meta property og:image with a decent size image but they don't.

Eric Klien
  • 539
  • 5
  • 11
  • 2
    It seems that this only works for pages and not users. It works great for pages though. For users, they have to authenticate and give permission from what I can tell. – Jake Feb 01 '16 at 04:04
  • @jake I think it's possible to get videos uploaded by users. I can see a lot of online facebook video downloaders that could do this. I am curious to know on how they manage to do so. – basagabi Dec 15 '16 at 18:21
  • In February 2016, when I looked into this, there wasn't really a way to do this with User's video, but you can do it with Page's videos. I am not sure what you are seeing, but this is not about viewing video on Facebook. If you see thumbnails from Facebook, showing on another site, they are either from a Facebook page (not user) or they got permission from the user using the API. That is the big issue, users are locked down, you have to get their permission through the API to use their content. – Jake Dec 15 '16 at 22:33
1

I just get it:

https://graph.facebook.com/VIDEO_ID?fields=format,source

This will get you an array of available formats with thumbnail URL and HTML for embeding. Also source attribute get .mp4 url of video.

Try: https://graph.facebook.com/1706818892661729?fields=format,source

Jure
  • 11
  • 1
1

i created a php function to answer your question without you having to go through reading the boring documentation about facebook graph. All you will need is just to insert your video link,facebook and youtube, but you can modify to add other sources. simply copy the youtube video link in the addres bar and for for facebook, right click on the video and click on show video url, then copy that.

    //get video thumbnail for facebook and youtube
function get_vid_thumbnail($link){
    $thumbnail='';
//check if video link is facebook
    if (strpos($link, 'facebook') !== false) {
        $thumbnail=fb_thumb($link);
        //$thumbnail='fb';
    }
//check if video link is youtube
    if (strpos($link, 'youtube.com') !== false) {
        $thumbnail=youtube_thumb($link);
        //$thumbnail='youtube';
    }
    return $thumbnail;
}


//supporting functions
//get youtube thumbnail
function youtube_thumb($link){
    $new=str_replace('https://www.youtube.com/watch?v=','', $link);
    $vv='https://img.youtube.com/vi/'.$new.'/0.jpg';
    return $vv;
}

//clean the facebook link
function fb_video_id($url) {
    //split the url
    $main=parse_url($url);
    //get the pathe and split to get the video id
    $main=$main['path'];
    $main=explode('/',$main);
    $main=$main[3];
    return $main;
}
//get the thumbnail
function fb_thumb($link) {
    $img = 'https://graph.facebook.com/'.fb_video_id($link).'/picture';
    return $img;
}

//get video thumbnail for fb and youtube ends

//get embed url for facebook and youtube to be used as video source
function get_vid_embed_url($link){
    $embed_url='sss';
//check if video link is facebook
    if (strpos($link, 'facebook') !== false) {
        # code...
        $embed_url=fb_embed_link($link);
        //$thumbnail='fb';
    }
//check if video link is youtube
    if (strpos($link, 'youtube.com') !== false) {
        # code...
        $embed_url=youtube_embed_link($link);
        //$thumbnail='youtube';
    }
    return $embed_url;
}
//get youtube embed link
function youtube_embed_link($link){
    $new=str_replace('https://www.youtube.com/watch?v=','', $link);
    $link='https://www.youtube.com/embed/'.$new;
    return $link;
}
//get facebook embed link
function fb_embed_link($link) {
    $link = 'https://www.facebook.com/plugins/video.php?href='.$link.'&show_text=0&width=560';
    return $link;
}
Sodruldeen Mustapha
  • 1,135
  • 10
  • 22