使用php从数据库中检索视频 [英] retrieving video from database using php

查看:110
本文介绍了使用php从数据库中检索视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何在用户使用html表单选择视频后从数据库中检索video_link。 2视频上传到我的数据库,原始视频和压缩视频,取决于用户连接。但是,由于ajax预计会在 $(#speed).val(html)中做出响应; 我认为我可以嵌入视频中的视频标签video in viewvideo.php



所以我的问题是我现在应该做什么?我应该在哪里检索video_link并嵌入视频?



这是我的编码。



html表单,用于检索用户希望观看的用户连接速度和video_id,并使用ajax发布表单数据。

HTML表单

 < form action =viewvideo.phpmethod =post> 
< br />
请选择视频
< br />

< select name =video_id>
<?php
while($ row = mysqli_fetch_array($ result))
{
?>
< option value =<?php echo $ row ['video_id']?>>
<?php echo $ row ['videoname']?>
< / option>
<?php
}
?>
< / select>

< br />
< input type =textid =speedname =speedvalue =>
< input type =Submitid =Submitvalue =Submit/>
< / form>

Ajax

  $ .ajax({
method:POST,
url:viewvideo.php,
data:{speedMbps:speedMbps,
video_id:$( '[name =video_id')。val()},
cache:false
})。done(function(html){
$(#speed).val html);
});

viewvideo.php

  if(isset($ _ POST ['video_id'])& isset($ _ POST ['speedMbps'])){
$ id = trim($ _ POST ['video_id'] );
$ speed = $ _POST ['speedMbps'];
echo $ id;

$ result = mysqli_query($ dbc,SELECT`video_id`,`video_link` FROM`video480p` WHERE`video_id` ='。$ id。');
$ count = mysqli_num_rows($ result); (($ speed< 100)&&($ count> 0)){//如果用户速度小于100则检索480p quailtiy video



) //它是否存在?
// if($ count> 0){
// exists,所以在关联数组中获取它
$ video_480p = mysqli_fetch_assoc($ result);
//这样你可以使用列名称来调出它的值。
//如果您想要视频的链接嵌入它;
echo $ video_480p ['video_link'];
}

else {
//不存在
}

?>

< video id =videowidth =640height =480controls autoplay>
< source src =<?php echo $ video_480p ['video_link'];?>类型= 视频/ MP4 >
您的浏览器不支持视频标签。
< / video>
< br />

<?php

$ result2 = mysqli_query($ dbc,SELECT`video_id`,`video_link` FROM`viewvideo` WHERE`video_id` ='。$ VIDEO_ID ')。
$ count2 = mysqli_num_rows($ result2);

//检索原始视频
if(($ speed> = 100)&&($ count2> 0)){
//是否存在?
// if($ count2> 0){
// exists,所以在关联数组中获取它
$ video_arr = mysqli_fetch_assoc($ result2);
//这样你可以使用列名称来调出它的值。
//如果您想要视频的链接嵌入它;
echo $ video_arr ['video_link'];
}
else {
//不存在

}
?>

< video id =videowidth =640height =480controls autoplay>
< source src =<?php echo $ video_arr ['video_link'];?>类型= 视频/ MP4 >
您的浏览器不支持视频标签。
< / video>
< br />

<?php

mysqli_close($ dbc);
?>


解决方案

如果您希望视频可供用户访问,您必须将实际的视频(MP4)文件放在浏览器可以下载它们的公共目录中。因此,video_link必须包含用户可访问的实际URL。如果视频不在公共目录中,则用户无法直接访问它们,或者可以使用data:protocol嵌入视频二进制数据。

  data:[< MIME-type>] [; charset =< encoding>] [; base64],< video data> b 




例如code><?php
function getVideoURLString($ file,$ type){
return'data:video /'。 $类型。 '; base64,'。
base64_encode(file_get_contents($ file));
}
?>

然后在HTML中

 < video ...> 
< source type =video / mp4src =<?php echo getVideoURLString($ filename,mp4);>
< / video>

在URL中嵌入视频数据有一个缺点,即加载HTML可能需要很长时间。


I am trying to figure out how to retrieve video_link from my database after users select the video using a html form. 2 videos are uploaded into my database, the original video and a compressed video, depending on user connection. The appropriate video will be embed in html 5 video tag in viewvideo.php however because ajax is expecting a response in $( "#speed" ).val( html ); i dun think i can embed the video in viewvideo.php

So my question is what should i do now? where should i retrieve the video_link and embed the video?

This is my coding now.

I have a html form that retrieve user connection speed and video_id that user wish to watch and using ajax to post the form data

Html form

<form action="viewvideo.php" method="post" >
                <br/>
                Please select the video
                <br/>

                <select name="video_id">
                    <?php
                        while($row = mysqli_fetch_array($result))
                        {
                    ?>       
                                      <option value="<?php echo $row['video_id']?>">
                    <?php echo $row['videoname']?>
                                      </option>         
                    <?php
                        }
                    ?>
                </select>

                <br />  
                <input type="text" id="speed" name="speed" value="">
                <input type="Submit" id="Submit" value="Submit" />
                </form>

Ajax

  $.ajax({
      method: "POST",
      url: "viewvideo.php",
      data: {speedMbps: speedMbps,
      video_id: $('[name="video_id"').val()},
      cache: false
    }).done(function( html ) {
        $( "#speed" ).val( html );
});

viewvideo.php

  if(isset($_POST['video_id']) && isset($_POST['speedMbps'] )){
                        $id = trim($_POST['video_id']);
                        $speed = $_POST['speedMbps'];
                        echo $id;

                        $result = mysqli_query($dbc , "SELECT `video_id`, `video_link` FROM `video480p` WHERE `video_id`='".$id."'");
                        $count = mysqli_num_rows($result);

                        if (($speed < 100) && ($count>0)) {     //if user speed is less than 100 retrieve 480p quailtiy video   

                            //does it exist?
                            //if($count>0){
                                //exists, so fetch it in an associative array
                                $video_480p = 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_480p['video_link'];                     
                                }

                            else{
                                //does not exist
                            }

        ?>

                        <video id="video" width="640" height="480" controls autoplay>
                        <source src="<?php echo $video_480p['video_link']; ?>" type="video/mp4">
                        Your browser does not support the video tag.
                        </video>
                        <br />

                        <?php

                        $result2 = mysqli_query($dbc , "SELECT `video_id`, `video_link` FROM `viewvideo` WHERE `video_id`='".$video_id."'");
                        $count2 = mysqli_num_rows($result2);

                        // retrieve original video
                         if (($speed >= 100) && ($count2 >0)) { 
                                //does it exist?
                                    //if($count2>0){
                                        //exists, so fetch it in an associative array
                                        $video_arr = mysqli_fetch_assoc($result2);
                                        //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_arr['video_link'];                      
                                        }
    else{
                                    //does not exist

                                }
        ?>

                        <video id="video" width="640" height="480" controls autoplay>
                        <source src="<?php echo $video_arr['video_link']; ?>" type="video/mp4">
                        Your browser does not support the video tag.
                        </video>
                        <br />

        <?php

                    mysqli_close($dbc);
        ?>

解决方案

If you want video to be accessible to user, then you must put actual video (MP4) files in public directory where browser can download them. So video_link must contains actual URL accessible to user. If videos are not in public directory, then user cannot access them directly or you can embed video binary data in URL with data: protocol

 data:[<MIME-type>][;charset=<encoding>][;base64],<video data>

For example

<?php
    function getVideoURLString($file, $type) { 
         return 'data:video/' . $type . ';base64,' .
                base64_encode(file_get_contents($file)); 
    }
?>

Then in HTML

<video ...>        
     <source type="video/mp4" src="<?php echo getVideoURLString($filename, "mp4");"> 
</video>

Embedding video data in URL has drawback that it may takes very long time to load HTML.

这篇关于使用php从数据库中检索视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆