Skip to content

Commit

Permalink
upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeankassio committed Jun 3, 2023
1 parent eed180f commit 91a3d72
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "jeankassio/videotogif",
"description": "A simple video to gif converter that uses FFMPEG and simplifies the whole process for you.",
"keywords": ["scraping", "scraper", "chromedriver", "webdriver", "metadata", "symfony"],
"homepage": "https://jeankassio.dev",
"license": "MIT",
"authors": [
{
"name": "Jean Kássio",
"email": "contato@jeankassio.dev",
"homepage": "https://jeankassio.dev"
}
],
"require": {
"php": ">=8.0",
"php-ffmpeg/php-ffmpeg": "^1.0.1"
},
"autoload": {
"psr-4": { "JeanKassio\\VideoToGif\\": "src/" }
}
}
54 changes: 54 additions & 0 deletions src/VideoToGif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace JeanKassio;

use FFMpeg\FFProbe;
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\Coordinate\Dimension;

class VideoToGif{

public function convert($videoBase64){
// Decodificar o vídeo base64 e salvá-lo em um arquivo temporário
$videoPath = tempnam(sys_get_temp_dir(), 'video').".mp4" ;
file_put_contents($videoPath, base64_decode($videoBase64));

// Obter informações do vídeo usando FFprobe
$ffprobe = FFProbe::create();
$videoInfo = $ffprobe->streams($videoPath)->videos()->first();

// Extrair o FPS, largura e altura do vídeo
$fps = $videoInfo->get('r_frame_rate');
$width = $videoInfo->get('width');
$height = $videoInfo->get('height');
$duration = $videoInfo->get('duration');

// Configurar o FFmpeg
$ffmpeg = FFMpeg::create();

// Converter para GIF
$gifPath = tempnam(sys_get_temp_dir(), 'gif').".gif";
$video = $ffmpeg->open($videoPath);

$video->gif(TimeCode::fromSeconds(0), new Dimension($width, $height), 5)->save($gifPath);

// Ler o arquivo GIF
$gifData = file_get_contents($gifPath);

// Codificar a saída em base64
$gifBase64 = base64_encode($gifData);

// Remover os arquivos temporários
unlink($videoPath);
unlink($gifPath);

// Retornar o resultado em base64, juntamente com as informações do vídeo
return [
'gif' => $gifBase64,
'fps' => $fps,
'width' => $width,
'height' => $height
];
}
}

0 comments on commit 91a3d72

Please sign in to comment.