2016/09/04

How to generate video thumbnails in php?



A Video link without a thumbnail is like a smartphone without a camera, you can use it but no one would like to use it. If you want your visitors to see your video, you need to make sure that your video link has a thumbnail which is forcing users to click on the link.

How can we do this?

1. Generate thumbnails for video using ffmpeg


There is a easy way to generate thumbnails for video in Linux using ffmpeg, it is nice library for processing videos.

First Install FFmpeg on your server


If you are using ubuntu then refer following link to install FFmpeg

http://www.sysads.co.uk/2014/07/install-ffmpeg-2-3-ubuntu-14-04/

// Code to generate video thumbnail 
 
// Location where video thumbnail to store
$thumbnail_path = 'your_site_domain/media/images/thumbnail/';
$second             = 1;
$thumbSize       = '150x150';
 
// Video file name without extension(.mp4 etc)
$videoname  = 'sample_video';
 
// FFmpeg Command to generate video thumbnail
 
$cmd = "{$ffmpeg_installation_path} -i {$video_file_path} -deinterlace -an -ss {$second} -t 00:00:01  -s {$thumbSize} -r 1 -y -vcodec mjpeg -f mjpeg {$path_to_store_generated_thumbnail} 2>&1";
 
exec($cmd, $output, $retval);
 
if ($retval)
{
    echo 'error in generating video thumbnail';
}
else
{
    echo 'Thumbnail generated successfully';
    echo $thumb_path = $thumbnail_path . $videoname . '.jpg';
}

2. Vimeo api to get video thumbnail 


Fetching the vimeo video thumbnail is very simple
$videoId             =  128122927;
 $thumbnail_size = ‘thumbnail_small’;
 
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$videoId.php"));
 
return $hash[0][$thumbnail_size];
/*
  return $hash[0]["thumbnail_small"];
  return $hash[0]["thumbnail_medium"];
  return $hash[0]["thumbnail_large"];
*/

3. Youtube video thumbnail API


Each YouTube video has 4 generated images.

Use either


http://img.youtube.com/vi//0.jpg
http://img.youtube.com/vi//1.jpg
http://img.youtube.com/vi//2.jpg
http://img.youtube.com/vi//3.jpg

or


http://img.youtube.com/vi//default.jpg
http://img.youtube.com/vi//hqdefault.jpg
http://img.youtube.com/vi//mqdefault.jpg
http://img.youtube.com/vi//maxresdefault.jpg

All of the above urls are available over https too.Just change http to https in any of the above urls. Alternatively, you can use the YouTube API to get thumbnail images.

----------

參考資料 : How to generate video thumbnails in php?

沒有留言:

張貼留言

Web 技術中的 Session 是什麼?

轉載原文:  Web 技術中的 Session 是什麼? 本文目標 在 Web 的世界裡,Session 已經是被廣泛使用的一種技術,大多數的 Web 開發者,肯定都能運用自如。在現今的各類 Web 應用程式的開發框架和工具中,Session 也已經被包裝成容易使用的模...