sujayjaju / ffmpeg-bundle
一个用于提供 PHP-FFmpeg (https://github.com/PHP-FFmpeg/PHP-FFmpeg) 作为 Symfony 服务的 Symfony 扩展包
0.5.2
2014-08-27 19:29 UTC
Requires
- php-ffmpeg/php-ffmpeg: 0.5.*@dev
This package is not auto-updated.
Last update: 2024-09-28 16:25:31 UTC
README
此扩展包为 PHP_FFmpeg 库提供了一个简单的包装,将其暴露为 Symfony 服务。
基于 pulse00/ffmpeg-bundle,由 pulse00 开发
使用示例
在 AppKernel 中启用扩展
.... new sujayjaju\FFmpegBundle\PhpFFmpegBundle(), ....
在 config.yml 中配置要使用的 ffmpeg 二进制文件
php_ffmpeg: ffmpeg_binary: /usr/bin/ffmpeg ffprobe_binary: /usr/bin/ffprobe binary_timeout: 300 # Use 0 for infinite threads_count: 4
使用服务
$ffmpeg = $this->get('php_ffmpeg.ffmpeg'); // Open video $video = $ffmpeg->open('/your/source/folder/input.avi'); // Resize to 640x480 $video ->filters() ->resize(new Dimension(640, 480), ResizeFilter::RESIZEMODE_INSET) ->synchronize(); // Create a thumbnail $video ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10)) ->save('/PATH/frame.jpg'); // Start transcoding and save video $video->save(new FMpeg\Format\Video\X264(), '/PATH/video.mp4');
与 1up-lab/OneupUploaderBundle 的示例集成
- 步骤 1:创建您的上传监听器类
namespace Application\YourBundle\EventListener; use FFMpeg\Exception\InvalidArgumentException; use FFMpeg\Exception\RuntimeException; use Oneup\UploaderBundle\Event\PostUploadEvent; class YourUploadListener { protected $doctrine; protected $ffmpeg; public function __construct($doctrine, $ffmpeg) { $this->doctrine = $doctrine; $this->ffmpeg = $ffmpeg; } public function onUpload(PostUploadEvent $event) { $file_path = $event->getFile()->getPathname(); $response = $event->getResponse(); $video = null; try{ $ffmpeg = $this->ffmpeg->open($file_path); }catch(InvalidArgumentException $e){ $response->setSuccess(false); $response->setError("Could not load file"); $response['preventRetry'] = true; unlink($file_path); return; }catch(RuntimeException $e){ $response->setSuccess(false); $response->setError("Invalid File"); $response['preventRetry'] = true; unlink($file_path); return; } // Sample code to check if video was uploaded $streams = $ffmpeg->getStreams(); foreach($streams as $stream){ if($stream->get('codec_type') == 'video'){ $video = $stream; } } if(is_null($video)){ $response->setSuccess(false); $response->setError("Invalid Video"); $response['preventRetry'] = true; unlink($file_path); return; } // Do what you want with the video // $video } }
- 步骤 2:定义您的服务
application.your_upload_listener: class: Application\YourBundle\EventListener\YourUploadListener arguments: [@doctrine, @php_ffmpeg.ffmpeg] tags: - { name: kernel.event_listener, event: oneup_uploader.post_upload, method: onUpload }