PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Creating MyTube with Flex and PHP

by Jack Herrington

With widespread broadband adoption, the continued shrinking cost of disk space, and the availability of the Adobe® Flash® Player and Flash video, it's no wonder that video sharing on the Internet is insanely popular. Sites like Google video and YouTube got the ball rolling, but now there are niche video sites everywhere. So, how can you get in on the action? How can you use technologies such as PHP, Flash, and Adobe Flex™ to build your own video sharing site? Turns out, it's a lot easier than you think.

This article walks you through building the PHP portion of the site as well as building a movie viewer in Flash using the Flex framework. To build a simple version of YouTube (or a MyTube, as it were), you should have a couple of tools in place.

On the server side, you first need PHP and MySQL. You'll use MySQL to store the data about the movies (for example, the file name for the movie, the thumbnail file, the height and width of the thumbnail, the title, and the description). PHP will do the work of formatting pages, both HTML and XML, depending on how you want to do it.

You also need an open source utility called ffmpeg to do the work of converting the video from whatever format your user uploads into a Flash Video (FLV) file. The ffmpeg utility can also generate a thumbnail snapshot of a frame from the movie for use when you present the list of available movies. Lest there be any doubt, ffmpeg is your new best friend in the world of video sharing. It's an amazing utility that is powerful, easy to use, and well documented.

On the frontend, I show several different user interface approaches. The first uses a hybrid HTML/Flash approach similar to YouTube. The second approach uses a completely Flash-based interface. For both of these approaches, I use the Flex framework to build the Flash applications that view the video and later to list the available videos and provide navigation.

Building the PHP Backend

To start building the backend, you must put together the database schema for MySQL. First, create the database. To do so, you use the mysqladmin command:

mysqladmin create movies

When that's done, load the database with the schema. The schema file is shown in Listing 1.

Listing 1. movies.sql

    title VARCHAR( 255 ),
    source VARCHAR( 255 ),
    thumb VARCHAR( 255 ),
    width INTEGER,
    height INTEGER,
    PRIMARY KEY( movieId )    

You load the schema file into the database like this:

mysql movies < movies.sql

To get data into the database, you need an HTML upload facility that takes the uploaded movies, converts them into Flash video, gets a thumbnail, and adds that to the database.

Building the Upload Page

The HTML page for uploading videos is actually quite simple, as Listing 2 shows.

Listing 2. addmovie.html
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<tr><td>Title</td><td><input type="text" name="title"></td></tr>
<tr><td>Movie</td><td><input type="file" name="movie"></td></tr>
<input type="submit" value="Upload" />

The form on this page goes to the upload.php page, which processes the movie, grabs a thumbnail, and adds the data to the database. This PHP script is shown in Listing 3.

Listing 3. upload.php
require "DB.php";

function converttoflv( $in, $out )
  unlink( $out );
  $cmd = "ffmpeg -v 0 -i $in -ar 11025 $out 2>&1";
  $fh = popen( $cmd, "r" );
  while( fgets( $fh ) ) { }
  pclose( $fh );

function getthumbnail( $in, $out )
  unlink( $out );
  $cmd = "ffmpeg -i $in -pix_fmt rgb24 -vframes 1 -s 300x200 $out 2>&1";
  $fh = popen( $cmd, "r" );
  while( fgets( $fh ) ) { }
  pclose( $fh );

function flv_import( $upfile, $fname, $title )
  $fname = preg_replace( '/\..*$/', '', basename( $fname ) );
  $flvpath = "$fname.flv";
  $thumbpath = "$fname.gif";

  converttoflv( $upfile, "movies\\$flvpath" );
  getthumbnail( $upfile, "movies\\$thumbpath" );

  $dsn = 'mysql://root@localhost/movies';
  $db =& DB::connect( $dsn );
  if ( PEAR::isError( $db ) ) { die($db->getMessage()); }

  $sth = $db->prepare( 'INSERT INTO movies VALUES ( 0, ?, ?, ?, ?, ? )' );
  $db->execute( $sth, array( $title, $flvpath, $thumbpath, 300, 200 ) );

flv_import( $_FILES['movie']['tmp_name'], $_FILES['movie']['name'], $_POST['title'] );
File sucessfully uploaded

The flv_import() function is the heart of the script. It calls out to the converttoflv() and getthumbnail() functions to convert the movie to a Flash video file and create a thumbnail. It then adds a record to the database that references the movie. Both the FLV and thumbnail functions use the ffmpeg command with various command-line options to convert the work with the video.

Pages: 1, 2, 3, 4

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: