Search code examples
flvh.264

How to generate FLV stream from raw h264 which can be played by Actionscript NetStream?


I have an issue with FLV stream generating. I've developed DVR system, an it should be able to stream video in FLV format (to play it on Actionscript NetStream). I receive video from encoder in raw H264 NAL units (0x00 0x00 0x00 0x01 ), also I can recognize is encoded frame IDR or non-IDR.

My solution to create FLV stream (based on Adobe spec: Video File Format Specification Version 10) was:

  1. wait for IDR frame;
  2. put FLV header
  3. put PrevTagSize(0)
  4. put FLV tag with video tag VIDEODATA with AVCVIDEODATA
  5. put PrevTagSize
  6. repeat steps 4,5 till end of streaming.

Stream looks good, and can be playable by ffplay, mplayer, vlc, etc. But not played by player based on Actionscript NetStream.

So, I've get raw h264 data and convert it to FLV using ffmpeg:

ffmpeg -f h264 -i d1.h264 -vcodec copy -f flv d1.flv

and try to compate both flv's my and ffmpeg's.

First of all I see that ffmpeg adds AVC sequence header, immediately after FLV header. I've started to do the same, but NetStream still not support my stream, and also another players ceased to play it.

Ok, then I've continue to compare flv's. Now I see that NAL unit headers in ffmpeg's coded FLV a bit changed, but I can't understand what the meaning of the changes. I read many specs, but nothing helpful. Did anybody can clarify me this?

Fo example my NAL units looks so: 00 00 00 01 XX XX ... - for all units

FFmpeg NALs: 00 00 [14 BA] 61 9A ... - non IDR (two bytes variable) 00 00 [7A 02] 65 88 ... - IDR (two bytes variable) 00 00 00 40 06 05 ... - SEI

Is there added some counter or anything else?

Will be happy to see any ideas, links, etc.


Solution

  • There are two common H.264 bitstream packing formats.

    1. Annex B contains start codes: 00 00 01
    2. MP4 is length prefixed XX XX XX XX

    You are creating annex B but it seems like you need mp4 packing format (length prefixed) for FLV. You have to remove (00) 00 00 01 and add the length as prefix.