From 91a3d723a5040418416e15a113d1bb2eab1c9bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean=20K=C3=A1ssio?= <26697873+jeankassio@users.noreply.github.com> Date: Sat, 3 Jun 2023 15:45:20 -0300 Subject: [PATCH] upload files --- composer.json | 21 ++++++++++++++++++ src/VideoToGif.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 composer.json create mode 100644 src/VideoToGif.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d273cee --- /dev/null +++ b/composer.json @@ -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/" } + } +} \ No newline at end of file diff --git a/src/VideoToGif.php b/src/VideoToGif.php new file mode 100644 index 0000000..64a74c3 --- /dev/null +++ b/src/VideoToGif.php @@ -0,0 +1,54 @@ +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 + ]; + } +}