1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Play videos using php

Discussion in 'PHP' started by moin, Apr 23, 2007.

  1. #1
    Hello friends,

    How to play videos using php, could anyone suggest me code to play the videos.

    For example, I have a video "abc.mpg", i want to play it in the browser using php code.

    I need help asap.

    Thanks.
     
    moin, Apr 23, 2007 IP
  2. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    videos don't play with php. you html to play a video, windows media player code, or quicktime, real player, or what would be easier would be flash. try uploading the video to youtube and just embedding their player into your page
     
    pepsipunk, Apr 24, 2007 IP
  3. erotomania

    erotomania Banned

    Messages:
    291
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    why using php while you can embed a video using html ?
     
    erotomania, Apr 24, 2007 IP
  4. moin

    moin Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No, I want to play the videos dynamically, for example

    video.php?abc.mpg

    how to retrieve the abc.mpg from the file name and play the video.
     
    moin, Apr 24, 2007 IP
  5. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #5
    if you want something simple videos would be accessed from video.php?v=abc.mpg or whatever

    
    <?
    $videos = array();
    $videos["abc.mpg"] = "http://example.com/vids/abc.mpg";
    $videos["sample.avi"] = "http://example.com/sample.avi";
    $chosen = $_GET['v'];
    echo "<embed src=".$videos[$chosen]."></embed>";
    ?>
    
    PHP:
    otherwise you can use a mysql database to retrieve values dynamically as you add them to the database

    but you need to embed the html into pages to watch videos. using youtube would be ideal, they have a very simple universal way of playing videos and they give you the embed code
     
    pepsipunk, Apr 24, 2007 IP
  6. moin

    moin Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can you suggest me the database code for it.

    Thanks.
     
    moin, Apr 24, 2007 IP
  7. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #7
    I'm not very skilled when it comes to mysql databases. I can't remember the code off hand to access these. You would also have to write the code to your specifications. Sorry
     
    pepsipunk, Apr 24, 2007 IP
  8. Chamaro Zwinkels

    Chamaro Zwinkels Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can make a database with following fields (for example: videos):

    - id
    - name
    - file

    Then you have an url: www.domain.com/video.php?id=1

    You can use a script like this:

    <?php
    if(empty($_GET[id]) OR !is_numeric($_GET[id])){
      echo 'Error';
    }else{
      $query=msyql_query("SELECT id,name,file FROM videos WHERE id='" . $_GET[id] . "' LIMIT 1");
      if(mysql_num_rows($query)==0){
        echo 'Error.';
      }else{
        $vid=mysql_fetch_object($query);
        $file=$vid->file;
        // put here your html
      }
    }
    ?>
    PHP:
     
    Chamaro Zwinkels, Apr 24, 2007 IP
  9. Parth01

    Parth01 Greenhorn

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #9
    Video Playing php Code :-

    <figure class="style-img-2 fleft">
    <a class="lightbox-image" href="video/video_AS3.swf?width=495&height=275&fileVideo=aa.flv" data-gal="prettyVideo[prettyVideo]">
    <img src="images/page1-img2.jpg" alt=""></a></figure>
     
    Parth01, Nov 6, 2015 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    @Parth01 You do realise that the code you posted is... wow. Incredibly bad, wrong, and completely useless in a real-life environment that does NOT apply lightbox, right? Also, with no instructions, how is the OP supposed to know that the image doesn't magically appear? And... just. No. Go away. Come back when you have something useful to contribute.
     
    PoPSiCLe, Nov 6, 2015 IP
    Vooler likes this.
  11. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #11
    Well you could have at least told the OP that the first guy's code was horrible also. @Chamaro Zwinkles
     
    KangBroke, Nov 7, 2015 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    Most of the code in this thread is horrible, so yes.
     
    PoPSiCLe, Nov 8, 2015 IP
  13. PaulLiu

    PaulLiu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    you can use html5 video tag to play video and change the src dynamically.
    <video id="myVideo" width="500" height="280"
      controls autoplay poster="image/poster.png">
    <source src="" type="video/mp4" />
    <em>Sorry, your browser doesn't support HTML5 video.</em>
    </video>
    
    <?php
    $videos[0]="http://somedomain1.com/video1.mp4";
    $videos[1]="http://somedomain2.com/video2.mp4";
    if(isset($_REQUEST['vid'])){
      $vid=$_REQUEST['vid'];
      //put your code here to convert $vid to $url of the video, e.g.
      $url=$videos[$vid];
    }
    ?>
    <script>
      var vid=document.getElementById('myVideo');
      vid.src='<?php echo $url?>';
    </script>
    HTML:
     
    PaulLiu, Nov 25, 2015 IP
  14. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #14
    Why are we assuming he wants to build some sort of infrastructure with PHP to allow a visitor to search and play thousands of videos? He didn't ask that. His question was simple... the answer is you can't directly play a video through PHP. You would use HTML5 or JavaScript or embed a player.
     
    NetStar, Nov 29, 2015 IP
  15. mrbrown123

    mrbrown123 Active Member

    Messages:
    550
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #15
    This is a simple way to play files in the way you asked. This player I use and is perfect. http://codecanyon.net/item/ultimate-video-player/7694071
     
    mrbrown123, Nov 30, 2015 IP
  16. PaulLiu

    PaulLiu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    Maybe Moin is asking for a php way to render or stream video, it is not that hard, here is the code
    <?php
    $browser = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36";
    $url=$_REQUEST['url'];
    play($url, "video/mp4");
    
    function play($file, $content_type = 'application/octet-stream') {
        global $start, $end, $length;
        @error_reporting(0);
    
        $opts = array(
            'http'=>array(
                'method'=> "GET",
                'seekable'=> true
            )
        );
    
       /*** Sends an http request to $file ***/
        $ctx = stream_context_create($opts);
        if (!($fp = fopen($file, 'r', false, $ctx))) {
            header("HTTP/1.1 404 Not Found");
            exit;
        }
        // Get file size
        $meta = stream_get_meta_data($fp);
        $jsonStr=json_encode($meta);
        $jsonStr=substr($jsonStr,strrpos($jsonStr, "Content-Length:"));
        preg_match("#Content\-Length:\s(\d+)#",$jsonStr,$matches);
        $filesize=$matches[1];
    
        $range=false;
        // Handle 'Range' header
        if(isset($_SERVER['HTTP_RANGE'])){
            $range = $_SERVER['HTTP_RANGE'];
        }else if($apache = apache_request_headers()){
            $headers = array();
            foreach ($apache as $header => $val){
                $headers[strtolower($header)] = $val;
            }
            if(isset($headers['range']))
                $range = $headers['range'];
            else $range = FALSE;
        //Is range
        $partial = false;
        if(strlen($range)>1){
            $partial = true;
            list($param, $range) = explode('=',$range);
            // Bad request - range unit is not 'bytes'
            if(strtolower(trim($param)) != 'bytes'){
                header("HTTP/1.1 400 Invalid Request");
                exit;
            }
            // Get range values
            $range = explode(',',$range);
            $range = explode('-',$range[0]);
            // Deal with range values
            if ($range[0] === ''){
                $end = $filesize - 1;
                $start = $end - intval($range[0]);
            } else if ($range[1] === '') {
                $start = intval($range[0]);
                $end = $filesize - 1;
            }else{
                // Both numbers present, return specific range
                $start = intval($range[0]);
                $end = intval($range[1]);
               // Invalid range/whole file specified, return whole file
                if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) $partial = false;
            }
            $length = $end - $start + 1;
        } else $partial = false; // No range requested
        // send extra headers for range handling...
        if ($partial) {
        // Send standard headers
            header("Content-Type: $content_type");
            header("User-Agent: $browser");
            header('HTTP/1.1 206 Partial Content');
            header('Accept-Ranges: bytes');
            header("Content-Length: $filesize");
            header("Content-Range: bytes $start-$end/$filesize");
            header("Cache-Control: max-age=2592000, public");
            header("Content-Transfer-Encoding: binary");
            header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
            header('Cache-Control: private');
            header('Access-Control-Allow-Origin: *');
            header("Content-Disposition: inline;");
            header('Connection: close');
            ob_clean();
            flush();
            $seek_range=$start;
            if($seek_range>0){
               if($seek_range<=PHP_INT_MAX)
                 stream_get_contents($fp,1,$seek_range);
               else {
                 while($seek_range>PHP_INT_MAX){
                    stream_get_contents($fp,PHP_INT_MAX);
                    $seek_range -= PHP_INT_MAX;
                 }
                 stream_get_contents($fp,1,$seek_range);
               }
           }
           while($length){
                set_time_limit(0);
                $read = ($length > 2048) ? 2048 : $length;
                $length -= $read;
                print stream_get_contents($fp, $read);
           }
           exit;
        }
        readstream($fp, $content_type, $filesize); //just send the whole file
        fclose($fp);
    }
    
    function readstream($fp, $content_type = 'application/octet-stream', $filesize) {
        global $start, $end, $length;
        if($start>0||$end<$filesize)
            header('HTTP/1.1 206 Partial Content');
        else
           header('HTTP/1.0 200 OK');
        header("Content-Type: $content_type");
        header("User-Agent: $browser");
        header('Accept-Ranges: bytes');
        header("Content-Length: $filesize");
        header("Content-Range: bytes $start-$end/$filesize");
        header("Cache-Control: max-age=2592000, public");
        header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
        header('Access-Control-Allow-Origin: *');
        header("Content-Transfer-Encoding: binary");
        header('Content-Description: File Transfer');
        header('Cache-Control: private');
        header('Connection: close');
        header("Content-Disposition: inline;");
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        ob_clean();
        flush();
        $cur=$start;
        while(!feof($fp)&& $cur<$end && (connection_status()==0)){
           $read=min(1024*16,$end-$cur);
           print stream_get_contents($fp, $read);
           $cur+=$read;
           usleep(1000);
        }
    
    }
    ?>
    PHP:
     
    PaulLiu, Dec 4, 2015 IP