Last active
May 27, 2020 18:02
-
-
Save MattLoyeD/51c91b49302811a22a423a4c5677b850 to your computer and use it in GitHub Desktop.
GIF to MP4 in PHP (with Linux & ffmpeg)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author: Matthieu Loye Deroubaix / @matloyed | |
* @website : http://www.agence-malttt.fr/ | |
* Do what you want with this file :) | |
* Usage : http://gifme.domain.tld/?url=http://www.domain.tld/blablabla.gif | |
*/ | |
if(isset($_GET['url']) && !empty($_GET['url'])) { | |
$uri = urldecode($_GET['url']); | |
$md5 = md5($uri); | |
$cache_path = dirname(__FILE__).'/cache/'; | |
$file = $cache_path.$md5.'.mp4'; | |
// ADAPT HERE | |
if(strpos($uri,'www.domain.tld') !== false){ | |
$file_path = str_replace('https://www.domain.tld/','/srv/app/',$uri); | |
}else{ | |
die; | |
} | |
if(!file_exists($file)) { | |
shell_exec('ffmpeg -i '.escapeshellcmd($file_path).' -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -pix_fmt yuv420p -movflags +faststart '.escapeshellcmd($file)); | |
} | |
header('Content-disposition: attachment; filename="'.$output_filename.'"'); | |
header('Content-Transfer-Encoding: binary'); | |
header("Content-Type: video/mp4"); | |
$start=0; | |
$size = filesize($file); | |
$length = $size; | |
$end = $size - 1; | |
//header('HTTP/1.1 206 Partial Content'); | |
header("Accept-Ranges: $start-$end"); | |
header("Content-Range: bytes $start-$end/$size"); | |
header("Content-Length: $length"); | |
$data = fopen("$file", "r"); | |
fseek($data, $start); | |
$bytesleft = $size-$start + 1; | |
$buffer = 1024 * 256; // 256kb | |
while(!feof($data)){ | |
if($bytesleft > $buffer){ | |
echo fread($data, $buffer); | |
flush(); | |
}else{ | |
echo fread($data, $bytesleft); | |
flush(); | |
} | |
//sleep(1); // Speedlimit = one buffer per second | |
} | |
fclose($data); | |
exit; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment