📜 ⬆️ ⬇️

flash video on web pages and php

This article is a reprint of an article dated November 16, 2007 from my blog, the link to which can be found in my own profile - but, since it so happened that this topic seems to be still interesting to many, besides not so long ago I read a report on this topic at the PHPConf conference ... well, judge for yourself.

Introduction


Recently, the technology of broadcasting videos via the web is becoming more and more widespread - without the need to download media content to the user's machine, with viewing directly from the browser. Thus, you can view a fairly large number of different video formats, however, this requires the presence of certain plug-ins on the user's side. Obviously, the most common such plugin is flash-player (well, well, well: to be precise, it cannot, of course, play video by itself - but it makes it easy to create an application that can play video content). A striking example of this approach is youtube.com . I will try to talk about how to independently organize flash video broadcasts on my web page and what are the approaches to this problem.


It should be noted that the article does not claim to be any innovative at all and is pure compilation from sources available in open form. I hope, nevertheless, that this compilation will give many the necessary minimum knowledge of the subject of the report, which will lay the foundation for its own development and research.
')

1. About FLV format


So, flash-player plays video in FLV format, does not understand other formats.
More information about the format itself can be found, for example, here: en.wikipedia.org/wiki/FLV , as well as, of course, on the website of Adobe, the manufacturer of flash-player . As part of this report, we will not need detailed knowledge of the format, and those that are needed are listed below.
An FLV file consists of a title and the actual movie itself.
The title contains certain meta-information about the film: duration, size, etc., etc. In fact, we are interested in this meta-information only in one circumstance - namely, that it contains data on key frames of the film (their position on the time and byte scale). Meta-information itself is an arbitrary nesting associative array, serialized into AMF format, which is one of the accepted standards when developing applications on flash.
Note : for PHP, there are several libraries that allow (de) serialization to / from AMF.

2. How to organize the translation of FLV-files on web pages


In order to consider the broadcast successful, two conditions must be met: the server successfully showed the content, and the client looked at it no less successfully. Accordingly, two parts of the application are needed - the server and the client. The client is a flash application that can play a video stream from the specified URL. There are many similar programs, and we will not dwell on them here. Let's talk about the server side. So, what are the possible ways of implementation?

2.1. Download - Download


This is the easiest way. It requires a simple client that simply requests a video stream at a given URL and reproduces it. The server only needs to process the HTTP request and issue the corresponding content. No specific software needed.
“Then why are there any other other ways?” - such a question is difficult not to ask. However, not everything is so smooth with this method ... The fact is that if it is great for showing small video clips, up to 2-3 minutes long, then films cannot be longer than that: for a user to watch any a piece of film, it is necessary that this piece has already downloaded to his computer. In other words, being at the beginning of the film, we cannot move the “slider” of the timer to the end and see the final credits. This method also does not provide any opportunities to protect video content from downloading.

2.2. Streaming - streaming video


This method is perhaps the most advanced. Here there is the possibility of rewinding to an arbitrary place in the stream, certain content protection mechanisms (frankly speaking, these mechanisms only make it difficult to obtain content, being, in fact, protected only from an incompetent cracker). Also useful feature - the organization of "live" video broadcasts. If you need live broadcasts - you need to stream, you have no other way out.
Well, the same question arises as with the previous method: “if streaming is so good, then why are there any other other ways?”. Streaming is good, but not everyone can afford it. For a media server (for example, "Flash Media Server") will have to pay a tidy sum. However, there are also open source solutions, for example, ffserver (which, however, does not quite fit the topic of the report, because it does not know how to stream files), as well as Red5 , which is written in Java and therefore also not suitable for everyone.

2.3. HTTP streaming (streaming video over HTTP)


From the name you can make a guess that the third method is a combination of the first two. In some approximation, we can assume that this is so. As we have already noted, a big disadvantage of the 1st method (download) is the impossibility of rewinding to an arbitrary place of the film. In HTTP-streaming, this problem is solved as follows: when rewinding to a place that has not yet been downloaded to the user's machine, the current download stops and a new request is sent to the server, containing a pointer to where to start downloading from the movie. There are certain subtleties that we will discuss in more detail in the next chapter. As for the advantages and disadvantages of this approach ... In my opinion, to translate files is the best option. It is simple enough to implement it in any language (scripting like PHP or Ruby - or compiled as C), so you can decide what to use based on the requirements for speed of development, speed of the resulting application, existing Software and other, other. Also for such popular fast web servers as Nginx and Lighttpd there are ready-made modules for broadcasting FLV files written in C and working very fast.
In this way I will dwell a little more ...

3.Http-streaming


So, let's take a closer look at the option of broadcasting flash videos using Http-streaming. As we have already said, in order to play a movie from an arbitrary location, the client sends an HTTP request to the server containing the “coordinate” of the location from which the video stream should be output. well, for example: localhost / flv / film.php? start = XXXXX . What is this coordinate? This is just the byte number from which the desired frame begins. By the way, you should always start playing the FLV file from the key frame.
“However, let me! Where does the client know this byte number from which the frame starts? yes even the key? "
I answer. Remember, at the beginning, when I briefly talked about what the FLV file consists of, I mentioned the meta information that is contained in the header? This meta-information contains data about the key frames of the film (their positions on the time and byte scale). Thus, the client can always find the closest key frame to the position in the stream that the user requested - and translate it into bytes, and send them to the server.
And what about the server? Well, actually, his task is now minimal: process the request, read the parameter value (in our case it is the $ _GET ['start'] variable), and issue the required video file starting with the requested byte. It's almost like that. Almost, but not quite. The FLV file must contain a header. If the byte requested by the user is not zero, then before outputting the contents of the file, starting with this byte, you need to insert the minimum possible header (to be honest, I did not understand in detail what it is, but I guess it is an empty array or object, serialized to AMF and prefixed with “FLV” characters).

3.1. Ready solutions: lighttpd , nginx + http_flv_module , flv4php .


Http-streaming support is implemented in the popular lighttpd and nginx web servers. In the case of using these solutions, you only need to put the FLV files in a place accessible to the web server, everything else is a server and client affair, there is no need to write any program code for the server part. The client will have to request FLV files by adding the “start” GET parameter to the URL, for example, local-nginx / sample.flv? Start = 12345 .
There is also a free solution (possibly more of them) based on PHP - flv4php . Its big plus is that in this project there is a ready player - a client for HTTP-streaming. The disadvantage is that this solution is rather heavy, and, according to my tests on my working machine, it is very processor-intensive (a strange phenomenon, which we did not find an adequate explanation, however, away from sin, we rushed to refuse to use the server part of flv4php and they limited themselves to borrowing a player from them, which, after being modified for our needs, began to serve us faithfully). Another drawback is that the first frame of the film is used as a “thumbs” (picture that the user sees when opening the page on the player’s screen), and this behavior is not customized. I admit the idea that flv4php can be configured in the best way, and get him to do what he needs without wild workloads on the processor. Nevertheless, a small fragment of the PHP code below does almost the same thing as flv4php - and is guaranteed to have good performance :)

3.2. Do it yourself or everything just seems complicated.


<?php

$start = (int) filter_input(INPUT_GET, 'position', FILTER_VALIDATE_INT);
if ($start < 0) die("Incorrect request");
// open file for reading
$fp = fopen('sample.flv', 'r');
$fsize = filesize($file);
if ($start > 0)
{
// seek to requested position
fseek($fp, $start);
// FLV header for the movie part. Magic. Trust me ;)
// Header code is completely taken from flv4php project
$header = "FLV" . pack('C', 1 ) . pack('C', 5 ) . pack('N', 9 ) . pack('N', 9 );
header("Content-Length: " . (strlen($header) + $fsize - $start));
echo $header;
} else {
header("Content-Length: " . $fsize);
}
while(!feof($fp)) {
print(fread($fp, 1024));
}
fclose($fp);

?>


As you can see, everything is extremely simple, I think, explanations are superfluous.

4. Conversion to FLV from other formats. Basics of ffmpeg & mencoder . Meta-information and how to shove it into the FLV-file.



Whew, practically, we are done. In this chapter, I will talk a little about how to convert video content into FLV format and how to stuff meta-information into ready-made movies that is necessary for implementing HTTP-streaming. I will not dwell on software available under MS Windows, instead focus on * nix-solutions (it’s no secret that most web projects run on servers running * nix / Linux / FreeBSD, and this information may be useful).
The most famous and powerful projects are ffmpeg and mencoder . Both programs work with most currently known video formats, have an extensive codec base and / or the ability to connect custom codecs. Both work with good speed (however, you understand that video processing is a thankless task and takes a lot of resources and time).
To convert an existing movie to FLV format, you need to run about the following commands:
ffmpeg :
$ ffmpeg -i sample.avi sample.flv
(see ffmpeg documentation - ffmpeg.mplayerhq.hu/ffmpeg-doc.html for more options)
mencoder :
$ mencoder sample.avi -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -oac mp3lame -lameopts abr:br=56 -srate 44100 -o sample.flv
(See the mencoder documentation - www.mplayerhq.hu/DOCS/HTML/en/index.html for more options.)
And again about the meta-information.
Neither ffmpeg nor mencoder insert meta information about key frames into the header of the resulting FLV file. Meanwhile, this information is absolutely necessary in order to organize HTTP-streaming. How to be? To the aid of the developers they come again - open-source products :). For example, a small program called Yamdi (Yet Another Meta-Data Injector): sourceforge.net/projects/yamdi . With its help, even large FLV-files (well, say, in gigabytes) can be provided with the necessary meta-data in a very short time.

Literature and links:


flv streaming with lighttpd : blog.lighttpd.net/articles/2006/03/09/flv-streaming-with-lighttpd
Flash Video Streaming with Nginx :
blog.kovyrin.net/2006/10/14/flash-video-flv-streaming-nginx

This section is not completely complete, in fact, of course, I had to sort out a little more :)

Source: https://habr.com/ru/post/30402/


All Articles