【问题标题】:PHP convert any video to MP4 using ffmpegPHP使用ffmpeg将任何视频转换为MP4
【发布时间】:2018-02-14 10:49:54
【问题描述】:

我有一个网站,允许用户上传视频。但是使用 HTML5 标签视频,只允许 MP4 视频

所以,我想将用户上传的任何类型的视频转换为 MP4,然后在我的数据库中添加路径。

我尝试了一些方法,将文件扩展名更改为 MP4,但它不起作用。 我读过一些关于 ffmepg 的文章,但我不知道如何使用它。

这是我的 PHP 脚本,我在其中更改文件扩展名,然后在我的数据库中添加路径,请问如何正确转换视频,我应该添加/更改什么?

<?php 
    if(file_exists($_FILES['media-vid']['tmp_name']) && is_uploaded_file($_FILES['media-vid']['tmp_name']))
    {
        $targetvid = md5(time());
        $target_dirvid = "videos/";
        $target_filevid =  $targetvid.basename($_FILES["media-vid"]["name"]);
        $uploadOk = 0;
        $videotype = pathinfo($target_filevid,PATHINFO_EXTENSION);

        $video_formats = array(
            "mpeg",
            "mp4",
            "mov",
            "wav",
            "avi",
            "dat",
            "flv",
            "3gp"
        );
        foreach ($video_formats as $valid_video_format)
        {
            if (preg_match("/$videotype/i", $valid_video_format)) 
            {
                $target_filevid = $targetvid . basename($_FILES["media-vid"] . ".mp4");
                $uploadOk = 1;
                break;
            } 
            else 
            {
                //if it is an image or another file format it is not accepted
                $format_error = "Invalid Video Format!";
            }
        }

        if ($_FILES["media-vid"]["size"] > 5000000000000)
        {
            $uploadOk = 0;
            echo "Sorry, your file is too large.";
        }

        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0 && isset($format_error))
        {
            echo "Sorry, your video was not uploaded.";
            // if everything is ok, try to upload file
        }
        else if ($uploadOk == 0) 
        {
            echo "Sorry, your video was not uploaded.";
        }
        else
        {
            $target_filevid = strtr($target_filevid,
            'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
            'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
            $target_filevid = preg_replace('/([^.a-z0-9]+)/i', '_', $target_filevid);
            if (!move_uploaded_file($_FILES["media-vid"]["tmp_name"], $target_dirvid. $target_filevid))
            {
                echo "Sorry, there was an error uploading your file. Please retry.";
            }
            else
            {
                $vid= $target_dirvid.$target_filevid;
                $nbvid = 1;
            }
        }
    }
?>

谢谢。

【问题讨论】:

    标签: php html video ffmpeg


    【解决方案1】:

    这对我有用

    $folder = '/path/to/uploads/folder';
    $filename = 'your_video.avi';
    $newFilename = pathinfo($filename, PATHINFO_FILENAME).'.mp4';
    
    exec('/usr/bin/ffmpeg -y -i '.$folder.DIRECTORY_SEPARATOR.$filename.' -c:v libx264 -c:a aac -pix_fmt yuv420p -movflags faststart -hide_banner '.$folder.DIRECTORY_SEPARATOR.$newFilename.' 2>&1', $out, $res);
    
    if($res != 0) {
        error_log(var_export($out, true));
        error_log(var_export($res, true));
    
        throw new \Exception("Error!");
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2015-07-02
      • 2017-02-16
      • 2021-06-30
      • 1970-01-01
      • 2015-12-19
      相关资源
      最近更新 更多