Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

php 视频压缩ffmpeg



Recommended Posts

   $save_dir = storage_path('app') . '/public/' . mt_rand(99, 999) . time() . '_video' . ".mp4"; //保存原视频
      $out_dir = storage_path('app') . '/public/' . mt_rand(99, 999) . time() . '_smallVideo' . ".mp4"; //压缩后的视频
  $rs_video = $this->reduceVideo($oss_url, $out_dir, $save_dir);
  if ($rs_video == false) {
      continue; //压缩视频失败

    public function reduceVideo($oss_url,$out_dir,$save_dir){
        $r = file_put_contents($save_dir, file_get_contents($oss_url));
        if(!$r) return  false;
        $toSize = "8M"; //最大不超过8M
        $file_size = filesize($save_dir);
        $file_size = intval($file_size / 1048576);
        if($file_size < 9){
            $str =  base64_encode(file_get_contents($save_dir));
            return $str; // 小于9m直接返回保存地址
        // -y 自动确认覆盖  -s 压缩后分辨率 -b:v 视频码率  -fs $toSize 最大视频大小,超过后裁剪
        $shell = "ffmpeg -i " . $save_dir . " -y  -s 360x640 -b:v 862k  -fs " . $toSize . "  ". $out_dir . " 2>&1";
        exec($shell, $output, $ret);
        if($ret == 0){
            //ret 压缩执行成功返回0 执行失败返回1
            $str =  base64_encode(file_get_contents($out_dir));
            return $str;
            if(file_exists($save_dir))    @unlink($save_dir);//删除本地
            return  false;


Link to post
Link to comment
Share on other sites


discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    • Create New...