lakshmajim/thumbnail

视频缩略图

1.4.51 2017-10-19 03:36 UTC

This package is auto-updated.

Last update: 2024-09-06 13:52:21 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads composer.lock

网络上的Wiki

INDEX

这是什么

  • 为指定视频生成缩略图(图像)
  • 此功能使用 FFMpeg
  • 将视频转换为WebM格式。

版本

1.4.5

兼容性

注意: 对于 1.4.3 及其他 早期 版本的文档,请参阅此处

安装依赖软件

此软件包依赖于FFMpeg,一个跨平台解决方案,用于记录、转换和流式传输音频和视频,即多媒体。

在16.04(Xenial Xerus)LTS上安装FFMpeg

  • 运行以下命令安装FFMpeg

    sudo apt-get update
    sudo apt-get install ffmpeg

在Ubuntu 14.04 LTS上安装FFMpeg

  • 添加mc3man ppa

    sudo add-apt-repository ppa:mc3man/trusty-media
  • 运行以下命令更新软件包列表。

    sudo apt-get update
    sudo apt-get update sudo apt-get dist-upgrade
  • 现在可以通过 apt 安装FFmpeg,运行此命令

    sudo apt-get install ffmpeg
    

在CentOS上安装FFMpeg

  • 启用EPEL仓库

    • 针对centos 6

         rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
    • 针对centos 5

         rpm -Uvh http://mirrors.kernel.org/fedora-epel/5/i386/epel-release-5-4.noarch.rpm
    • 针对centos 7

         yum install epel-release
  • 通过以下命令检查EPEL仓库是否启用

       yum repolist
  • 导入Nux Dextop仓库的官方GPG密钥

       rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
Nux Dextop是一个第三方RPM仓库,其中包含许多CentOS、RHEL和ScientificLinux的桌面和多媒体相关软件包(例如,Ardour、Shutter等)。目前,Nux Dextop仓库适用于CentOS/RHEL 6和7。
  • 使用以下yum命令安装Nux Dextop。

    • centos 6

      rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
    • centos 7

      rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
  • 现在验证Nux Dextop仓库是否已成功安装

       yum repolist
  • 运行以下命令安装FFMpeg

       yum install ffmpeg

在Windows上安装FFMpeg

请参阅以下链接

WikiHow

AdapticeSolutions

安装

  • 此软件包在packagist上可用
    composer require lakshmaji/thumbnail
  • 将服务提供者添加到 providers 数组中
Lakshmaji\Thumbnail\ThumbnailServiceProvider::class,
  • 将外观添加到 aliases 数组中
'Thumbnail' => Lakshmaji\Thumbnail\Facade\Thumbnail::class,
  • 尝试使用composer更新应用程序(依赖项,但不是必需的 😉 )
  composer update

配置

  • 发布配置文件,这会将thumbnail.php文件发布到您的应用程序 config 目录中。
    php artisan vendor:publish
  • 配置所需的FFMpeg配置。
  • FFMpeg会自动检测ffmpeg和ffprobe二进制文件。如果您想明确指定二进制文件路径,您可以在 .env 文件中配置它们。
  • 您可以在.env文件中指定输出图像尺寸。(尺寸数组参数)
  • 将水印或播放按钮URL添加到thumbnail.php中可用的 watermark 数组中
  • 或者,您可以从laravel .env文件中配置它们,以下是.env文件中样本水印资源配置的示例
#Thumbnail image dimensions
THUMBNAIL_IMAGE_WIDTH  = 320
THUMBNAIL_IMAGE_HEIGHT = 240

#Watermark image
WATERMARK_IMAGE = true
WATERMARK_PATH  = /var/www/html/thumb/storage/watermark/p.png

#Custom FFMPEG binaries path
FFMPEG_BINARIES = true
FFMPEG_PATH     = /opt/local/ffmpeg/bin/ffmpeg
FFPROBE_PATH    = /opt/local/ffmpeg/bin/ffprobe
FFMPEG_TIMEOUT  = 3600
FFMPEG_THREADS  = 12

这确保了所有带有水印的生成缩略图都具有固定尺寸

生成缩略图

以下示例说明了Thumbnail软件包的使用方法

<?php 

namespace Trending\Http\Controllers\File;

use Carbon;
use Thumbnail;
use Illuminate\Http\Request;
use Trending\Http\Controllers\Controller;


/**
 * -----------------------------------------------------------------------------
 *   ThumbnailTest - a class illustarting the usage og Thumbnail package 
 * -----------------------------------------------------------------------------
 * This class having the functionality to upload a video file 
 * and generate corresponding thumbnail
 *
 * @since    1.0.0
 * @version  1.0.0
 * @author   lakshmaji 
 */
class ThumbnailTest extends AnotherClass
{
  public function testThumbnail()
  {
    // get file from input data
    $file             = $this->request->file('file');

    // get file type
    $extension_type   = $file->getClientMimeType();
    
    // set storage path to store the file (actual video)
    $destination_path = storage_path().'/uploads';

    // get file extension
    $extension        = $file->getClientOriginalExtension();


    $timestamp        = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
    $file_name        = $timestamp;
    
    $upload_status    = $file->move($destination_path, $file_name);         

    if($upload_status)
    {
      // file type is video
      // set storage path to store the file (image generated for a given video)
      $thumbnail_path   = storage_path().'/images';

      $video_path       = $destination_path.'/'.$file_name;

      // set thumbnail image name
      $thumbnail_image  = $fb_user_id.".".$timestamp.".jpg";
      
      // set the thumbnail image "palyback" video button
      $water_mark       = storage_path().'/watermark/p.png';

      // get video length and process it
      // assign the value to time_to_image (which will get screenshot of video at that specified seconds)
      $time_to_image    = floor(($data['video_length'])/2);


      $thumbnail_status = Thumbnail::getThumbnail($video_path,$thumbnail_path,$thumbnail_image,$time_to_image);
      if($thumbnail_status)
      {
        echo "Thumbnail generated";
      }
      else
      {
        echo "thumbnail generation has failed";
      }
    }
  }
}
// end of class ThumbnailTest
// end of file ThumbnailTest.php  

方法

$thumbnail_status = Thumbnail::getThumbnail(<VIDEO_SOURCE_DIRECTORY>,<THUMBNAIL_STORAGE_DIRECTORY>,<THUMBNAIL_NAME>);
$thumbnail_status = Thumbnail::getThumbnail(<VIDEO_SOURCE_DIRECTORY>,<THUMBNAIL_STORAGE_DIRECTORY>,<THUMBNAIL_NAME>,<TIME_TO_TAKE_SCREENSHOT>);

注意:从版本1.4.4开始,某些方法参数已被弃用。对于此软件包的早期版本(<=1.4.3),请参阅此处。水印和输出图像(缩略图图像)的配置可以从 app/config.php 文件中进行。

其他

  • 要生成带有播放按钮的缩略图图像(视频截图)
    • 您的控制器类
    Thumbnail::getThumbnail($video_path,$thumbnail_path,$thumbnail_image,$time_to_image);
    • 您的配置文件
    'watermark' => [
            'image' => [
                'enabled' => env('WATERMARK_IMAGE', true),
                'path'    => env('WATERMARK_PATH', 'http://voluntarydba.com/pics/YouTube%20Play%20Button%20Overlay.png'),
            ],
            'video' => [
                'enabled' => env('WATERMARK_VIDEO', false),
                'path'    => env('WATERMARK_PATH', ''),
            ],
    ],
  • 要生成缩略图图像(视频截图)
  Thumbnail::getThumbnail($video_path,$thumbnail_path,$thumbnail_image);
  • 将视频转换为WebM格式
            $video_path = $destination_path.'/'.$file_name; // source video path
                        $clipped_video  = $destination_path.'/'.'clipped_'.$file_name; // converted video file
 
Thumbnail::clipWebM($video_path,$clipped_video);

许可

MIT