태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.
페이지를 읽고 있습니다. ( 아쿠아바다's Blog )
분류 전체보기 (769)
쉐어포인트 (24)
Exchange (12)
SQL (121)
XML (36)
WEB (294)
O / S (97)
삶의향기 (162)
기획 (19)
RSS 피드(IE 7.0부터 기본 지원됩니다. 이전 버전 사용자는 접합한 툴을 사용하세요!!)

Creating MyTube with Flex and PHP(1/4)

O / S/Linux 2007/07/06 11:01 by 아쿠아바다

Creating MyTube with Flex and PHP

by Jack Herrington
05/24/2007

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
DROP TABLE IF EXISTS movies;

CREATE TABLE movies (
    movieId INTEGER NOT NULL AUTO_INCREMENT,
    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
<html>
<body>
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<table>
<tr><td>Title</td><td><input type="text" name="title"></td></tr>
<tr><td>Movie</td><td><input type="file" name="movie"></td></tr>
</table>
<input type="submit" value="Upload" />
</form>
</body>
</html>

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
<html><body>
<?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
</body></html>

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.

1 2 3 4

좀더 흥미로운 내용이 많이 있습니다.. HOME > O / S/Linux를 확인하세요
TAG , , ,   
0 Trackback, 0 Comment, :
1  ... 143 144 145 146 147 148 149 150 151  ... 769 
Statistics Graph
Total : 557,110 Today : 171