Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

place your ad here

Web Premium



Get Qwest High Speed Internet



Video streaming with PHP and xmoov

November 24th, 2008 . by polygeek

pgTV
Watch the video tutorial:

Streaming video with xmoov/PHP

How to set up xmoov to create a video player that can stream videos on an server without installing any streaming server software. Includes overview of video player built with Flex.

[ Other video tutorials ]

It used to be that if you wanted to do video streaming you had to use Flash Media Server or that Red5 thing. The problem with that is first the cost and secondly you can’t just toss this stuff on your server and say go stream. But xmoov fixes all that, very simple to use and for the low, low, low, discount price of absolutely nothing.

What is streaming? ( skip to the next heading if you already know this stuff. )

Code to download

  • xmoov : php code to do the streaming
  • FLVMDI : metaData injector
  • example videoPlayer : source code for a Flex video player built to do xmoov streaming
  • Sample videos : videos with keyframe data injected into the metaData.

In case your not familiar with video playback there are two methods of delivery: progressive and streaming. Playing a video progressively is much like downloading an image from a server. You don’t need any special software on the server. The downside to progressive is that users can’t seek to a portion of the video until it has been downloaded.

That isn’t a big problem if you have fairly short videos, like commercials or maybe a music video. But for long videos it can be an issue.

Streaming a video allows the user to jump to the very end of a video and watch that without downloading all the bits that came before.

Another difference between the two is that progressive videos end up on the user’s browser cache. With streaming you can keep it out of the cache which is important for videos that you don’t want users to redistribute – like movies.

You can read more about Flash video streaming at the Wikipedia.org.

Installing xmoov
xmoov is simple to install. How simple? Well, can you FTP? Cause that’s about all you need to do. Just drop one little file – xmoov.php – into the root of your server. One thing to look at is the opening PHP tag in the xmoov.php file. I’m just learning PHP and one thing I’ve noticed is that sometimes a block of PHP script begins with: <? and sometimes it begins with <?php. Apparently that’s a server setting or something. The developers for

view source

xmoov must have their servers set up so that they don’t have to include the php part of the <?. But that script wouldn’t work at all on my development system here or on my host. If you are having trouble getting the code to work then just add the php to the <? and that might fix your problem, like it did for me.

How xmoov works
With progressive videos you would tell your video player to play a video. Again, it’s much the same as telling an <image> component in Flex what it’s source is. Here’s some Flex code as an example:

<mx:VideoDisplay
source="http://polygeek.com/videos/sea.flv"
width="320" height="240" />

With xoov it’s nearly the same. You need to tell the <VideoDisplay> component to play everything through the xmove.php script. All you would need to do is change the source to this:

source="{ videoFileName }"

Where videoFileName = “http://polygeek.com/xmoov.php?file=sea.flv&position=0″;

You can see that you’re passing the file and position to the xmoov. It’s pretty obvious what the file is. The position is the number of bits into the movie from where the script will start sending the file stream. In this case we’re starting at the beginning.

This is an ultra-important point: you can only stream from the file position of a key frame in the video. You can’t just make up a number of bits into the stream and expect it to work. So, the question is, where are the key frames?

Key frame, key frame, where art thou key frame?
Here’s what you do to get a key frame: you fake a hand off to the left and then run the QB on a bootleg to the right where he… Sorry, the game is on.

Okay, here’s what you do to find the key frames: you need some tool that can inspect your video files for key frames and then add the entire list to an Array in the metaData. It’s quick and simple to do. I used FLVMDI – download link is about 2/3rds of the page down – which has a command line and GUI version. Unfortunately I couldn’t get the GUI version to work but the command line worked like a charm.

The download page for FLVMDI has full documentation. Here are the parameters that I used for the command line version: flvmdi.exe C:\wamp\www\videos /x /k

What that will do is inject the key frames metaData into the file and also output an XML file with the same data. If you don’t want the XML file then leave off the /x parameter.

If you want to inspect the metaData then you can use RichFLV – a free AIR app – that will play videos and gives you a panel to display the metaData. In the case of the key frame metaData it will only show an [ object object ] for the value. But at least you know it’s there. Obviously you can just run your app in debug mode and set a break-point in the onMetaData event handler to inspect the values if you want – more on that later.

Seeking to a key frame
Now that we’ve gone through all this work you might want to actually set your video player up to seek. When the user seeks forward, or backward, in the video you need to know the time value in the movie that they are targeting – in seconds.

The metaData that FLVMDI has injected is two parallel arrays. The times array is a list of all the time values in the video that correspond to a key frame. You will need to loop through that array looking for the value that is nearest to where the user wants to seek. Once you have the index of the times array then you can use the corresponding element in the in the filepositions array – which is a collection of all the file positions that correspond to a key frame – to tell xmoov what part of the video file to start sending from.

The code in my onSeek() method looks like this:

private function onSeek( e:SliderEvent ):void {
var seekTo:Number = e.value;
var i:int = 0;
while( _keyFrame_timePositions[i] < seekTo ) {
i++
}
vidWin.source = "http://polygeek.com/xmoov.php?file=?sea.flv&position=" + _keyFrame_filePositions[i];
}

Settings in the xmoov.php file

There are only two settings that you must change from the defaults of the xmoov.php file. When you download it find lines 33 and 36. By default they look like this:

define('XMOOV_PATH_ROOT', '' );
define('XMOOV_PATH_FILES', 'videos/' );

If you are testing on a local machine you can set the first line to something like ‘C:\wamp\www\’. On my server I used: $_SERVER['DOCUMENT_ROOT']

The second line is just the folder on your server where the videos are located. That’s all the changes that you need to make to xmoov.php. There are other settings for bandwidth limiting and caching that you can read more about in the documentation – if you can find it.

So in the end my file looks like this:

define('XMOOV_PATH_ROOT', $_SERVER['DOCUMENT_ROOT'] );
define('XMOOV_PATH_FILES', '/videos/' );

Issues
Originally when I used FLVMDI to inject metaData I added the parameter to add an event for when the last second of video is played. For some reason that caused the <VideoDisplay> to bounce at the end of playback back a second or so. So if you don’t need to know when the last second of video plays then I’d leave that off.

You can see in the source of the example that I had to do some trickery to keep the timeline-thumb ( that the user drags to do the seeking ) from bouncing after a seek.

Closing
That pretty much covers it. The source code in my example is extensively commented so give it a looksy.

Please let me know if you have any questions or comments on how this could be improved.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

Technological tipping points

February 12th, 2007 . by polyGeek

In nature a population of organisms in an ecosystem compete over nutrients. Most of the time the ecosystem is in a state of predictable flux. But a newly introduced organism or a change in resources can throw everything into chaos. If the ecosystem passes a tipping point then a new state of predictable flux will be established.

It doesn’t take much of an imagination to see that the web is very much like an ecosystem of organisms and resources. The web ecosystem recently went through a tipping point. Web content had been relatively stable since the mid 90s. All hinged around text and graphics. Then broadband connections began to grow in number. That allowed a previously undernourished form of content to rapidly expand – video.

Initially there were a handful of competing video organisms trying to dominate the ecosystem. Then the most unlikely organism rose to dominate. And it regulated the others to limited and withering niches.

Video, video, everywhere

The BBC just admitted recently that the main reason they don’t use Flash video is timing: Flash video became the obvious answer after they had committed their resources to other formats. Do their users like it? No, and they have let the BBC know about it. But it isn’t going to change anytime soon. That’s because businesses rarely change unless there is a clear path to profits. In the BBC’s case they would have to buy lots of software, make changes to the servers, and retrain their engineers, who by now have automated and optimized their workflow so that they can spend most of the day checking youTube for broken links.

While nearly every new video based website coming online today uses Flash video CNN, MSNBC, BBC, NFL, and many others who were early adapters continue to use Windows Media/Real Player.

Go into the light

We all know that webTime moves very fast. Sometimes it seems that what’s popular now is yesterdays news tomorrow. I’m sure that many of the aforementioned sites would love it if they had the foresight to jump on the Flash bandwagon from the beginning. The trick is knowing where the spotlight is going to shine before the crowds arrive. Then you can just stand there and soak up the attention.

Okay, so where is the spotlight going to shine next?

Lets take a look at the web ecosystem and see if we can discover what the next tipping point might be so that we can predict where the spotlight is headed next.

One for you. Two for me.

Given that Windows makes up over 90% of the OS market ask yourself this: do you think that the Windows OS (Vista/XP) marketshare will grow at the expense of Apple and Linux?

No? I don’t think so either. It’s a given that the market will grow for Windows OS, because there are just more damn people in the world all the time, but the market share has reached its peak penetration.

Apple continues to erode into the Microsoft OS dominance. More than anything else the ability to run both Windows XP and OS-X on the same PC makes it easier to migrate over. You can dual boot both until the day you realize that you don’t want to use XP anymore. (Note: I work for Microsoft and haven’t used an Apple OS since the Apple IIe.)

Linux is bound to expand its marketshare. For one thing it’s very small right now – around 1-2% – so there’s nowhere to go but up. Plus, Linux is free, or nearly so which is attractive. Plus it is very stable, and fast. I like Neal Stephenson’s analogy, circa 1999: Windows is like the family station wagon that’s always breaking down and expensive as hell to fix. Apple OS is like an expensive European sports car, fast, and cool looking but a little finicky. And Linux is like a modern M1 Army tank that is faster and handles better than a European sports car while getting 100 MPG. And did I mention it’s free?

There are two main drawbacks to Linux: consistency between distros and lack of software, particularly hardware drivers.

Hopefully the newly formed Linux Foundation – HP, IBM, Intel and others – will help fix the consistency issue. They will also be able to pressure other hardware makers into providing Linux compatible drivers for their printers, video cards, cameras, etc.

As the market share for Apple and Linux continue to grow the demand for cross platform applications will grow. The more cross platform applications there are the easier it is for someone to switch from the Windows platform to Apple or Linux. That’s a feedback cycle.

This has happened before. Not too long ago a small company in the Seattle suburbs found itself on the positive end of a feedback cycle. Their OS was cheap and could be run on cheap computers. Businesses flocked to their platform. Their rapid rise was a boon for other software companies who could focus their products on just one platform and not bother with creating versions for two or more operating systems. That worked just fine until Microsoft added your application to their suit and then your business died. This is how Microsoft became king of the hill.

Today many software companies support only Windows XP. Especially if their software targets business users. And rightfully so. Why devote all the resources it takes to develop cross platform applications when you can focus on just one dominant platform. You shouldn’t. Not unless someone came up with a way to have one code base for your software that would work across platforms. Good luck with that because it’s been the holy grail of software development for decades.

Look at this shiny cup

There is a project out there code named Apollo from the good guys at Adobe. Apollo will enable developers to create software that can do nearly anything that any other application can do but with a twist. With one code base you can create an application that will run the same on Windows, Apple OS-X or Linux. And it’s a very fast development environment. Like how fast? How about creating a standards based web browser in 60 seconds? Or a file system explorer in 30 seconds? Those are projects that could take a team of developers months to create a few years ago. For Apollo that isn’t much more complex than creating the classic “Hello World”.

Is Apollo going to dominate the market from day one? No, certainly not. But it might be the resource the environment needs to reach a tipping point. There are already a handful of projects in the works and the product isn’t even in Beta yet. So look to see a splash in Summer’07 when Apollo goes 1.0.

Initially you’ll see a lot of today’s Web 2.0 Rich Internet Applications going to Apollo. That’s because many of these RIAs currently use Flex. Flex and Apollo use the same development environment so the transition from browser to desktop will be smooth in most cases. Plus AJAX applications will run on Apollo and give them access to the local file system. You’ll also see a cornucopia of desktop widgets created with Apollo coming out in Fall’07.

By this time next year you might see entire office suites created with Apollo. It has those capabilities. And of course there will be applications that have no real counterpart today because the world of web enabled applications is just starting.

Aziz. LIGHT!

Get ready to hear the terms like platform agnostic, platform independent, neutral platform, cross OS, etc. in every other tech article you read. It’s where the spotlight is headed.

Of course Microsoft isn’t going to sit idly by. They have their own initiatives such as Windows Live, whatever that is today, and WFP/e. Forget for a moment how good or bad these products might be. There is no way Microsoft is going to be able to compete in a cross OS space because that would be akin to chopping off one of their own legs – namely the Operating System division. No matter how much business since it might make from the standpoint of the Office team the company just can’t fully embrace platform independence.

What do you do if you drop your OS in a pool of molten Adobe?

According to all the press and pundits out there Google is the biggest threat to Microsoft. No doubt there will be some tug-o-war going on between the two companies over their overlapping territories. However, both companies should pay attention to the rumblings underfoot. They might find out that they are playing tug-o-war on a volcano made of Adobe. If you haven’t heard those rumblings sound like IPTV, P2P, VoIP, and FlashLight3 Video. And those are just the ones that are leaking out.

How do you know she is a witch?

Lets step back and think about what an OS is. My, possibly oversimplified, definition is: a place where your applications run or if it sounds better a runtime environment. No matter what Microsoft might say Internet Explorer is not a part of the Operating System. Certainly no more so than NotePad is. Neither are Windows Media Player, Windows Explorer or any of the other bundled apps who have their roots artificially entwined in the innards of the OS. That’s just what they are: bundled apps. And those apps run on Windows. Not in it.

Guess what. Apollo is just as much an Operating System as Windows Vista. Applications run on Apollo. Of course Apollo on a Windows PC is like an OS on top of an OS. That’s really overkill. It would be much better to use an OS under Apollo that is lightweight, fast and stable. Like, say Linux.

The typical business user needs: access to email – preferably on and off-line, a browser, wordprocessing, spreadsheets, slideshow, IM, and a media player. Each of those were killer apps when they happened on the scene but none of them are much of a challenge for a decent software company to create these days. In fact, you can get versions of every one of these apps for free. Oh, wait, you can even get the operating system for free.

Would this setup satisfy everyone? No, of course not. But enough will switch that might push the web ecosystem into a tipping point where Windows is no longer the king of the hill.

Here’s the real kicker: if this happens and Adobe Apollo ends up pushing Microsoft off the hill then the landscape will be radically altered. That’s because there won’t be a hill left to speak of. The Apollo run-time is going to be distributed for free. Linux is free. There will be no OS market to speak of.

Neal Stephenson said it himself in his book In the Beginning was the Command Line:

…it is the fate of operating systems to become free.

Adobe Software Australia Compare Adobe graphic software and buy from Australian stores

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

2 minute custom video interface in Flash

January 1st, 2007 . by polyGeek

Here’s a demonstration using my video.Maru pre-compiled clip to create a custom video interface in Flash in less than 2 minutes. Okay, it isn’t pretty. I’m just going for speed.

To start with the FLA is completely blank with absolutely no code. The only thing in the Library is my pre-compiled MovieClip which you can download yourself.

There is also a lot more functionality than this. I just wanted to cover the basics.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

Seeking in Flash Video

December 8th, 2006 . by polyGeek

Have you ever noticed that when you use the playhead control to scrub through a video that when you release it the video plays smoothly from the frame you see however the playhead will jump, sometimes a lot, just a little bit forward.

I first noticed this when working on my videoGenerator code and it was driving me nuts. Finally, I went to youTube.com and a few other Flash video sites and tested their video. You know what? I got the same result.

That lead me to believe that maybe it wasn’t my code but something to do with Flash video. Turns out I was right.

First, a quicky on how Flash video, and many other video formats, are encoded. If you could look at the individual frames of a video stream you would notice that most of them are incomplete pictures. For sure the very first frame will be complete but the next few frames after that will only be the parts of the picture that have changed from the first. After a hand full of frames you get another complete picture and then the process starts over again. These complete frames are called key frames. The more key frames you have the larger your filesize will be. However, if you don’t have enough the video quality begins to suffer. Especially when the video content is changing a great deal during every frame, like an action scene. (You can read all about how to best choose your key frame values here.) I’m going to get on with how this affects scrubbing the video.

Very simply, when you are scrubbing over Flash video – moving the playhead around – the Flash player can only show you the key frames. It can’t show you the incomplete frames in between. Here’s a good example of how that looks. The video below is simply a white bar that moves across the video window. You can use the scrubber to move back and forth through the video and see the jumpiness. I included multiple versions with different key frame values so that you could see the effect.

Note: these videos were all encoded at 15 frames per second. A key frame interval of 15 ends up being 1 keyframe/second and a key frame interval of 5 ends up being 3 keyframes/second.

Also, to see the effect that I’m talking about here you have to move the playhead very slowly. If you move it quickly the jumpiness is a result of the Playing trying to catch up with you and has nothing to do with key frame placement.

The file sizes for these videos are as follows:

interval of 1 = 1,350 KB

interval of 5 = 351 KB

interval of 10 = 224 KB

interval of 15 = 181 KB

interval of 90 = 109 KB

interval of auto = 124 KB

You can see that having a very small interval blows up the file size but at a certain point having a very large interval does little to save file size.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

Dvorak on FLV

October 7th, 2006 . by polyGeek

John C. Dvorak at PC Magazine gives Flash Video another thumbs up in his column Watching Things Fall Apart . Is there anyone out there who isn’t on the dole of a competitor who has a compelling reason not to use Flash for web video? If there is I haven’t heard from them.

I think that what’s best about this is that users are getting a good experience watching Flash video. Granted most people out there watching YouTube or Google Video have no idea that Flash is involved in the process of watching the video but more and more are learning and therefore creating a positive association with Flash.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

   



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud