【问题标题】:retrieving video from database using php使用php从数据库中检索视频
【发布时间】:2016-02-03 01:30:51
【问题描述】:

我试图弄清楚在用户使用 html 表单选择视频后如何从我的数据库中检索 video_link。 2个视频被上传到我的数据库中,原始视频和一个压缩视频,取决于用户连接。适当的视频将嵌入到 viewvideo.php 的 html 5 视频标签中,但是因为 ajax 期望在 $( "#speed" ).val( html ); 中得到响应,我不认为我可以嵌入viewvideo.php 中的视频

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

这是我现在的编码。

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

HTML格式

<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({
      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);
        ?>

【问题讨论】:

    标签: php jquery html ajax video


    【解决方案1】:

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

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

    例如

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

    然后在 HTML 中

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

    在 URL 中嵌入视频数据的缺点是加载 HTML 可能需要很长时间。

    【讨论】:

    • 这不能回答我的问题...如果我使用表单帖子而不是 ajax 帖子发送表单,我可以嵌入视频。当我使用 ajax $("#speed").val(html);期望返回值,因此我在 viewvideo.php 中的嵌入视频不起作用
    猜你喜欢
    • 2018-08-15
    • 2014-11-15
    • 2011-08-25
    • 2021-02-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多