Vipin's cogitations: How to make video streaming site's like youtube

Vipin’s cogitations: How to make video streaming site’s like youtube.

How to make video streaming site’s like youtube

Video streaming really a very awesome and a very new type of technology ,we experience at Webkul. Every time million’s of user around the world open the YouTube but you know how these video sites work .There is a lot of technologies on streaming media , we will explain some of them :


Concept:
So how this video streaming works .Mainly you can stream your data on two standard protocols

Protocols:

1- HTTP
2- RTSP+RTP


Helper Media(Video Player):

For a normal site you can stream your media(video) over HTTP but the main disadvantage with HTTP is stateless ,HTTP is a stateless protocol and HTTP don’t know about the state of the helper(Helper means video player ) . There are so many free open source video player but we at webkul are using Flowplayer .

Flow player is really an awesome video player ,you can easily customize and mold as per your need. There are so many conditions for making a streaming a video site like

1- You are using a hosting web space
2-You are using a dedicated server
3- others 😉

Actually there are some software for the encoding and decoding of your media file . Generally all the video player(flow player) supports .flv extension for streaming , means you can not stream your .AVI or .DAT extension with video player like flow-player . so thats the reason that you must have a encoding-decoding (conversion) software

Video Conversion:

This is the very most important aspect for making a video site . If you don’t know how to convert videos in to desired format then its really hard to make a video site

Software Requirement and platform:

There are so many open source software for the video conversion like ffmpeg,mencoder etc. We are using FFMPEG for the video conversion really very handy and easy to use .

we are using Ubuntu (Linux) operating system as a platform for the video site .Ubuntu is simply awesome ,very user friendly , very large community and apt-get is just God 😮

For the Scripting language we are using PHP , I think no need to explain PHP now a days so we can say this “how to make video site using open source“.

Host or Server:

If you are using a hosting account then make sure these softwares are install in your host and if you are having a dedicated server then hureeeeh ,you need not to be worry about anything .

So i think that the basic example it sufficient now starts with mechanism

Video Conversion and Thumbnail Generation:

As previously said we are using Ffmpeg so now how we’ll use ffmpeg with PHP. For all this purpose we are using exec function . The video conversion commend is

exec(“/usr/bin/ffmpeg -i /mnt/videos/process/”.$file.” -f flv -b 500000 “.$fileFlv);

$file : This is the variable for the source media file which you want to convert e.g example.avi

$fileFlv : This is the variable for the converted flv file e.g example.flv .For more on PHP and FFmpeg check here .

so now you have a converted .flv file now its time to grab thumbnails from the video

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1″);

$file : This is the variable for the source media file which you want to convert e.g example.avi.

$image: This the variable for the grabbed thumbnail from the video

For more in thumbnails generation please have a look Here .

Get length of the Video File :

This is also a useful technique , suppose you want to generate more than one thumbnails in specified interval let say you want nine thumbnails of the video . So at this point you must get the length of the video

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” 2>&1 | grep Duration | cut -d ‘ ‘ -f 4 | sed s/,//”)

$file : This is the variable for the source media file which you want to convert e.g example.avi. For more on video length calculation please have a look Here

So if you calculate the length from the commend , now you can generate nine thumbnails like this

 

$cmd = exec(“$ffmpeg -i /mnt/videos/process/”.$file.” 2>&1 | grep Duration | cut -d ‘ ‘ -f 4 | sed s/,//”);
$array = explode(‘.’, $cmd);
$time = split(‘:’,$array[0]);
$total = ($time[0] * 3600) + ($time[1] * 60) + $time[2];
$second = “5”;
$second2 = ceil($total*1/8);
$second3 = ceil($total*2/8);
$second4 = ceil($total*3/8);
$second5 = ceil($total*4/8);
$second6 = ceil($total*5/8);
$second7 = ceil($total*6/8);
$second8 = ceil($total*7/8);
$second9 = $total-5;


 

So in this way you will get nine equal interval as described here as $second1, $second2 ……. . Now you can make nine thumbnails from the video as like

 

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image1 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second2 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image2 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second3 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image3 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second4 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image4 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second5 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image5 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second6 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image6 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second7 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image7 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second8 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image8 2>&1″);

exec(“$ffmpeg -i /mnt/videos/process/”.$file.” -an -ss $second9 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image9 2>&1″);

 


So now you have have converted FLV file and nine thumbnails with flow-player . I think this is sufficieint data to conquer the world .enjoy 😉

If you don’t want to stream your video in HTTP you can use RTSP+RTP as well for this you have to use streaming server like DSS(darwin streaming server) or RED5 . I’ll suggest to use darwin its open source and handy to use

We are not done yet on the streaming media, if you have any problem or suggestion drop a mail in our mailbox or just put a comment in the commentbox .we’ll respond you as soon as possible thanks …..