I want to display the first or any frame of a youtube video directly as I click on its url.
Can I also get the dimensions of that particular frame ?

How should I do it ?
Is 'ffmpeg' an option, but I have seen that it is used with videos that are already downloaded in the system rather than on a website ?

Please help!

Recommended Answers

All 11 Replies


Do you mean first frame as image? or first frame up to some set time frame as video?

if you only need the thumbnail, then thats pretty easy I think.. here is a demo of my not-so-alpha-buggy-trash-bin NON-Zend dependent Youtube API .First public preview :).

Script is not available for public, because I am in agreement with my brother who is running this API project, but I think it is worth previewing it to the public as the development progresses.. besides, developer don't really have to rely on Zend ????? no! I still love Zend because I invested lots of money on the certification, I don't need for a profession in Mathematics and Physics. ... :)

Here is a function that will do it for you automatically, just provide the url of the video and you have the choice which thumb you want to show.


## written and provided to you by veedeoo or poorboy 2012.

## public Notice to all it may concern.
## this script is excluded from any youtube API project I have association with, and is not included in the non-disclosure agreement signed by me for the year 2010 - 2012.
## this script is free to use any way you like it, as long as it does not violate youTube's terms of service as described and defined in http://www.youtube.com/static?gl=US&template=terms.
## if you are a developer, and this script is going to be use in part or in full within your applications using youtube API, then instructions and rules are written in https://developers.google.com/youtube/creating_monetizable_applications.

function getVideoId($url)
    $url = $url.'&';
    $pattern = '/v=(.+?)&+/';
    preg_match($pattern, $url, $matches);
    return ($matches[1]);

## just add the url of the video from youtube as shown in my example below.

$v_id = getVideoId('http://www.youtube.com/watch?v=wUjT5Yt6TbU');
echo "video ID: ".$v_id;
echo '<br/>Big Image:<br/><img src="http://i2.ytimg.com/vi/'.$v_id.'/0.jpg" /> <br/>';
echo 'Image Two:<br/><img src="http://i2.ytimg.com/vi/'.$v_id.'/default.jpg" /> <br/>';
echo 'Image Three:<br/><img src="http://i2.ytimg.com/vi/'.$v_id.'/1.jpg" /> <br/>';
echo 'Image Four:<br/><img src="http://i2.ytimg.com/vi/'.$v_id.'/2.jpg" /> <br/>';
echo 'Image Five:<br/><img src="http://i2.ytimg.com/vi/'.$v_id.'/3.jpg" /> <br/>';


What I had planned to do was to somehow get the frame whether first or not as a full sized image!
i.e. if the youtube video is 1080p then the frame or image that I get should be 1092x1080 px in dimensions ...
I hope you are getting what I mean ....

the /0.jpg is the very large image they created after upload, and is more likely that if the video is of high quality or encoded in HD then the /0.jpg is also equivalent to either maxresdefault.jpg or hqdefault.jpg.

Having the maxresdefault and hqdefault included in this discussion, we cannot rely on its availability. The reason is that the encoder will not know the bitrate of the video uploaded by user, until it is being process. Based on the source bitrate, encoder will decide programmatically if it will be encoded as 1080p 740, or just a reqular quality video. I don't see any logical reason behind the 500kb bit rate video being encoded in the maxrate bitrate of 5000kb, that will not make the output any better. FFmpeg or any encoder cannot provide a higher quality beyond the quality of the input.

So in short, you are better of taking the /0.jpg instead of taking chances on the maxresdefault.jpg or hqdefault.jpg which may not be available.

to get the dimension of the 0.jpg, just add this code

list($x,$y) = getimagesize('http://i2.ytimg.com/vi/'.$v_id.'/0.jpg');  
echo 'Image Widht: '.$x.'<br/>';
echo 'Image Height: '.$y.'<br/>';

You can then device some kind of if statement to sort out the dimension you want to use.

example of video with maxresdefault.jpg

  1. this is o.jpg http://i.ytimg.com/vi/9kD8sxIjVuc/0.jpg
  2. this is a blowup 0.jpg http://i.ytimg.com/vi/9kD8sxIjVuc/maxresdefault.jpg
  3. this is the hqdefault.jpg http://i.ytimg.com/vi/9kD8sxIjVuc/hqdefault.jpg

Notice the quality differences ?? Pretty much the hqdefault.jpg is the same as the o.jpg..

I see that all the 0.jpg images are being displayed at 480x360 for every video.
Does this mean that all youtube have a default resolution of 480x360 if present.... ?

Ok, if I just keep Youtube aside for a moment.
Suppose, I have a database of videos ... that have various video resolutions. And then I want to extract a frame from that video .... in that case do I have to rely on "ffmpeg" ?

yes, if you have the video in your server. ffmpeg is needed to create thumbnails.

Based on the resolution of the video itself, we can determine what would be the final dimension of the image. In order to do this, you need the latest ffmpeg distro installed in your server AND the ffmpeg php .

Upon successful installation of the ffmpeg with all of its required packages and plugins, and the ffmpeg php, we can easily determine pretty much all of the information about the video..

For example, we can use simple codes below to extract some important data about the video

  ## we can open the video as an object
  ## we don't want it as boolean persistent

  ## we set dimension as null at first
   $thisVideoDimension = null;

  if($veedeoo = new ffmpeg_movie('uploads/yourVideo.mp4'){
  ## the media is a valid and supported media
$thisMedia = true;
## we get the dimension of the video from the veedeoo object

$thisVideoDimension = array($veedeoo->getFrameWidth(),$veedeoo->getFrameHeight());

## we do a simple check if the width is greater than 480
## we can always use the long form of if statement, instead of a short hand as shown below.

$thisWidthForHD = (($thisVideoDimension[0] > 480) && ($thisVideoDimension[1] > 380))? true : false;

## now based on the response from thiswidthforHd, we can construct our encoder.

## hd thumb
$thumb = (PATH_OF_YOUR_FFMPEG." -y -i uploads/yourVideo.mp4 -f mjpeg -s WIDTH_THAT_YOU_WANT x HEIGHT_THAT_U_WANT -vframes 1 -ss TIME_FRAME_TO_CAPTURE -an FILENAME_OF_YOUR_IMAGE.jpg") ;
 $thumb = (PATH_OF_YOUR_FFMPEG." -y -i uploads/yourVideo.mp4 -f mjpeg -s WIDTH_THAT_YOU_WANT x HEIGHT_THAT_U_WANT -vframes 1 -ss TIME_FRAME_TO_CAPTURE -an FILENAME_OF_YOUR_IMAGE.jpg") ;
 ## execute the ffmpeg


That should create a thumbnails based on the frame dimension of the input video .

I am not able to install ffmpeg-php.
There are no .dll files in the package I downloaded! What should I do ?

    $thisVideoDimension = null;
    if($veedeoo = new ffmpeg_movie('uploads/SeanKingston.mp4'){
        $thisMedia = true;
        $thisVideoDimension = array($veedeoo->getFrameWidth(),$veedeoo->getFrameHeight());

    $thisWidthForHD = (($thisVideoDimension[0] > 480) && ($thisVideoDimension[1] > 380))? true : false;

        $thumb = ("F:\ffmpeg\bin\ffmpeg.exe"." -y -i uploads/SeanKingston.mp4 -f mjpeg -s 1920 x 1080 -vframes 1 -ss 00:00:10 -an filehd.jpg") ;
        $thumb = ("F:\ffmpeg\bin\ffmpeg.exe"." -y -i uploads/SeanKingston.mp4 -f mjpeg -s 120 x 90 -vframes 1 -ss 00:00:10 -an filesmall.jpg") ;

I am getting this error ....
Parse error: parse error in E:\wamp\www\Stuff\ff1.php on line 3

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.