Build a YouTube clone

07 October 2007 video, webdev

YouTube and all other sites of video sharing are all based on the conversion of video file in the FLV format (Flash Video) and the FLV files will be reproduced via a Flash player embedded in a web page. In the end of 2006 I have played with some free and open source software with the scope to verify if it’s possible to realize a test application like YouTube with no software cost.

It’s better to make clear that you can’t do a video sharig site with a normal hosting for obvious reasons:

  • Software: you must install some software and in hosting this is not permitted;
  • CPU power: file conversion is a heavy work and in a shared server it’s a no-no;
  • Bandwidth: the server must receive in upload the (big) video files and must stream all the video seen by the visitors; these flows require a stratosferic daily badwidth for a pupular site.

Now you know you can’t do your personal YouTube. Continue reading?

The software I used:

  • mencoder (part of mplayer): conversion in FLV format;
  • FLV Player the player to embed in web pages;
  • ffmpeg generation of thumbnails

The test has been made on a FreeBSD 6.0 machine. Test site (front-end and back-end) has been powered by PHP 4.4.5 and MySQL 4.1.21.

Inspiration: Video Blogging using Django and Flash(tm) Video (FLV)

How it works

It’s like a photo gallery. All the information of the video (title, date, etc) are saved on the database and the files are saved on disk.

In brief this is the workflow:

  • a user upload a video file (avi, mpg, mov, …) and a record is inserted in the database;
  • every 5 minuts a cronjob check if there are some new files to convert;
  • if yes, some scripts (PHP and Bash) are executed to convert the video in FLV, generate a thumbnail and delete the original file;
  • in the database the record is updated and the video became available on the site for viewing.

The crontab calling convert.php:

bc. 0-55/5 * * * * /usr/local/bin/php /path/convert.php > /dev/null

convert.php will call some other scripts for convertion and thumbnail. It’s useless to report all this script, but only the interesting parts:

Encoding the video (encode.sh):

/usr/local/bin/mencoder video_orig.avi -o video.flv
-lavfopts i_certify_that_my_video_stream_does_not_use_b_frames
-of lavf
-oac mp3lame
-lameopts abr:br=56
-ovc lavc
-lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3
-vf scale=320:240
-srate 22050
-msglevel all=0 > /dev/null 

The video will be resized to 320×240 pixels.

Thumbnail (thumbs.sh):

/usr/local/bin/ffmpeg -i video.flv -vcodec png -vframes 2 -an -f rawvideo -s 120x90 thumb.png 

The thumbnail is generated in PNG format with dimensions 120×90 pixels.

After the cronjob work, the video is available. How to put it on the site? Suppose you have a page play.php to which pass the video ID as a querystring with a url like www.example.com/play.php?videoid=1; with this videoid you must retreive the name file from the database and pass it to the FLV player:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="320" height="260">
<param name="movie" value="flvplayer.swf" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param name="BGCOLOR" value="#181818" />
<param name="flashvars" value="file=video.flv&showdigits=true&autostart=true&showfsbutton=false" />
<embed
src="flvplayer.swf" mce_src="flvplayer.swf"
width="320"
height="260"
quality="high"
scale="noscale"
bgcolor="#181818"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
flashvars="file=video.flv&showdigits=true&autostart=true" />
</object> 

Note: the code embed the player, not the video. The video file must be passed through the flashvars variable.

The fundamental it’s all here. All the rest it’s a normal web app with an admin back-end, nothing real different from a photo gallery with some users that make login and upload ffiles.

This test app runs well on my home server, but it’s an abandoned project. My only interest was to understand what’s behind the scene of the 2006 bomb: video sharing. It’s a draft, all the work to make it a real application (security, load balancing, efficient storing, …) are beyond my scope. I never be able to put on-line a video sharing service.

After moths of oblivion I feel the desire to publish my study.

Screenshot

Here there are some screenshots of the application. I named it “MyTube”. Yes , I know, it’s not a beautiful appeal, but it’s only a prototype.

Final considerations

Google spent a big amount of money to acquire YouTube but not for the superlative coding behind the scene (banal indeed, as you see) but for the enormous users base. Google has bought YouTube’s users, not YouTube’s application. YouTube founders deserve the merit (well refunded) to have taken advantage of this idea.

The video on-line sector is saturated, Web Video Cheat Sheet reports over 80 sites all similar. There are the niches, but are they profitable? Probably only the porno.

Hardware, bandwidth and professional resources involved in a big project of video sharing require big investments. It’s impossible start with a hobby project hoping to be acquired like del.icio.us, you must start with a bunch of money from the beginning.

Many sites of video sharing use free and open source software, just like in my test. I hope that who earns money from video sharing give some donations to the developers of these softwares, considering without them a large part of Web2.0 would not exist.

Comments

17/03/08 00:57 - Eyal Levi

Contact me.
I want to open my own web page.
eyalevisun@gmail.com

10/08/08 06:10 - Tdf

I am very interested in knowing how you built this app, i would like something similar to incorporate in to my website for members to use.

09/09/10 23:32 - incomen

Great tutorial, even though I don’t think I can do it, is great to know that you can clone youtube. Thanks!