Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD
Last active May 27, 2020 18:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MattLoyeD/51c91b49302811a22a423a4c5677b850 to your computer and use it in GitHub Desktop.
Save MattLoyeD/51c91b49302811a22a423a4c5677b850 to your computer and use it in GitHub Desktop.
GIF to MP4 in PHP (with Linux & ffmpeg)
<?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