| Server IP : 208.122.213.31 / Your IP : 216.73.216.45 Web Server : Apache System : Linux msd6191.mjhst.com 4.18.0-553.137.1.el8_10.x86_64 #1 SMP Wed Jun 24 11:40:24 UTC 2026 x86_64 User : WHMCS_MIA_382 ( 1001) PHP Version : 7.4.33 Disable Function : NONE MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/httpd/html/store.tiacyrusxxx.com/public_html/dev/ffmpeg-php-master/ |
Upload File : |
<?php
class Mediainfo {
var $xml, $audiobitrate, $videobitrate, $audiochannels, $duration, $width, $height, $audiocodec, $videocodec;
function Mediainfo($source) {
// May need full path to mediainfo, usually /usr/bin/mediainfo
$this->xml = new SimpleXMLElement(shell_exec('mediainfo --output=XML '. $source));
$this->parse();
}
function parse() {
foreach ($this->xml->File->track as $track) {
$type = strtolower($track['type']);
$bitrate = $track->Bit_rate;
$bitrate = (int)substr($bitrate,0,strpos($bitrate,' '));
$bitrate = $bitrate * 1000;
switch ($type) {
case 'general':
// Get duration in seconds
preg_match_all('/\d+/', $this->xml->File->track[0]->Duration, $matches);
if (count($matches) == 1)
$this->duration = $matches[0];
if (count($matches) == 2)
$this->duration = $matches[0] * 60 + $matches[1];
if (count($matches) == 3)
$this->duration = $matches[0] * 60 * 60 + $matches[1] * 60 + $matches[2];
break;
case 'video':
// Get some details from the video stream
$this->videobitrate = $bitrate;
$this->height = $track->Height;
$this->width = $track->Width;
$this->videocodec = strtolower($track->Format);
break;
case 'audio':
// Audio stream - If there's many and they differ then the last one is chosen.
$this->audiobitrate = $bitrate;
preg_match('/\d+/', $track->Channel_s_, $matches);
$this->audiochannels = $matches[0];
$this->audiocodec = strtolower($track->Format);
break;
}
}
if ($this->audiobitrate == null) $this->audiobitrate = 0;
if ($this->audiochannels == null) $this->audiochannels = 0;
}
}
function CreateVideo($videoFile) {
$mediainfo = new Mediainfo($videoFile);
$videobitrate = $mediainfo->videobitrate;
$audiobitrate = $mediainfo->audiobitrate;
$audiochannels = $mediainfo->audiochannels;
$width = $mediainfo->width;
$height = $mediainfo->height;
$pcmd = "ffmpeg -i " . $videoFile;
if ($height >= 720) {
$pcmd .= " -vf ";
if ($height == 1088) {
$pcmd .= "crop=iw:1080:x:y,";
}
$pcmd .= 'scale="trunc(oh*in_w/in_h/2)*2:720"';
$width = (float)(720 * $width) / $height;
}
// Bitrate depending on bitrate of input
if ($width <= 320)
$vbitrate = $videobitrate >= 310000 ? "300" : null;
else if ($width <= 480)
$vbitrate = $videobitrate >= 510000 ? "500" : null;
else if ($width <= 640)
$vbitrate = $videobitrate >= 710000 ? "700" : null;
else if ($width <= 900)
$vbitrate = $videobitrate >= 810000 ? "800" : null;
else if ($width <= 1280)
$vbitrate = $videobitrate >= 1010000 ? "1000" : null;
else
$vbitrate = $videobitrate >= 1210000 ? "1200" : null;
// If source codec is h264 then we don't need to change it.
if ($mediainfo->videocodec == "h264" && empty($vbitrate)) {
$pcmd .= " -vcodec copy";
} else {
$pcmd .= " -vcodec libx264 -r 29.97 -g 160 -cmp 2 -subcmp 2 -mbd 2 -flags +aic+cbp+mv0+mv4 -trellis 1";
if (!empty($vbitrate)) {
$pcmd .= " -b:v ${vbitrate}k";
} else {
$pcmd .= " -b:v ${videobitrate}";
}
}
// Same as before - Don't reencode audio if it's already AAC and less than 96k
if ($mediainfo->audiocodec == "aac" && $audiobitrate <= 96000) {
$pcmd .= " -acodec copy";
} else {
if ($audiobitrate > 96000) $audiobitrate = 96000;
// if ($audiochannels == 1) $audiobitrate /= 2;
$pcmd .= " -acodec aac -strict experimental -ac " . $audiochannels . " -ar 44100 -ab " . $audiobitrate;
}
// $pcmd .= " ".$this->ffmpeg_extra;
$pcmd .= " -y ./video.mp4";
echo $pcmd;
//if(run($pcmd) != 0) throw new Exception("FFmpeg error: ".$flvname, posix_get_last_error());
// MP4Box available in gpac package in debian (May need another repo, can't remember)
$pcmd = sprintf("MP4Box -inter 3000 video.mp4 -out video_new.mp4");
echo $pcmd;
//if(run($pcmd) != 0) throw new Exception("MP4Box error: ".$flvname, posix_get_last_error());
}
$_POST['filename'] = "test/data/test3.mp4";
CreateVideo($_POST['filename']);