I am trying to create thumbnails from external videos, mainly MP4 and FLV. I am using FFmpegPHP . I already have work on thumbnails, but I need to upload the video on my server first. Will it be possible to transfer only a small part of the video, and then extract the thumbnail from it?
Here is the code that I still have:
require_once PRIV . 'Vendor/FFmpegPHP/FFmpegAutoloader.php'; // Download the whole video. $video = file_get_contents($_PUT['video']); $file = 'path_to_cache'; file_put_contents($file, $video); $movie = new FFmpegMovie($file); // Generate the thumbnail. $thumb = $movie->getFrame($movie->getFrameCount() / 2); $thumb->resize(320, 240); imagejpeg($thumb->toGDImage(), 'path_to_thumb');
Anyone have a suggestion?
EDIT
As Brad suggested, here is the updated code:
$file = CACHE . 'moodboard_video_' . rand(); $fh = fopen($file, 'w'); $size = 0; curl_setopt($ch, CURLOPT_URL, $_PUT['video']); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use($fh, &$size){ $length = fwrite($fh, $data); if($length === FALSE) { return 0; } else { $size += $length; } // Downloads 1MB. return $size < 1024 * 1024 * XXXXXX ? $length : 0; }); curl_exec($ch); fclose($fh); curl_close($ch); // Create the thumbnail. $thumb = $movie->getFrame(XXXXXX); $thumb->resize(static::DEFAULT_THUMBNAIL_WIDTH, $thumb->getHeight() / $thumb->getWidth() * static::DEFAULT_THUMBNAIL_WIDTH); $image = $thumb->toGDImage(); imagejpeg($image, PRIV . static::THUMBNAILS_PATH . $item->getLastInsertIdentifier() . '_' . static::DEFAULT_THUMBNAIL_WIDTH);
source share