Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD MattLoyeD/index.php
Last active Aug 22, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.