Uploading and Playing Video

Hi,

I want to ask some help,I am working in localhost in my p.c…After uploading my video which is .mp4 format…The video is save in the folder after uploading in this path C:\wamp\www\myWeb\videoSave., Also the name,size,type and the temp_file of the video is stored in the database…Now i retrieve the Names of the video in the Database,and display this in the table…what i want to achieve is that when i click a certain name of the video which is displayed in the table,this will play the video.is this possible?please help me.

Thank you in advance.

Hi jemz,

This is possible in a number of different ways. If using html 5 you could do something like:


// Do your database retrieval, in this example $data has your results as a multi-dimensional array.

$video_file = $data[0]['video_file'];
/*
* You could store the src, type, and codec width and height in the database.
*/
<video preload controls>
            <source src="./video/<?php $video_file ?>.mp4" type='video/mp4;' codecs="avc1.42E01E, mp4a.40.2"' />
            <source src="./video/<?php $video_file ?>.webm" type='video/webm;'  codecs="vp8,vorbis"'/>
            <source src="./video/<?php $video_file ?>.ogv" type='video/ogg;' codecs="theora,vorbis"'/>
            <object type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" width="640" height="352">
              <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />
              <param name="allowFullScreen" value="true" />
              <param name="wmode" value="transparent" />
              <param name="flashvars" value='config={"clip":{"url":"http://www.domain.com/<?php $video_file ?>.flv","autoPlay":false,"autoBuffering":true}}'/>
              <!-- Image Fallback. Typically the same as the poster image. -->
            </object>
</video>

The above will provide the default html 5 video controls you can style this you have to just read up on it.

In HTML 5 video you can have the file auto play; although it is normally not recommended. Generally it is better to allow people to choose when to start the video.

There are also a number of video players that you can embed and with a Flash Video Player you can stream the file; whereas with HTML 5 video you can’t. I like using HTML 5 video because you can scale videos using CSS with good performance and not too bad of degradation. This is far more difficult to do with embedded players.

Steve

Hi Server Storm,

Thank you for the reply and helping me on my problem,I will try your solution and i will write back as soon as possible.

Hi, about this $video_file = $data[0][‘video_file’]; what is inside the $data[0]['video_file]?you mean is that the field of my database?..I do apologize i am confuse.can you please enlighten me on this.

Thank you in advance.

Hi,

Yes the $data[0’[‘video_file’] would come from a field in your database.

Your database SELECT query might look like:


SELECT
  video_name
  , video_file
  , type
  , codec 
FROM
 videos
INNER 
  JOIN videos2users as v2u
    ON v2u.video_id = video_id
INNER 
  JOIN users as usr
    ON usr.id = v2u.user_id
WHERE 
  usr.id = 107

This query shows three tables videos, users, and video2users being joined. We select the video_name, video_file, type, and codec fields from the video tables. The records that are returned all belong to user 107 (typically a user would be specified as a variable, so when a user uploads their video and subsequently wants to display them you would pass the user id in a variable);

So if you where using PDO to connect to your database you could do this:


$user_id = $_POST['user_id']; // Get the user id from either a video search or upload page.
$sql ="
SELECT
  video_name
  , video_file
  , type
  , codec 
FROM
 videos
INNER 
  JOIN videos2users as v2u
    ON v2u.video_id = video_id
INNER 
  JOIN users as usr
    ON usr.id = v2u.user_id
WHERE 
  usr.id = :usr_id";
$stmt = $o_Db->prepare($this->sql); // Prepare the sql using PDO prepare
$stmt->bindParam(':usr_id', $user_id);
$stmt->execute();$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);

The var_dump($results) might give you:

array(2) {
    [0]=> array(5) { 
        ["video_id"]=> integer "10236" 
        ["video_name"]=> string(5) "sharks" 
        ["video_file"]=> string(13) "./videos/animals/ocean/sharks.mp4"; 
        ["type"]=> string(10) "video/mp4;" 
        ["codec"]=> string(22) "avc1.42E01E, mp4a.40.2" 
    } 
    [1]=> array(5) { 
        ["video_id"]=> integer "10237" 
        ["video_name"]=> string(5) "sharks" 
        ["video_file"]=> string(13) "./videos/animals/ocean/sharks.webm"; 
        ["type"]=> string(10) "video/webm;" 
        ["codec"]=> string(22) "vp8,vorbis" 
    } 
}}

Steve

Hi ServerStorm,

Thank you for this idea…i will try again this.I hope i can achieve now…I will write back to you as soon as possibe.
Thank you and more power to you always.

Hi Steve, It’s working now i can play now the video Thank you so much for helping me…but i have a little problem on the 2D Array i don’t know how to use like this $video_file = $data[0][‘video_file’];

I don’t know how to insert that code or to apply that after you selecting the table in the database…can you please show me how to do this…Thank you in advance and i am hoping for your positive response.

Hi jemz,

When your results come back from the db, it will be in a multi-dimensional array; in other words a number of arrays that each contain a complete set of rows that have been returned on your query.

If my query returns two videos, my attributes for the videos like name, type, codec will be in each row. My first record (if I return the query to a variable $results) will contain two arrays that contain the fields for both video files so I might do:


$html_widget = '<video preload controls>';
foreach ($results as $row){
  $file_name = '';
  $type = '';
  $codecs = '';
  foreach($row as $key => $value){
     switch($key){
       case 'file_name':
         $file_name = $value;
         break;
       case 'type':
         $type = $value;
         break;
       case 'codecs':
         $codecs = $value;
         break;
     }
  }
  $html_widget .= '<source src="./video/<?php $file_name ?>" type="<?php $type ?>" codecs="<?php $codecs />"';
}
$html_widget .= '</video>';

Hi,

Thank you for the patience and giving more some examples…Okay i understand a little bit…I need more practice and i will try to apply your code…Thank you so much server storm…I will write again to you if i have doubt.