Streaming video using lighttpd / nginx, Mplayer (Mencoder), Ruby, Flvtool2

    As soon as there is a need for a video service on a site or portal, immediately the question arises for developers about converting the video files uploaded by users of the resource into a flash video format that the browser understands.

    The study of this problem is reflected in the next article.

    Stages of implementing streaming video:
    1. Server for working with video streaming module enabled
      There are two most common options for implementing a platform for video processing:
      • lighthttpd
        It requires:
      • nginx
        For it is necessary:
        • Install nginx .
        • Recompile nginx with the –with-http_flv_module option. In the new http_flv_module module, the streaming function was first implemented in version 0.4.7 (there was an annoying error in the streaming implementation, which is fixed by http://blog.kovyrin.net/files/flv_fix.patch). In 0.4.8, there is no longer any error.
          # ./configure --with-http_flv_module ...SOME-OTHER-OPTS...
        • The next step is to activate streaming for flv files in nginx.conf:
          server {
          ...
          location ~ \.flv$ {
          flv;
          }
          ...
          }
        The nginx server according to reviews is a more acceptable option.
        See also here .
    2. Converting video files to a format intended for transmission over the network as a Flash Video stream (flv)
      There are two most common conversion options:
      • using the Ffmpeg module For
        more details see here .
        However, this is not the most suitable tool for the task, because it organizes two-stage transcoding of video through an intermediate format understood by ffmpeg.
      • using MPlayer / MEncoder
        For this you need:
        • Download the source package for mplayer from the official mplayer website and compile them;
        • Minimize the set of disabled codecs at the compilation stage.
          Example of transcoding with mencoder: Documentation - see here . Please note that a 170 mb file will be transcoded within 17-20 minutes. Those. you must either prohibit files of a similar size, or enable the process in the background.
          mencoder The.Simpsons.18x05.avi \
          -o simpsons.flv -of lavf \
          -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc \
          -ofps 25 \
          -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 \
          -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames \
          -vf scale=320:240

        Note:
        The mplayer already have codecs for decoding audio. For Ffmpeg, you must install an additional Lame codec for decoding audio.
    3. Retrieving metadata for a video file.
      Obtaining metadata is necessary in order to be able to scroll through the video.
      This requires the flvtool2 utility .
      It is written in Ruby. For flvtool2 to work , a version no lower than 1.8.4 is required - download .
      To install the flvtool2 package :
      gem install flvtool2-1.0.6.gem
      To update the meta-information in the file:
      flvtool2 -UP simpsons.flv
      Note:
      The source of blog.kovyrin.net indicates that the current version of flvtool2 contains a small but very unpleasant error that prevents using this software with files generated by mencoder. When you run flvtool2, you will get the following result:
      /usr/local/lib/site_ruby/1.8/flv/amf_string_buffer.rb:163: [BUG] Segmentation fault
      To solve this problem, open the file lib / flv / amf_string_buffer.rb in the flvtool2 source code and change line 163 from:
      write [(time.to_i * 1000.0)].pack('G')
      to:
      write [(time.to_f * 1000.0)].pack('G')However, the webnext.ru source does not indicate a similar error - it may already have been fixed in new versions of flvtool2.
    4. Creating a queue
      It is necessary to take into account that video conversion is a rather resource-consuming process. It must be run with low priority. Plus, you must create a queue from files awaiting conversion. The queue length must be calculated based on the configuration of each specific server.
    5. Flash player, understands strimm video
      FlowplayerJW FLV MEDIA PLAYER

    Also popular now: