PHP将视频文件上传到数据库

时间:2016-01-12 05:53:05

标签: php video ffmpeg

我正在开展一个学校项目,让用户将视频文件上传到服务器。服务器将使用ffmpeg压缩视频并将文件存储在上传文件夹中。其他用户将能够流式传输上传的视频。

我的问题是如何检索ffmpeg生成的视频并将链接存储在数据库中?

我正在使用此代码,但它只检索原始视频的路径。

$filePath = dirname(__FILE__); 

Upload.php的部分代码

$target_dir = "upload/"; //where you want to upload the files to
$target_file = $target_dir.basename($_FILES['file']['name']);
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
$newFileName = $target_dir.sha1(pathinfo(basename($_FILES['file']['name']), PATHINFO_FILENAME)).'-'.time().'.'.$fileType;
move_uploaded_file($_FILES['file']['tmp_name'], $newFileName);
$unique_id = rand(1000000,9999999);

shell_exec("C:\\ffmpeg\\bin\\ffmpeg.exe -i ".$newFileName." -vcodec libx264 -crf 20 \"upload\\{$newFileName}\" > logfile.txt 2>&1");

/// save information into database
                $username = "root";
                $password = "";
                $hostname = "localhost"; 
                $dbname = "test_database";

                //connect to the database
                $dbc = mysqli_connect($hostname, $username, $password, $dbname) or die ("could not connect to the database");

                //execute the SQL query and return records
                 $result = mysqli_query($dbc, "INSERT INTO `viewvideo` (`vID`, 'video_id`, `video_link`) VALUES ('', '".$unique_id."', '".$newFileName."')");

                if(!$result){echo mysqli_error($dbc); }

                echo $result;

            /*  
                declare in the order variable
                $result = mysqli_query($dbc, $sql); //order executes
                if($result){
                    echo("<br>Input data is succeed");
                } else{
                    echo("<br>Input data is fail");
                }
            */  

                //close the connection
                mysqli_close($dbc);

输出 enter image description here

1 个答案:

答案 0 :(得分:0)

请记住将上传的文件移动到您选择的目录中。防止文件相互覆盖的方法是为其创建新名称。上传成功时执行此操作。

正确上传&amp;将视频添加到数据库

$target_dir = "video/"; //where you want to upload the files to
$target_file = $target_dir.basename($_FILES['file']['name']);
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
$newFileName = $target_dir.sha1(pathinfo(basename($_FILES['file']['name']), PATHINFO_FILENAME)).'-'.time().'.'.$fileType;
move_uploaded_file($_FILES['file']['tmp_name'], $newFileName);

之后你必须创建一个表&amp;插入两件核心东西;视频和$newFileName路径的唯一/令牌生成ID。

您可以使用生成包含字母数字字符的令牌/ ID的函数,也可以使用简单的函数 $unique_id = rand(1000000,9999999);

让我们考虑你有一个包含3列的表videos; vIDvideo_id&amp; video_link

vID应该是一个自动递增的INT。 video_id将是INT,video_link是TEXT类型。

之后是SQL。

$result = mysqli_query($db_connection, "INSERT INTO `videos` (`vID`, video_id`, `video_link`) VALUES ('', '".$unique_id."', '".$newFileName."'");
if(!$result){echo mysqli_error($db_connection); }

检索会是这样的。请务必在另一页上添加此内容。我们将 stream.php 视为流式传输视频的网页名称。

if(isset($_GET['id'])){
    $id = trim($_GET['id']);
    //check if it exists
    $result = mysqli_query($db_connection , "SELECT `video_id`, `video_link` FROM `videos` WHERE `video_id`='".$id."'");
    $count = mysqli_num_rows($result);
    //does it exist?
    if($count>0){
        //exists, so fetch it in an associative array
        $video_arr = mysqli_fetch_assoc($result);
        //this way you can use the column names to call out its values. 
        //If you want the link to the video to embed it;
        echo "Video Link: ".$video_arr['video_link'];
    }else{
        //does not exist
    }
}

最后,创建一个指向它的链接:http://your-website.com/stream.php?id=video id here