Site icon บริษัท โค๊ดบี จำกัด

PHP วิธีใช้ ffmpeg แปลงไฟล์วีดีโอเป็น mp4

PHP-วิธีใช้-ffmpeg-แปลงไฟล์วีดีโอเป็น-mp4

PHP-วิธีใช้-ffmpeg-แปลงไฟล์วีดีโอเป็น-mp4

ffmpeg คือ ซอฟต์แวร์สำหรับจัดการไฟล์มีเดียต่าง ๆ ไม่ใช่เฉพาะวีดีโอ แต่รวมถึงไฟล์ภาพ เราสามารถใช้ความสามารถของ ffmpeg เพื่อแปลงไฟล์วีดีโอนามสกุลต่าง ๆ ตัวอย่างเช่น การแปลงไฟล์วีดีโอนามสกุล avi เป็นไฟล์ mp4 เพื่อให้สามารถนำไป streaming ผ่านเว็บไซต์ โดยการใช้ html5 video tags ได้

ffmpeg เป็นซอฟต์แวร์ที่เปิดให้นักพัฒนาสามารถนำไปใช้งานได้ฟรี ไม่มีค่าใช้จ่าย ( Open Source ) สามารถใช้งานได้ทั้ง 3 ระบบปฏิบัติการหลัก คือ Linux, MacOS และ ระบบปฏิบัติการ Window

ติดตั้ง ffmpeg และแปลงไฟล์ avi เป็น mp4

$ffmpegpath = "ffmpeg.exe";
$input = 'sample.avi';
$output = 'sample.mp4';

echo 'Converting...';

if (convertVideo($input, $output)){
    echo '<meta http-equiv="refresh" content="3;url=/videoconvert/player.php?vdo='.$output.'">';
}

function convertVideo($input, $output) {
    global $ffmpegpath;
    
    if(!file_exists($input)){
        echo 'file not exists';
        return false;
    }
    if(file_exists($output)) return true;
    $command = "$ffmpegpath -i $input  -y $output";
    exec( $command);
    if(!file_exists($output)) return false;
    if(filesize($output)==0) return false;
    return true;
}
showPlayer($_GET['vdo']);
function showPlayer($video_path){
  echo $video_path;
}
Exit mobile version